Heim > php教程 > php手册 > Hauptteil

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

WBOY
Freigeben: 2016-09-19 08:55:06
Original
1053 Leute haben es durchsucht

直接上代码,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>?>
Nach dem Login kopieren

 效果

压缩前

压缩后

 

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 Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage