在 PHP 中合并两个带有透明度的图像
我正在尝试通过 php 制作多个具有背景透明度的 .png 的合成图像,并将生成的图像存储在我的数据库中。我的问题是,当我合并图像时,图像的透明部分被删除。
这是我创建合成图像的代码:
$base = imagecreatefrompng('application/assets/images/vel1_bg.png');
imagealphablending($base, true);
list($baseWidth, $baseHeight, $type, $attr) = getimagesize('application/assets/images/vel1_bg.png');
$user_board_items = $this->config->item('user_board_items');
foreach($array as $key => $value){
$item = imagecreatefrompng('application/assets/images/items/' . $user_board_items[$value[0]] . '.png');
imagealphablending($item, true);
list($width, $height, $type, $attr) = getimagesize('application/assets/images/items/'. $user_board_items[$value[0]] . '.png');
imagecopymerge($base,
$item,
floor(($value[1] / 100) * $baseWidth),
floor(($value[2] / 100) * $baseHeight),
0,
0,
$width,
$height,
100);
imagedestroy($item);
}
//We have to capture the output buffer
ob_start();
imagepng($base);
$baseimg = ob_get_clean();
这会生成如下图像:
我正在寻找更像这样的东西: (注意透明部分的表示方式)
I'm attempting to make a composite image of several .png's with background transparencies via php and store the resulting image in my database. My problem is that the transparent sections of my images are being dropped when I merge the images.
This is my code to create the composite image:
$base = imagecreatefrompng('application/assets/images/vel1_bg.png');
imagealphablending($base, true);
list($baseWidth, $baseHeight, $type, $attr) = getimagesize('application/assets/images/vel1_bg.png');
$user_board_items = $this->config->item('user_board_items');
foreach($array as $key => $value){
$item = imagecreatefrompng('application/assets/images/items/' . $user_board_items[$value[0]] . '.png');
imagealphablending($item, true);
list($width, $height, $type, $attr) = getimagesize('application/assets/images/items/'. $user_board_items[$value[0]] . '.png');
imagecopymerge($base,
$item,
floor(($value[1] / 100) * $baseWidth),
floor(($value[2] / 100) * $baseHeight),
0,
0,
$width,
$height,
100);
imagedestroy($item);
}
//We have to capture the output buffer
ob_start();
imagepng($base);
$baseimg = ob_get_clean();
This produces an image like this:
And I'm looking for something more like this:
(Note how the transparent sections are represented)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用 imagecopymerge() 来合并透明图像。
最好在脚本中使用 imagecopyresampled() 。
Do not use imagecopymerge() for merge transparent image.
It's better to use imagecopyresampled() in your script.
如前所述,在尝试不同的解决方案数小时后,imagecopyresampled 也对我有用。要传递的参数保持不变,只是您必须删除最后一个参数,但添加源宽度和高度。您的电话可能是:
As mentioned before, imagecopyresampled worked for me as well after hours of trying different solutions. The parameters to be passed remain the same, except you have to remove the last one, but add source width and height. Your call would probably be: