PHP的GD扩展提供了两个函数来缩放图像:
ImageCopyResized(dest, src, dx, dy, sx, sy, dw, dh, sw, sh);ImageCopyResampled(dest, src, dx, dy, sx, sy, dw, dh, sw, sh);
ImageCopyResized( )函数在所有GD版本中有效,但其缩放图像的算法比较粗糙,可能会导致图像边缘的锯齿。GD 2.x中新增了一个ImageCopyResampled( )函数,其像素插值算法得到的图像边缘比较平滑(但该函数的速度比ImageCopyResized()慢)。
将图片缩小四倍的例子:
<?php
$src = ImageCreateFromJPEG(‘php.jpg’);
$width = ImageSx($src);
$height = ImageSy($src);
$x = $width/2; $y = $height/2;
$dst = ImageCreateTrueColor($x,$y);
ImageCopyResized($dst,$src,0,0,0,0,$x,$y,$width,$height);
//ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$width,$height);
header(‘Content-Type: image/jpeg’);
ImageJPEG($dst,”,100);
?>