当使用GD组合时,为什么此透明PNG会引起边界? [英] Why does this transparent PNG cause borders when combined using GD?

查看:79
本文介绍了当使用GD组合时,为什么此透明PNG会引起边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP从另一个图像创建图像.这是我的代码:

I am trying to create an image from an another image using PHP. Here is my code:

<?php
    $width = 109;
    $height = 109;
    $image = imagecreatetruecolor($width, $height);
    $source_under = imagecreatefrompng('ecloid_under.png');
    $black = imagecolorallocate($image, 0x00, 0x00, 0x00);

    imagecolortransparent($image, $black);

    imagecopy($image, $source_under, 0, 0, 0, 0, $width, $height);

    header('Content-type: image/png');
    imagepng($image);
    imagedestroy($image);
?>

所以我正在将此图像加载到$source_under

So I am loading this image in $source_under

并将其复制到透明的空白画布"图像上.这是该操作的结果:

and copying it over a transparent blank "canvas" image. Here is the result of that operation:

可以看出,整个初始图像周围都有一种黑色边框.我认为这是由于以下事实:画布"图像最初是全黑的.因此,图像的透明度和抗锯齿是有问题的.

As can be seen, there is a sort of black border around the whole initial image. I think this is due to the fact that initially, the "canvas" image is all black. So there is something wrong with the transparency and the anti-aliasing of the image.

这不是我第一次遇到类似的问题,但最后一次原因是源图像.这次,在Photoshop中打开它并没有显示任何潜在的问题.

This isn't the first time I have a similar problem, but last time the source image was the cause. This time around, opening it in Photoshop does not show any potential problems with it.

有人知道如何解决此问题吗?

Does anyone know how to fix this?

推荐答案

在将原始图像复制到$ image之前,是否可以尝试在其上启用alpha混合:

Can you try to enable alpha blending on $image before you copy the original to it:

imagealphablending($image, true); 

第二种尝试是创建一种透明颜色,并在复制之前用该颜色填充$ image.

Second try would be to create a transparent color and to fill $image with that color before the copy.

$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparent);
imagealphablending($image, true); 

这篇关于当使用GD组合时,为什么此透明PNG会引起边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