imagecopyresized() 函数用于拷贝图像或图像的一部分并调整大小,成功返回 TRUE ,否则返回 FALSE 。
语法:
bool imagecopyresized( resource dst_im, resource src_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 )
imagecopyresized() 的典型应用就是生成图片的缩略图:
<?php
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);
?>
上面的例子将原图缩小为原来的一半尺寸。