步驟如下:
1.取得圖片的基本資訊
用這個getimagesize函數
2.判斷圖片資源類型並建立對應圖片資源
用這個getPicType函數(自訂函數)
3.計算縮放比例
原則是取最小的縮放係數.例如:800*600的圖片,縮放到200*200以下,則縮放係數為scale=200/800>200/600?200/600:200/800; 因為1/4
4.計算縮放後的尺寸
wdith = floor(800*1/4);向下取整
height = floor(600*1/4);
結果縮放後的圖片變成200*150
5.建立目標影像資源
用這個imagecreatetruecolor函數,創建一個真彩的圖像
6.等比縮放
用這個imagecopyresampled函數實現縮放
7.輸出圖像
用這個outputImage函數(自訂函數)
8.釋放圖片資源
用這個imagedestroy函數
縮放前後如圖:
750*525
200*140
程式碼如下:
<code><span>/** *<span> @function</span> 等比缩放函数(以保存的方式实现) *<span> @param</span> string $picname 被缩放的处理图片源 *<span> @param</span> int $maxX 缩放后图片的最大宽度 *<span> @param</span> int $maxY 缩放后图片的最大高度 *<span> @param</span> string $pre 缩放后图片名的前缀名 *<span> @return</span> string 返回后的图片名称(带路径),如a.jpg --> s_a.jpg */</span><span><span>function</span><span>scalePic</span><span>(<span>$picname</span>,<span>$maxX</span>=<span>100</span>,<span>$maxY</span>=<span>100</span>,<span>$pre</span>=<span>'s_'</span>)</span> {</span><span>$info</span> = getimagesize(<span>$picname</span>); <span>//获取图片的基本信息</span><span>$width</span> = <span>$info</span>[<span>0</span>];<span>//获取宽度</span><span>$height</span> = <span>$info</span>[<span>1</span>];<span>//获取高度</span><span>//判断图片资源类型并创建对应图片资源</span><span>$im</span> = getPicType(<span>$info</span>[<span>2</span>],<span>$picname</span>); <span>//计算缩放比例</span><span>$scale</span> = (<span>$maxX</span>/<span>$width</span>)>(<span>$maxY</span>/<span>$height</span>)?<span>$maxY</span>/<span>$height</span>:<span>$maxX</span>/<span>$width</span>; <span>//计算缩放后的尺寸</span><span>$sWidth</span> = floor(<span>$width</span>*<span>$scale</span>); <span>$sHeight</span> = floor(<span>$height</span>*<span>$scale</span>); <span>//创建目标图像资源</span><span>$nim</span> = imagecreatetruecolor(<span>$sWidth</span>,<span>$sHeight</span>); <span>//等比缩放</span> imagecopyresampled(<span>$nim</span>,<span>$im</span>,<span>0</span>,<span>0</span>,<span>0</span>,<span>0</span>,<span>$sWidth</span>,<span>$sHeight</span>,<span>$width</span>,<span>$height</span>); <span>//输出图像</span><span>$newPicName</span> = outputImage(<span>$picname</span>,<span>$pre</span>,<span>$nim</span>); <span>//释放图片资源</span> imagedestroy(<span>$im</span>); imagedestroy(<span>$nim</span>); <span>return</span><span>$newPicName</span>; } <span>/** * function 判断并返回图片的类型(以资源方式返回) *<span> @param</span> int $type 图片类型 *<span> @param</span> string $picname 图片名字 *<span> @return</span> 返回对应图片资源 */</span><span><span>function</span><span>getPicType</span><span>(<span>$type</span>,<span>$picname</span>)</span> {</span><span>$im</span>=<span>null</span>; <span>switch</span>(<span>$type</span>) { <span>case</span><span>1</span>: <span>//GIF</span><span>$im</span> = imagecreatefromgif(<span>$picname</span>); <span>break</span>; <span>case</span><span>2</span>: <span>//JPG</span><span>$im</span> = imagecreatefromjpeg(<span>$picname</span>); <span>break</span>; <span>case</span><span>3</span>: <span>//PNG</span><span>$im</span> = imagecreatefrompng(<span>$picname</span>); <span>break</span>; <span>case</span><span>4</span>: <span>//BMP</span><span>$im</span> = imagecreatefromwbmp(<span>$picname</span>); <span>break</span>; <span>default</span>: <span>die</span>(<span>"不认识图片类型"</span>); <span>break</span>; } <span>return</span><span>$im</span>; } <span>/** * function 输出图像 *<span> @param</span> string $picname 图片名字 *<span> @param</span> string $pre 新图片名前缀 *<span> @param</span> resourse $nim 要输出的图像资源 *<span> @return</span> 返回新的图片名 */</span><span><span>function</span><span>outputImage</span><span>(<span>$picname</span>,<span>$pre</span>,<span>$nim</span>)</span> {</span><span>$info</span> = getimagesize(<span>$picname</span>); <span>$picInfo</span> = pathInfo(<span>$picname</span>); <span>$newPicName</span> = <span>$picInfo</span>[<span>'dirname'</span>].<span>'/'</span>.<span>$pre</span>.<span>$picInfo</span>[<span>'basename'</span>];<span>//输出文件的路径</span><span>switch</span>(<span>$info</span>[<span>2</span>]) { <span>case</span><span>1</span>: imagegif(<span>$nim</span>,<span>$newPicName</span>); <span>break</span>; <span>case</span><span>2</span>: imagejpeg(<span>$nim</span>,<span>$newPicName</span>); <span>break</span>; <span>case</span><span>3</span>: imagepng(<span>$nim</span>,<span>$newPicName</span>); <span>break</span>; <span>case</span><span>4</span>: imagewbmp(<span>$nim</span>,<span>$newPicName</span>); <span>break</span>; } <span>return</span><span>$newPicName</span>; }</code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介紹了 php等比縮放圖片,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。