Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function imagetrim($im, $bgcol, $pad=null){
- $p = array_fill(0, 4, 0);
- // Get the width and height of the image.
- $imw = imagesx($im);
- $imh = imagesy($im);
- // Set the X variables.
- $xmin = $imw;
- $xmax = 0;
- // find the endges.
- for ($iy=0; $iy<$imh; $iy++) {
- $first = true;
- for ($ix=0; $ix<$imw; $ix++) {
- $ndx = imagecolorat($im, $ix, $iy);
- if ($ndx != $bgcol) {
- if ($xmin > $ix) { $xmin = $ix; }
- if ($xmax < $ix) { $xmax = $ix; }
- if (!isset($ymin)) { $ymin = $iy; }
- $ymax = $iy;
- if ($first) { $ix = $xmax; $first = false; }
- }
- }
- }
- // The new width and height of the image. (not including padding)
- $imw = 1+$xmax-$xmin; // Image width in pixels
- $imh = 1+$ymax-$ymin; // Image height in pixels
- // Make another image to place the trimmed version in.
- $im2 = imagecreatetruecolor($imw+$p[1]+$p[3], $imh+$p[0]+$p[2]);
- // Make the background of the new image the same as the background of the old one.
- $bgcol2 = imagecolorallocate($im2, ($bgcol >> 16) & 0xFF, ($bgcol >> 8) & 0xFF, $bgcol & 0xFF);
- imagefill($im2, 0, 0, $bgcol2);
- // Copy it over to the new image.
- imagecopy($im2, $im, $p[3], $p[0], $xmin, $ymin, $imw, $imh);
- // To finish up, return the new image.
- return $im2;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement