Home Backend Development PHP Tutorial How to generate transparent png image thumbnails in php

How to generate transparent png image thumbnails in php

Aug 08, 2016 am 09:33 AM
gt height resize this width

Note: This function depends on the GD2 graphics library

I recently wanted to use PHP to generate thumbnails. I searched online and found this article: Generate image thumbnails with PHP

After trying it out, I found the following problems:

1. The thumbnails generated from png images are in jpg format.

2. The thumbnail generated from the png image has no transparent (semi-transparent) effect (filled with black background)

3. The code syntax is relatively old

Therefore, we simply modified and optimized it based on this version.

PHP generate thumbnail class

&lt;?<span>php </span><span>/*</span><span> * desc: Resize Image(png, jpg, gif)
     * author: 十年后的卢哥哥(http://www.cnblogs.com/lurenjiashuo/)
     * date: 2014.11.13
     * base from: http://www.oschina.net/code/snippet_5189_2491 </span><span>*/</span><span>class</span><span> ResizeImage { </span><span>//</span><span>图片类型</span><span>private</span><span>$type</span><span>; </span><span>//</span><span>实际宽度</span><span>private</span><span>$width</span><span>; </span><span>//</span><span>实际高度</span><span>private</span><span>$height</span><span>; </span><span>//</span><span>改变后的宽度</span><span>private</span><span>$resize_width</span><span>; </span><span>//</span><span>改变后的高度</span><span>private</span><span>$resize_height</span><span>; </span><span>//</span><span>是否裁图</span><span>private</span><span>$cut</span><span>; </span><span>//</span><span>源图象</span><span>private</span><span>$srcimg</span><span>; </span><span>//</span><span>目标图象地址</span><span>private</span><span>$dstimg</span><span>; </span><span>//</span><span>临时创建的图象</span><span>private</span><span>$im</span><span>; </span><span>function</span> __construct(<span>$imgPath</span>, <span>$width</span>, <span>$height</span>, <span>$isCut</span>, <span>$savePath</span><span>) { </span><span>$this</span>-&gt;srcimg = <span>$imgPath</span><span>; </span><span>$this</span>-&gt;resize_width = <span>$width</span><span>; </span><span>$this</span>-&gt;resize_height = <span>$height</span><span>; </span><span>$this</span>-&gt;cut = <span>$isCut</span><span>; </span><span>//</span><span>图片的类型</span><span>$this</span>-&gt;type = <span>strtolower</span>(<span>substr</span>(<span>strrchr</span>(<span>$this</span>-&gt;srcimg,"."),1<span>)); </span><span>//</span><span>初始化图象</span><span>$this</span>-&gt;<span>initi_img(); </span><span>//</span><span>目标图象地址</span><span>$this</span> -&gt; dst_img(<span>$savePath</span><span>); </span><span>//</span><span>--</span><span>$this</span>-&gt;width = imagesx(<span>$this</span>-&gt;<span>im); </span><span>$this</span>-&gt;height = imagesy(<span>$this</span>-&gt;<span>im); </span><span>//</span><span>生成图象</span><span>$this</span>-&gt;<span>newimg();
            ImageDestroy (</span><span>$this</span>-&gt;<span>im);
        } </span><span>private</span><span>function</span><span> newimg() { </span><span>//</span><span>改变后的图象的比例</span><span>$resize_ratio</span> = (<span>$this</span>-&gt;resize_width)/(<span>$this</span>-&gt;<span>resize_height); </span><span>//</span><span>实际图象的比例</span><span>$ratio</span> = (<span>$this</span>-&gt;width)/(<span>$this</span>-&gt;<span>height); </span><span>if</span>(<span>$this</span>-&gt;<span>cut) { </span><span>//</span><span>裁图</span><span>$newimg</span> = imagecreatetruecolor(<span>$this</span>-&gt;resize_width,<span>$this</span>-&gt;<span>resize_height); </span><span>if</span>(<span>$this</span>-&gt;type=="png"<span>) {
                    imagefill(</span><span>$newimg</span>, 0, 0, imagecolorallocatealpha(<span>$newimg</span>, 0, 0, 0, 127<span>));
                } </span><span>if</span>(<span>$ratio</span>&gt;=<span>$resize_ratio</span><span>) { </span><span>//</span><span>高度优先</span> imagecopyresampled(<span>$newimg</span>, <span>$this</span>-&gt;im, 0, 0, 0, 0, <span>$this</span>-&gt;resize_width,<span>$this</span>-&gt;resize_height, ((<span>$this</span>-&gt;height)*<span>$resize_ratio</span>), <span>$this</span>-&gt;<span>height);
                } </span><span>else</span><span> { </span><span>//</span><span>宽度优先</span> imagecopyresampled(<span>$newimg</span>, <span>$this</span>-&gt;im, 0, 0, 0, 0, <span>$this</span>-&gt;resize_width, <span>$this</span>-&gt;resize_height, <span>$this</span>-&gt;width, ((<span>$this</span>-&gt;width)/<span>$resize_ratio</span><span>));
                }
            } </span><span>else</span><span> { </span><span>//</span><span>不裁图</span><span>if</span>(<span>$ratio</span>&gt;=<span>$resize_ratio</span><span>) { </span><span>$newimg</span> = imagecreatetruecolor(<span>$this</span>-&gt;resize_width,(<span>$this</span>-&gt;resize_width)/<span>$ratio</span><span>); </span><span>if</span>(<span>$this</span>-&gt;type=="png"<span>) {
                        imagefill(</span><span>$newimg</span>, 0, 0, imagecolorallocatealpha(<span>$newimg</span>, 0, 0, 0, 127<span>));
                    }
                    imagecopyresampled(</span><span>$newimg</span>, <span>$this</span>-&gt;im, 0, 0, 0, 0, <span>$this</span>-&gt;resize_width, (<span>$this</span>-&gt;resize_width)/<span>$ratio</span>, <span>$this</span>-&gt;width, <span>$this</span>-&gt;<span>height);
                } </span><span>else</span><span> { </span><span>$newimg</span> = imagecreatetruecolor((<span>$this</span>-&gt;resize_height)*<span>$ratio</span>,<span>$this</span>-&gt;<span>resize_height); </span><span>if</span>(<span>$this</span>-&gt;type=="png"<span>) {
                        imagefill(</span><span>$newimg</span>, 0, 0, imagecolorallocatealpha(<span>$newimg</span>, 0, 0, 0, 127<span>));
                    }
                    imagecopyresampled(</span><span>$newimg</span>, <span>$this</span>-&gt;im, 0, 0, 0, 0, (<span>$this</span>-&gt;resize_height)*<span>$ratio</span>, <span>$this</span>-&gt;resize_height, <span>$this</span>-&gt;width, <span>$this</span>-&gt;<span>height);
                }
            } </span><span>if</span>(<span>$this</span>-&gt;type=="png"<span>) {
                imagesavealpha(</span><span>$newimg</span>, <span>true</span><span>);
                imagepng (</span><span>$newimg</span>,<span>$this</span>-&gt;<span>dstimg);
            } </span><span>else</span><span> {
                imagejpeg (</span><span>$newimg</span>,<span>$this</span>-&gt;<span>dstimg);
            }
        } </span><span>//</span><span>初始化图象</span><span>private</span><span>function</span><span> initi_img() { </span><span>if</span>(<span>$this</span>-&gt;type=="jpg"<span>) { </span><span>$this</span>-&gt;im = imagecreatefromjpeg(<span>$this</span>-&gt;<span>srcimg);
            } </span><span>if</span>(<span>$this</span>-&gt;type=="gif"<span>) { </span><span>$this</span>-&gt;im = imagecreatefromgif(<span>$this</span>-&gt;<span>srcimg);
            } </span><span>if</span>(<span>$this</span>-&gt;type=="png"<span>) { </span><span>$this</span>-&gt;im = imagecreatefrompng(<span>$this</span>-&gt;<span>srcimg);
            }
        } </span><span>//</span><span>图象目标地址</span><span>private</span><span>function</span> dst_img(<span>$dstpath</span><span>) { </span><span>$full_length</span> = <span>strlen</span>(<span>$this</span>-&gt;<span>srcimg); </span><span>$type_length</span> = <span>strlen</span>(<span>$this</span>-&gt;<span>type); </span><span>$name_length</span> = <span>$full_length</span>-<span>$type_length</span><span>; </span><span>$name</span> = <span>substr</span>(<span>$this</span>-&gt;srcimg,0,<span>$name_length</span>-1<span>); </span><span>$this</span>-&gt;dstimg = <span>$dstpath</span><span>;
        }
    } </span>?&gt;
