Last active
December 30, 2019 20:41
-
-
Save CristalT/20b7e70db33d31a07ceae4c6fac8d7bb to your computer and use it in GitHub Desktop.
Recorta los espacios vacíos de una imagen PNG y la guarda como JPG.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$png = 'image/path/filename.png'; | |
$img = imagecreatefrompng($png); | |
//find the size of the borders | |
$b_top = 0; | |
$b_btm = 0; | |
$b_lft = 0; | |
$b_rt = 0; | |
//top | |
for (; $b_top < imagesy($img); ++$b_top) { | |
for ($x = 0; $x < imagesx($img); ++$x) { | |
if (imagecolorat($img, $x, $b_top) != 0xFFFFFF) { | |
break 2; //out of the 'top' loop | |
} | |
} | |
} | |
//bottom | |
for (; $b_btm < imagesy($img); ++$b_btm) { | |
for ($x = 0; $x < imagesx($img); ++$x) { | |
if (imagecolorat($img, $x, imagesy($img) - $b_btm - 1) != 0xFFFFFF) { | |
break 2; //out of the 'bottom' loop | |
} | |
} | |
} | |
//left | |
for (; $b_lft < imagesx($img); ++$b_lft) { | |
for ($y = 0; $y < imagesy($img); ++$y) { | |
if (imagecolorat($img, $b_lft, $y) != 0xFFFFFF) { | |
break 2; //out of the 'left' loop | |
} | |
} | |
} | |
//right | |
for (; $b_rt < imagesx($img); ++$b_rt) { | |
for ($y = 0; $y < imagesy($img); ++$y) { | |
if (imagecolorat($img, imagesx($img) - $b_rt - 1, $y) != 0xFFFFFF) { | |
break 2; //out of the 'right' loop | |
} | |
} | |
} | |
$canvas = imagecreatetruecolor(imagesx($img), imagesy($img)); | |
imagefill($canvas, 0, 0, imagecolorallocate($canvas, 255, 255, 255)); | |
imagealphablending($canvas, TRUE); | |
imagecopy($canvas, $img, 0, 0, 0, 0, imagesx($img), imagesy($img)); | |
imagedestroy($img); | |
imagejpeg($canvas, 'firmas_tmp/' . $documento . '.jpg', 90); | |
imagedestroy($canvas); | |
unlink($png); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment