在 PHP 中合并两个带有透明度的图像

发布于 2024-11-16 15:47:57 字数 1387 浏览 6 评论 0原文

我正在尝试通过 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:
enter image description here

And I'm looking for something more like this:
enter image description here
(Note how the transparent sections are represented)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栖竹 2024-11-23 15:47:57

不要使用 imagecopymerge() 来合并透明图像。

最好在脚本中使用 imagecopyresampled() 。

Do not use imagecopymerge() for merge transparent image.

It's better to use imagecopyresampled() in your script.

嘴硬脾气大 2024-11-23 15:47:57

如前所述,在尝试不同的解决方案数小时后,imagecopyresampled 也对我有用。要传递的参数保持不变,只是您必须删除最后一个参数,但添加源宽度和高度。您的电话可能是:

  imagecopyresampled($base,
                $item,
                floor(($value[1] / 100) * $baseWidth),
                floor(($value[2] / 100) * $baseHeight),
                0,
                0,
                $width,
                $height,
                $width,
                $height);

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:

  imagecopyresampled($base,
                $item,
                floor(($value[1] / 100) * $baseWidth),
                floor(($value[2] / 100) * $baseHeight),
                0,
                0,
                $width,
                $height,
                $width,
                $height);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文