首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP-GD:保留半透明区域

PHP-GD:保留半透明区域
EN

Stack Overflow用户
提问于 2012-03-03 19:08:20
回答 1查看 307关注 0票数 4

我需要一个PHP网站的通用图像上传。照片和徽标应该在一定程度上重新调整大小,以确保它们不会太大,并且适合设计。

我试着用下面的代码:

代码语言:javascript
复制
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来说工作得很好,只是半透明区域是个问题。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-03 17:22:16

尝试:

代码语言:javascript
复制
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 (我知道它是有效的)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9545600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档