合并多个图像会创建破碎的图像


Merging multiple images creates broken image

所以我试图创建一个名称数组,取与该名称相关的图像,并将它们合并在一起。下面的代码似乎只是返回一个破碎的图像。

<?php
header('Content-Type: image/png');
$startLink = file_get_contents("http://websiteforimage.com/1/img.png");
$start = imagecreatefromstring($startLink);
$name = array("2", "3", "4", "5", "6");
foreach($name as $value){
    $link = "http://websiteforimage.com/" . $value . "/img.png";
    $img = imagecreatefrompng($link);
    $offset = imagesx($start) + imagesx($img);
    imagecopymerge($start, $img, $offset, 0, 64, 64, 64, 64, 100);
}
imagepng($start);
?>

有什么线索可以解决这个问题吗?

我最初的想法是imagecopymerge()函数。你没有明确说明如何合并它们但我假设是从左到右的?

在这种情况下,你的坐标似乎是错误的

$start  - sure, that's the first image
$img    - yes, that's the new image to attach
$offset - makes sense, that's where the new image is going to start on destination
0       - makes sense too, as we are starting at the very top
64      - makes no sense to me as you are saying the you want to start 64 pixels in on the new image
64      - makes no sense again as you want to start 64 pixels down on your source image
64      - I assume the source is 64 pixels wide
64      - ...and 64 pixels high?

所以假设你真的想这么做,那么一切看起来都很好。我唯一的另一个问题是关于函数本身。我一直认为imagecopymerge只会在您想要放置源文件的位置位于目标文件上时才会起作用。例如,如果目标是100像素宽,100像素高,你想把一个图像从左边99像素和从顶部99像素,那么只有1像素可以玩,它不会神奇地增加目标的尺寸。

所以考虑到这一点,我很确定你的第一个图像需要足够大的提前采取其他图像,否则他们根本不会出现,如果他们的X,Y坐标大于目的地的尺寸。不要引用我的话,这只是思考的食物!

好运。

哦,跟进-你可能想看看这个作为指南?它似乎在做同样的事情http://diceattack.wordpress.com/2011/01/03/combining-multiple-images-using-php-and-gd/