在PHP中使用透明胶片合并两个图像 [英] Merge two images with transparencies in PHP

查看:90
本文介绍了在PHP中使用透明胶片合并两个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过php制作具有背景透明性的几个.png的合成图像,并将结果图像存储在我的数据库中.我的问题是,合并图像时,图像的透明部分会被丢弃.

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)

推荐答案

请勿使用imagecopymerge()合并透明图像.

Do not use imagecopymerge() for merge transparent image.

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

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

这篇关于在PHP中使用透明胶片合并两个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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