Home > Backend Development > PHP Tutorial > How to create transparent PNG images in PHP

How to create transparent PNG images in PHP

小云云
Release: 2023-03-21 17:22:02
Original
2992 people have browsed it

This article mainly shares with you the method of creating transparent PNG images in PHP. I hope it can help you.

After verification, the code is as follows:

<?php
/*  *$sourePic:原图路径  * $smallFileName:小图名称  * $width:小图宽  * $heigh:小图高  */
function pngthumb($sourePic, $smallFileName, $width, $heigh){
    $image = imagecreatefrompng($sourePic);//PNG
    imagesavealpha($image,true);//这里很重要 意思是不要丢了$sourePic图像的透明色;
    $BigWidth=imagesx($image);//大图宽度
    $BigHeigh=imagesy($image);//大图高度
    $thumb = imagecreatetruecolor($width,$heigh);
    imagealphablending($thumb,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
    imagesavealpha($thumb,true);//这里很重要,意思是不要丢了$thumb图像的透明色;
    if(imagecopyresampled($thumb,$image,0,0,0,0,$width,$heigh,$BigWidth,$BigHeigh)){
        imagepng($thumb,$smallFileName);
    }
    return $smallFileName;//返回小图路径
}
pngthumb("001.png", "test2.png", 300, 300);//调用
echo &#39;<p style="background-color: red;"><img src="test2.png"></p>&#39;;
?>
Copy after login

Related recommendations:

PHP implements support for transparent png image scaling

php Example code for detecting whether png images are complete

png image php Code for generating text png images

The above is the detailed content of How to create transparent PNG images in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template