PHP GD图像问题
我正在尝试编写一些代码,可以将图像叠加在一起并将它们输出为 1 个图像。我还可以将其中一张图像向上移动 20 像素,向下移动 10 像素。我已经完成了所有工作,并设置了我想要的方式,但唯一的问题是当我将其中一张图像向右移动时,它在左侧显示一个黑点,我如何才能使其全部透明。即使图像移动了?
<?php
$layers = array();
$shiftx = array();
$shifty = array();
$wid = array();
$hei = array();
$layers[] = imagecreatefrompng("egg.png");
$shiftx[] = "0";
$shifty[] = "0";
$wid[] = "75";
$hei[] = "75";
$layers[] = imagecreatefrompng("fedora.png");//Top Layer
$shiftx[] = "-20";
$shifty[] = "0";
$wid[] = "56";
$hei[] = "56";
$image = imagecreatetruecolor(100, 100);
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefill($image, 0, 0, $transparent);
//Will merge the layers into one image//
for ($i = 0; $i < count($layers); $i++)
{
$y=$shifty[$i];
$x=$shiftx[$i];
$w=$wid[$i]-$shiftx[$i];
$h=$hei[$i]-$shifty[$i];
imagecopy($image, $layers[$i], 0, 0, $x, $y, $w, $h);
}
//Everything is now done, except for the image output. We will do this now.//
header('Content-type: image/png');
imagealphablending($image, false);
imagesavealpha($image, true);
imagepng($image);
imagedestroy($image);
?>
I am trying to code something where I can overlay images on top of each other and have them output as 1 image. Also I have it where I can shift one of the images 20 px over and 10 px down. I have everything done, and set up how I want it, but the only issue is when I shift one of the images to the right it shows a black spot on the left, how do I get it all to be transparent. Even when the image is shifted?
<?php
$layers = array();
$shiftx = array();
$shifty = array();
$wid = array();
$hei = array();
$layers[] = imagecreatefrompng("egg.png");
$shiftx[] = "0";
$shifty[] = "0";
$wid[] = "75";
$hei[] = "75";
$layers[] = imagecreatefrompng("fedora.png");//Top Layer
$shiftx[] = "-20";
$shifty[] = "0";
$wid[] = "56";
$hei[] = "56";
$image = imagecreatetruecolor(100, 100);
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefill($image, 0, 0, $transparent);
//Will merge the layers into one image//
for ($i = 0; $i < count($layers); $i++)
{
$y=$shifty[$i];
$x=$shiftx[$i];
$w=$wid[$i]-$shiftx[$i];
$h=$hei[$i]-$shifty[$i];
imagecopy($image, $layers[$i], 0, 0, $x, $y, $w, $h);
}
//Everything is now done, except for the image output. We will do this now.//
header('Content-type: image/png');
imagealphablending($image, false);
imagesavealpha($image, true);
imagepng($image);
imagedestroy($image);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这样的事情:
这不是我的蛋糕PHP图像迷你处理类。这保留了 PNG 和 GIF 的透明度。我不建议完全复制此内容,而只是为了让您走上正确的轨道
PS:请注意 $this->image 和 $new_image 是加载的图像。
它的加载方式如下:
PSS IF you're要水印..这就是我所做的:
Try something like this:
This is off my cakePHP image minipulation class. This perserves PNG and GIF transparency. I wouldnt suggest copying this exact but just to put yu on the right track
PS: Note that $this->image and $new_image are the loaded image.
It is loaded like:
PSS IF youre going for a watermark.. this is what i did: