合并多个图像会产生损坏的图像

时间:2014-06-08 19:35:36

标签: php 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);

?>

关于如何解决这个问题的任何线索?

1 个答案:

答案 0 :(得分:0)

我最初的想法是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/