Maison >php教程 >php手册 >PHP学习笔记:等比例缩放图片

PHP学习笔记:等比例缩放图片

WBOY
WBOYoriginal
2016-09-19 08:55:061076parcourir

直接上代码,imgzip($src,$newwid,$newhei)这个函数带进去的分别是原图片、缩放要求的宽度、缩放的长度。代码都备注了,不懂可以留言哈哈

<span style="color: #000000;">php 

</span><span style="color: #008000;">//</span><span style="color: #008000;">压缩图片  缩略图</span>

<span style="color: #800080;">$src</span>= "xiezheng.jpg"<span style="color: #000000;">;
</span><span style="color: #800080;">$newwid</span>=640<span style="color: #000000;">;
</span><span style="color: #800080;">$newhei</span>= 480<span style="color: #000000;">;
</span><span style="color: #0000ff;">function</span> imgzip(<span style="color: #800080;">$src</span>,<span style="color: #800080;">$newwid</span>,<span style="color: #800080;">$newhei</span><span style="color: #000000;">){
    </span><span style="color: #800080;">$imgInfo</span> = <span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$src</span><span style="color: #000000;">);

    </span><span style="color: #800080;">$imgType</span> = image_type_to_extension(<span style="color: #800080;">$imgInfo</span>[2], <span style="color: #0000ff;">false</span><span style="color: #000000;">);

    </span><span style="color: #800080;">$fun</span> = "imagecreatefrom{<span style="color: #800080;">$imgType</span>}"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;">声明图片   打开图片 在内存中</span>
    <span style="color: #800080;">$image</span> = <span style="color: #800080;">$fun</span>(<span style="color: #800080;">$src</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">方便配置长度宽度、高度,设置框为变量wid,高度为hei</span>
    <span style="color: #800080;">$wid</span>=<span style="color: #800080;">$imgInfo</span>[0<span style="color: #000000;">];
    </span><span style="color: #800080;">$hei</span>=<span style="color: #800080;">$imgInfo</span>[1<span style="color: #000000;">];
</span><span style="color: #008000;">//</span><span style="color: #008000;">判断长度和宽度,以方便等比缩放,规格按照500, 320</span>
    <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$wid</span>><span style="color: #800080;">$hei</span><span style="color: #000000;">){
        </span><span style="color: #800080;">$wid</span>=<span style="color: #800080;">$newwid</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$hei</span>=<span style="color: #800080;">$newwid</span>/(<span style="color: #800080;">$wid</span>/<span style="color: #800080;">$hei</span><span style="color: #000000;">);
    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
        </span><span style="color: #800080;">$wid</span> = <span style="color: #800080;">$newhei</span> * (<span style="color: #800080;">$wid</span> / <span style="color: #800080;">$hei</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$hei</span> = <span style="color: #800080;">$newhei</span><span style="color: #000000;">;
    }

</span><span style="color: #008000;">//</span><span style="color: #008000;">在内存中建立一张图片</span>
    <span style="color: #800080;">$images2</span> = imagecreatetruecolor(<span style="color: #800080;">$newwid</span>, <span style="color: #800080;">$newhei</span>); <span style="color: #008000;">//</span><span style="color: #008000;">建立一个500*320的图片

//将原图复制到新建图片中
//imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)</span>
<span style="color: #000000;">
    imagecopyresampled(</span><span style="color: #800080;">$images2</span>, <span style="color: #800080;">$image</span>, 0, 0, 0, 0, <span style="color: #800080;">$wid</span>,<span style="color: #800080;">$hei</span>, <span style="color: #800080;">$imgInfo</span>[0],<span style="color: #800080;">$imgInfo</span>[1<span style="color: #000000;">]);

</span><span style="color: #008000;">//</span><span style="color: #008000;">销毁原始图片</span>
    imagedestroy(<span style="color: #800080;">$image</span><span style="color: #000000;">);


</span><span style="color: #008000;">//</span><span style="color: #008000;">直接输出图片文件</span>

    <span style="color: #008080;">header</span>("Content-type: ".<span style="color: #800080;">$imgInfo</span>['mime'<span style="color: #000000;">]);
    imagejpeg(</span><span style="color: #800080;">$images2</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;">保存图片 到新文件</span>
    imagejpeg(<span style="color: #800080;">$images2</span>, 'new.jpg', 100); <span style="color: #008000;">//</span><span style="color: #008000;">10代码输出图片的质量 0-100 100质量最高

//销毁</span>
    imagedestroy(<span style="color: #800080;">$images2</span><span style="color: #000000;">);
}
imgzip(</span><span style="color: #800080;">$src</span>,<span style="color: #800080;">$newwid</span>,<span style="color: #800080;">$newhei</span><span style="color: #000000;">);


</span>?>

 效果

压缩前

压缩后

 

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:thinkphp验证码Article suivant:VirtrualBox 搭建本地lamp环境