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


How to merge multiple images together using GD library?

我已经删除了我原来的问题,因为我已经设法部分完成了这项工作,我有7个图像,

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

当这些图像中的每一个放在一起时,都应该创建一个完美地融合在一起的图像。

我目前的结果是,

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

正如你所看到的,它几乎完美地工作,但其中2层要么没有被复制,要么没有正确保存字母。我不确定我做错了什么,因为所有其他层似乎都工作得很好,以黑色显示的图像是

https://dl.dropboxusercontent.com/u/58586640/layer3.pnghttps://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问题,有人有想法吗?

有趣的是,我昨天确实做到了这一点,下面是我如何使用静态背景层和在组合之前生成的二维码来完成的。

$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);