Copy after login

use

When using it, just call the constructor of the class directly. The constructor is as follows:

<span>$resizeimage</span> = <span>new</span> resizeimage($imgPath, $width, $height, $isCut, $savePath);
Copy after login

parameters

$imgPath: original image address

$width: thumbnail width

$height: Thumbnail height

$isCut: Whether to crop, bool value

$savePath: thumbnail address (can be the same as the original image address)

Example

&lt;?<span>php </span><span>include</span> "ResizeImage.php"<span>; </span><span>//</span><span>jpg</span><span>$jpgResize</span> = <span>new</span> ResizeImage("img/test_1920_1200.jpg", 320, 240, <span>false</span>, "img/test_320_240.jpg"<span>); </span><span>//</span><span>png</span><span>$pngResize</span> = <span>new</span> ResizeImage("img/test_1024_746.png", 320, 240, <span>false</span>, "img/test_320_240.png"<span>); </span>?&gt;
Copy after login

Effect

The above introduces how PHP generates transparent png image thumbnails, including the content of generating thumbnails in PHP. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

What are the differences between Huawei GT3 Pro and GT4?

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Fix: Snipping tool not working in Windows 11

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

How to Fix Can't Connect to App Store Error on iPhone

What does the width of html mean? What does the width of html mean? Jun 03, 2021 pm 02:15 PM

What does the width of html mean?

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

How to compress and format images in Vue? How to compress and format images in Vue? Aug 25, 2023 pm 11:06 PM

How to compress and format images in Vue?

How to resize, crop, rotate, and flip images in Python How to resize, crop, rotate, and flip images in Python May 10, 2023 am 10:43 AM

How to resize, crop, rotate, and flip images in Python

Detailed explanation of CSS dimension properties: height and width Detailed explanation of CSS dimension properties: height and width Oct 21, 2023 pm 12:42 PM

Detailed explanation of CSS dimension properties: height and width

See all articles