使用imagecopyresized合并多个图像

时间:2013-08-29 20:10:13

标签: php image merge

我想将几张图片合并为一张。我在下面有这个数组。

$ products_settings - 数组:

(
[0] => Array
    (            
        [product_left] => 368
        [product_top] => 317
        [product_width] => 67.0
        [product_height] => 85.0
        [product_file] => /whateverfile1.jpg
    )

[1] => Array
    (
        [product_left] => 569
        [product_top] => 459
        [product_width] => 67.0
        [product_height] => 85.0
        [product_file] => /whateverfile2.jpg
    )

[2] => Array
    (
        [product_left] => 710
        [product_top] => 359
        [product_width] => 67.0
        [product_height] => 85.0
        [product_file] => /whateverfile3.jpg
    )

)

我试过这个:

<?php
header('Content-Type: image/jpeg');

foreach($products_settings as &$ps) {

    //Original-product/image-file
    $filename = $ps['product_file'];
    list($source_width, $source_height) = getimagesize($filename);
    $source_image = imagecreatefromjpeg($filename);

    //Destination image (as large as outfit-area-canvas)
    $dest_image = imagecreatetruecolor(intval($canvas_settings['width_canvas']), intval($canvas_settings['height_canvas']));
    $dest_width = intval($ps['product_width']);
    $dest_height = intval($ps['product_height']);
    $dest_x = intval($ps['product_left']);
    $dest_y = intval($ps['product_top']);

    //Resize source-image to new width and height and then copy from source to destination              
    imagecopyresized($dest_image, $source_image, $dest_x, $dest_y, 0, 0, $dest_width, $dest_height, $source_width, $source_height);

    imagejpeg($dest_image, 'dummy.jpg');

}
?>

只有数组$products_settings中的最后一个图像(在本例中为,无论是文件3.jpg )合并到文件dummy.jpg中,但我想将所有三个产品合并到文件虚拟中。 JPG。我怎么能实现这一目标?请给我一些指示!

1 个答案:

答案 0 :(得分:1)

您不断在每个循环上重新创建目标图像......在循环开始之前将其放置。

然后在所有循环完成后输出图像。

相关问题