Home >Backend Development >PHP Tutorial >php proportional scaling pictures
The steps are as follows:
1. Get basic information about the picture
Use this getimagesize function
2. Determine the image resource type and create the corresponding image resource
Use this getPicType function (custom function)
3. Calculate the scaling ratio
The principle is to take the smallest scaling factor. For example: if an 800*600 picture is scaled to less than 200*200, the scaling factor is scale=200/800>200/600?200/600:200/800; because 1/4 < ; 1/3 so choose the scaling factor of 1/4
4. Calculate the scaled size
wdith = floor(800*1/4); round down
height = floor(600*1/4);
As a result, the zoomed image becomes 200*150
5. Create the target image resource
Use this imagecreatetruecolor function to create a true color image
6. Scale proportionally
Use this imagecopyresampled function to achieve scaling
7. Output image
Use this outputImage function (custom function)
8. Release image resources
Use this imagedestroy function
as shown before and after scaling:
750*525
200*140
The code is as follows:
<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); }); });
The above introduces the proportional scaling of pictures in PHP, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.