首頁  >  文章  >  後端開發  >  裁剪图片PHP代码

裁剪图片PHP代码

WBOY
WBOY原創
2016-07-25 08:46:15984瀏覽

下面的例子裁切图片的左上角的100x100的部分。可以通过修改$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. ?>
复制代码

PHP


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn