PHP 拷贝图像 imagecopy 与 imagecopyresized 函数,imagecopy() 函数用于拷贝图像或图像的一部分,imagecopyresized() 函数用于拷贝部分图像并调整大小。
imagecopy() 函数用于拷贝图像或图像的一部分,成功返回 TRUE,否则返回 FALSE。
- bool imagecopy( resourcedst_im, resourcesrc_im, int dst_x, int dst_y, int src_x, int src_y,int src_w,int src_h )
- header("Content-type: image/jpeg");
- //创建目标图像$dst_im= imagecreatetruecolor(150, 150);
- //源图像$src_im= @imagecreatefromjpeg("images/flower_1.jpg");
- //拷贝源图像左上角起始 150px 150pximagecopy( $dst_im, $src_im, 0, 0, 0, 0, 150, 150);
- //输出拷贝后图像imagejpeg($dst_im);
- imagedestroy($dst_im);
- imagedestroy($src_im);
-
imagecopyresized() 函数用于拷贝图像或图像的一部分并调整大小,成功返回 TRUE,否则返回 FALSE。
- bool imagecopyresized( resourcedst_im, resourcesrc_im, int dst_x, int dst_y, int src_x, int src_y,int dst_w, int dst_h, int src_w,int src_h )
本函数参数可参看 imagecopy() 函数,只是本函数增加了两个参数(注意顺序):
imagecopyresized() 的典型应用就是生成图片的缩略图:
- header("Content-type: image/jpeg");
- //原图文件$file= "images/flower_1.jpg";
- //缩略图比例$percent= 0.5;
- //缩略图尺寸list($width, $height) = getimagesize($file);
- $newwidth= $width* $percent;
- $newheight= $height* $percent;
- //加载图像$src_im= @imagecreatefromjpeg($file);
- $dst_im= imagecreatetruecolor($newwidth, $newheight);
- //调整大小imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
- //输出缩小后的图像imagejpeg($dst_im);
- imagedestroy($dst_im);
- imagedestroy($src_im);