如何使用GD库将多个图像合并在一起?

时间:2013-12-11 10:27:38

标签: php image gd php-gd

我已经删除了原来的问题,因为我已经设法部分使这个工作,我有7张图片,

https://dl.dropboxusercontent.com/u/58586640/layer1.png https://dl.dropboxusercontent.com/u/58586640/layer2.png https://dl.dropboxusercontent.com/u/58586640/layer3.png https://dl.dropboxusercontent.com/u/58586640/layer4.png https://dl.dropboxusercontent.com/u/58586640/layer5.png https://dl.dropboxusercontent.com/u/58586640/layer6.png https://dl.dropboxusercontent.com/u/58586640/layer7.png

当这些图像放在彼此的顶部时,每个图像应创建一个完美地插在一起的图像。

我目前的结果是,

https://dl.dropboxusercontent.com/u/58586640/Screen%20Shot%202013-12-11%20at%2018.31.57.png

正如您所看到的,它几乎完美无缺,但其中2个图层未被复制或未正确保存alpha。我不确定我做错了什么,因为所有其他层似乎工作正常,黑色的图像是,

https://dl.dropboxusercontent.com/u/58586640/layer3.png https://dl.dropboxusercontent.com/u/58586640/layer4.png

这是我目前的代码,

    <?php

$images = array('img/layer1.png', 'img/layer2.png', 'img/layer3.png', 'img/layer4.png', 'img/layer5.png', 'img/layer6.png', 'img/layer7.png');

// foreach($images as $i) {
//  var_dump(file_exists($i));
// }

// Allocate new image

$img = imagecreatetruecolor(704, 469);

// Make alpha channels work

imagealphablending($img, true);
imagesavealpha($img, true);

foreach($images as $fn) {

    // Load image
    $cur = imagecreatefrompng($fn);
    imagealphablending($cur, true);
    imagesavealpha($cur, true);

    // Copy over image
    imagecopy($img, $cur, 0,0,0,0, 704, 469);

    // Free memory
    imagedestroy($cur);

}

header('Content-Type: image/png');
imagepng($img);

正如你所看到我已经检查过以确保文件存在,并且他们这样做它必须是GD问题,任何人都有想法?

1 个答案:

答案 0 :(得分:0)

有趣的是我昨天完成了这个,这是我如何使用静态背景图层和组合之前生成的QR码。

$template = imagecreatefrompng("./images/asset_barcode_template.png");
//QR created fine assemble final image
$qrimage = imagecreatefrompng($this->qrfilename);
//final image assembled just fine
imagecopy($template, $qrimage, 230, 6, 0, 0, 106, 106);
imagepng($template,$filename,0,PNG_NO_FILTER);
相关问题