Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1.  
  2. function imagetrim($im, $bgcol, $pad=null){
  3.  
  4. $p = array_fill(0, 4, 0);
  5.  
  6. // Get the width and height of the image.
  7. $imw = imagesx($im);
  8. $imh = imagesy($im);
  9.  
  10. // Set the X variables.
  11. $xmin = $imw;
  12. $xmax = 0;
  13.  
  14. // find the endges.
  15. for ($iy=0; $iy<$imh; $iy++) {
  16. $first = true;
  17. for ($ix=0; $ix<$imw; $ix++) {
  18. $ndx = imagecolorat($im, $ix, $iy);
  19. if ($ndx != $bgcol) {
  20. if ($xmin > $ix) { $xmin = $ix; }
  21. if ($xmax < $ix) { $xmax = $ix; }
  22. if (!isset($ymin)) { $ymin = $iy; }
  23. $ymax = $iy;
  24. if ($first) { $ix = $xmax; $first = false; }
  25. }
  26. }
  27. }
  28.  
  29. // The new width and height of the image. (not including padding)
  30. $imw = 1+$xmax-$xmin; // Image width in pixels
  31. $imh = 1+$ymax-$ymin; // Image height in pixels
  32.  
  33. // Make another image to place the trimmed version in.
  34. $im2 = imagecreatetruecolor($imw+$p[1]+$p[3], $imh+$p[0]+$p[2]);
  35.  
  36. // Make the background of the new image the same as the background of the old one.
  37. $bgcol2 = imagecolorallocate($im2, ($bgcol >> 16) & 0xFF, ($bgcol >> 8) & 0xFF, $bgcol & 0xFF);
  38. imagefill($im2, 0, 0, $bgcol2);
  39.  
  40. // Copy it over to the new image.
  41. imagecopy($im2, $im, $p[3], $p[0], $xmin, $ymin, $imw, $imh);
  42.  
  43. // To finish up, return the new image.
  44. return $im2;
  45. }
  46. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement