gpt4 book ai didi

php - 可以使用 GD 和 PHP 为透明图像添加背景颜色

转载 作者:可可西里 更新时间:2023-11-01 00:04:25 29 4
gpt4 key购买 nike

我有使用 GD 用 php 语言编写的缩略图创建类。我想知道当我上传 png 或 gif 透明图像时,我可以在该缩略图图像中放置背景吗?如果可能的话,请指导我怎么做。谢谢。

最佳答案

这是一个适用于 PNG 文件的解决方案:

$filePath = '';  //full path to your png, including filename and extension
$savePath = ''; //full path to saved png, including filename and extension
$colorRgb = array('red' => 255, 'green' => 0, 'blue' => 0); //background color

$img = @imagecreatefrompng($filePath);
$width = imagesx($img);
$height = imagesy($img);

//create new image and fill with background color
$backgroundImg = @imagecreatetruecolor($width, $height);
$color = imagecolorallocate($backgroundImg, $colorRgb['red'], $colorRgb['green'], $colorRgb['blue']);
imagefill($backgroundImg, 0, 0, $color);

//copy original image to background
imagecopy($backgroundImg, $img, 0, 0, 0, 0, $width, $height);

//save as png
imagepng($backgroundImg, $savePath, 0);

关于php - 可以使用 GD 和 PHP 为透明图像添加背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1496169/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com