PHP-GD:保留半透明区域

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP-GD:保留半透明区域脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要 PHP站点的通用图像上传.照片和徽标应该在一定程度上重新调整大小,以确保它们不会太大并且符合设计.

我正在尝试使用此代码

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,127);
        imagefilledrectangle($new_image,$nWidth,$nHeight,$transparent);
    }

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

但是,当我上传的图像区域的alpha值介于0到255之间时,它们会被完整的黑色替换,将抗锯齿区域变为黑色边框.

完全透明度适用于PNG和GIF,只有透明区域是个问题.

如果我没有使用正确的语来解释我的问题,我很抱歉,也许这就是为什么我几乎找不到任何东西.

@H_512_18@解决方法
尝试:

function resize($width,$height);

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

    imagecopyresampled($new_image,$this->getHeight());
    $this->image = $new_image;
}

基于this(我知道它的工作原理).

脚本宝典总结

以上是脚本宝典为你收集整理的PHP-GD:保留半透明区域全部内容,希望文章能够帮你解决PHP-GD:保留半透明区域所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。