【问题标题】:PHP-GD: preserve half-transparent areasPHP-GD:保留半透明区域
【发布时间】:2012-03-03 11:08:19
【问题描述】:

我需要一个 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, 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,只是半透明区域是个问题。

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

【问题讨论】:

  • 无法重现,问题可能出在其他地方,例如,如果图像是PNG,是否使用了imagecreatefrompng?可以将imagefilledrectangle替换为imagefill,将imagecopyresized替换为imagecopyresampled,也可以加上imagealphablending($this->image, true);...

标签: php transparency php-gd


【解决方案1】:

试试:

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(我知道它有效)。

【讨论】:

    猜你喜欢
    • 2011-08-20
    • 1970-01-01
    • 2017-03-17
    • 2013-02-21
    • 2013-10-24
    • 2011-04-25
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多