gpt4 book ai didi

PHP-GD:保留半透明区域

转载 作者:可可西里 更新时间:2023-10-31 23:42:56 26 4
gpt4 key购买 nike

我需要为 PHP 站点上传通用图像。照片和 Logo 的大小应调整到一定范围,以确保它们不会太大并适合设计。

我正在尝试使用这段代码:

function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);

if($this->image_type == PNG or $this->image_type == GIF) {
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $nWidth, $nHeight, $transparent);
}

imagecopyresized($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}

但是,当我上传一张图像,其区域的 alpha 值介于 0 和 255 之间时,它们会被全黑取代,将抗锯齿区域变成黑色边框。

完全透明适用于 PNG 和 GIF,只是半透明区域是个问题。

如果我没有使用正确的术语来解释我的问题,我深表歉意,也许这就是为什么我几乎找不到任何东西的原因。

最佳答案

尝试:

function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);

if($this->image_type == PNG or $this->image_type == GIF) {
imagefill($new_image, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($new_image,true);
imagealphablending($new_image, true);
}

imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}

基于 this (我知道它有效)。

关于PHP-GD:保留半透明区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9545600/

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