首頁  >  文章  >  php教程  >  PHP學習筆記:等比例縮放圖片

PHP學習筆記:等比例縮放圖片

WBOY
WBOY原創
2016-09-19 08:55:061055瀏覽

直接上程式碼,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>?>

 效果

壓縮前

壓縮後

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:thinkphp驗證碼下一篇:thinkphp驗證碼