【问题标题】:merging several images with imagecopyresized将多个图像与 imagecopyresized 合并
【发布时间】:2013-08-29 20:10:12
【问题描述】:

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

$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 中。我怎么能做到这一点?请给我一些指点!

【问题讨论】:

  • 嗯,首先要考虑循环之前的内容,循环中的内容以及循环之后的部分。现在你只有在循环中——正如你已经意识到的那样——会适得其反。
  • @hakre - 我已经提供了数组(它是 products_settings 数组)。图像资源被正确创建,left、top、width、height 被正确创建并合并到 dummy.jpg - 它适用于一个数组项。

标签: php image merge


【解决方案1】:

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

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

【讨论】:

  • 非常感谢!我现在想通了! :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 2018-12-21
  • 2010-10-17
相关资源
最近更新 更多