Heim > Backend-Entwicklung > PHP-Tutorial > php等比缩放图片

php等比缩放图片

WBOY
Freigeben: 2016-07-28 08:26:18
Original
1361 Leute haben es durchsucht

步骤如下:
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
 php等比缩放图片
200*140
 php等比缩放图片

代码如下:

<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>
Nach dem Login kopieren
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了 php等比缩放图片,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage