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);?> 原图:http://files.jb51.net/upload/2007417223843653.jpg使用ImageCopyResized()函数生成的结果:http://files.jb51.net/upload/2007417223843379.jpg使用ImageCopyResampled()函数生成的结果:
很明显可以看到两个函数生成的图像效果是不一样的,ImageCopyResampled()函数生成的结果比较平滑,效果较好。
顺便贴一个效果,用ASCII表示图像。ImageColorAt()有一个很有趣的用处,它可以循环检查图像中的每一个像素的颜色,然后对该颜色数据进行操作。
源代码:<html>
<body bgcolor="#000000" style="line-height:6pt"> <?php $im = imagecreatefromjpeg('test1.jpg'); $dx = imagesx($im); $dy = imagesy($im); for($y = 0; $y < $dy; $y++) { for($x=0; $x < $dx; $x++) { $col = imagecolorat($im, $x, $y); $rgb = imagecolorsforindex($im,$col); printf('<font color=#%02x%02x%02x>*</font>', $rgb['red'],$rgb['green'],$rgb['blue']); } echo "<br>\n"; } imagedestroy($im); ?> </body></html>
很有趣吧,呵呵..