欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

裁剪图片PHP代码

程序员文章站 2022-04-07 21:16:16
...

下面的例子裁切图片的左上角的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
相关标签: 裁剪图片PHP代码