2013-08-29 33 views
0

我想將幾個圖像合併成一個。下面列出了這個數組。合併幾個圖像與imagecopyresized

$ products_settings - 數組:數組$products_settings是(在這種情況下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'); 

} 
?> 

只有最後一個圖像:

(
[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 
    ) 

) 

我已經試過這合併成dummy.jpg文件,但我想將所有三種產品合併到dummy.jpg文件中。我怎麼能做到這一點?請給我一些指點!

+0

那麼,它首先考慮什麼屬於循環之前,什麼是循環以及循環之後的部分。現在你只有在循環中 - 你已經意識到 - 是適得其反的。 – hakre

+0

@hakre - 我提供了數組(它是products_settings數組)。圖像資源被正確創建,左側,頂部,寬度,高度被創建並正確地合併到dummy.jpg中 - 它與一個數組項目一起工作。 – bestprogrammerintheworld

回答

1

您不斷在每個循環上重新創建目標圖像......在循環開始之前將其放置。

然後在所有循環完成後輸出圖像。

+0

非常感謝!我已經明白了! :-) – bestprogrammerintheworld