【发布时间】:2012-06-20 18:33:11
【问题描述】:
我想在 png 图像中创建一个透明区域,某种“洞”。所以我可以把这张图片放在一些背景图片上,然后通过那个“洞”看到背景的片段。我在某个论坛上找到了这段代码:
$imgPath = 'before.png';
$img = imagecreatefrompng($imgPath); // load the image
list($width,$height) = getimagesize($imgPath); // get its size
$c = imagecolortransparent($img,imagecolorallocate($img,255,1,254)); // create transparent color, (255,1,254) is a color that won't likely occur in your image
$border = 10;
imagefilledrectangle($img, $border, $border, $width-$border, $height-$border, $c); // draw transparent box
imagepng($img,'after.png'); // save
它适用于在 png 图像中创建透明区域(在本例中为矩形)。但是当我将此 png 图像放在其他图像之上时,该区域会失去透明度,因此我最终会在结果图像的中间看到彩色矩形。有人可以帮帮我吗?
【问题讨论】:
-
你的意思是你得到的图像有透明孔?如果是,那可能是浏览器的问题..您使用的是哪一个?
-
不,我的意思是我获得的图像包含 rgb(255,1,254) 填充矩形而不是透明矩形区域。无论如何,我刚刚找到了这个问题的答案。我正在使用 imagecopy() 将 png 复制到背景上。并且仅使用 imagecopymerge() 复制透明度。应该更仔细地阅读文档。
-
好!玩透明胶片:D