php基础练习–图片缩放:
<?php<BR> /**<BR> * image zoom.<BR> */<BR> function imageZoom($filename, $w, $h) {<BR> /* Arguments meaning */<BR> /* $filename: the source of the name */<BR> /* $w: you want get the image's width */<BR> /* $h: you want get the imgage's height */<BR> $arr = getimagesize($filename);<BR> $src_w = $arr[0];<BR> $src_h = $arr[1];<BR> $src_t = $arr[2];<BR> /*1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),<BR>= TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,<BR>= IFF,15 = WBMP,16 = XBM*/<BR> $src_m = $arr['mime'];</P><P> $src_img = imagecreatefromjpeg($filename);</P><P> if (($w / $src_w) >($h / $src_h)) {<BR> $bili = $h / $src_h;<BR> } else {<BR> $bili = $w / $src_h;<BR> }<BR> $dst_w = $src_w * $bili;<BR> $dst_h = $src_h * $bili;<BR> $dst_img = imagecreatetruecolor($dst_w, $dst_h);</P><P> imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);</P><P> header("content-type:{$src_m}");</P><P> switch ($src_t) {<BR> case 1:<BR> $imgout = "imagegif";<BR> break;<BR> case 2:<BR> $imgout = "imagejpeg";<BR> break;<BR> case 3:<BR> $imgout = "imagepng";<BR> break;<BR> default:<BR> echo "The type was wrong!";<BR> break;<BR> }</P><P> $dst_filename = "s_".$filename;<BR> $imgout($dst_img, $dst_filename<span>@本文来*源gaodai#ma#com搞*!代#%^码$网*</span><textarea>搞gaodaima代码</textarea>);</P><P> imagedestroy($dst_img);<BR> }<BR> $filename = 'gg.jpg';<BR> imageZoom($filename, 100, 200);<BR>
核心:注意缩放比例如何得到,虽然这样得到的图片可能会与预想的有点差别,但是最起码保证了缩放比例。
类型的控制。