Home  >  Article  >  php教程  >  PHP study notes: scaling pictures in equal proportions

PHP study notes: scaling pictures in equal proportions

WBOY
WBOYOriginal
2016-09-19 08:55:061057browse

Go directly to the code. The function imgzip($src,$newwid,$newhei) brings in the original image, the width required for scaling, and the length of scaling. The code has been noted. If you don’t understand, you can leave a message haha

<?<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>?>

Effect

Before compression

After compression

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn