Home  >  Article  >  Backend Development  >  Crop image PHP code

Crop image PHP code

WBOY
WBOYOriginal
2016-07-25 08:46:15984browse

The example below crops the 100x100 part of the upper left corner of the image. You can modify the cropping range by modifying the values ​​​​of $src_x, $src_y, $src_w, $src_h.

  1. $filename= "test.jpg";
  2. list($w, $h, $type, $attr) = getimagesize($filename);
  3. $src_im = imagecreatefromjpeg($filename );
  4. $src_x = '0'; // begin x
  5. $src_y = '0'; // begin y
  6. $src_w = '100'; // width
  7. $src_h = '100'; // height
  8. $dst_x = '0'; // destination x
  9. $dst_y = '0'; // destination y
  10. $dst_im = imagecreatetruecolor($src_w, $src_h);
  11. $white = imagecolorallocate($dst_im, 255, 255, 255);
  12. imagefill($dst_im, 0, 0, $white);
  13. imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
  14. header( "Content-type: image/png");
  15. imagepng($dst_im);
  16. imagedestroy($dst_im);
  17. ?>
Copy code

PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn