使用imagecopy调整大小合并多个图像


merging several images with imagecopyresized

我想将多个图像合并为一个。我下面有这个阵列。

$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中的最后一个图像(在本例中为whateverfile3.jpg)被合并到文件dummy.jpg中,但我想将所有三个产品合并到文件dummy.jpg.如何实现这一点?请给我一些建议!

在每个循环中不断重新创建目标图像。。。把它放在循环开始之前。

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