2014-09-03 104 views

回答

5
<?php 

$img1_path = 'images_1.png'; 
$img2_path = 'images_2.png'; 

list($img1_width, $img1_height) = getimagesize($img1_path); 
list($img2_width, $img2_height) = getimagesize($img2_path); 

$merged_width = $img1_width + $img2_width; 
//get highest 
$merged_height = $img1_height > $img2_height ? $img1_height : $img2_height; 

$merged_image = imagecreatetruecolor($merged_width, $merged_height); 

imagealphablending($merged_image, false); 
imagesavealpha($merged_image, true); 

$img1 = imagecreatefrompng($img1_path); 
$img2 = imagecreatefrompng($img2_path); 

imagecopy($merged_image, $img1, 0, 0, 0, 0, $img1_width, $img1_height); 
//place at right side of $img1 
imagecopy($merged_image, $img2, $img1_width, 0, 0, 0, $img2_width, $img2_height); 

//save file or output to broswer 
$SAVE_AS_FILE = TRUE; 
if($SAVE_AS_FILE){ 
    $save_path = "your target path"; 
    imagepng($merged_image,$save_path); 
}else{ 
    header('Content-Type: image/png'); 
    imagepng($merged_image); 
} 

//release memory 
imagedestroy($merged_image); 

?> 

嘗試

+0

真棒完美的作品芭菲!我怎樣才能將合併的圖像上傳到服務器?我試過'move_uploaded_file'和'copy',但都沒有工作。 – Graham 2014-09-03 04:44:50

+0

謝謝,但實際上不起作用。當我運行該腳本時,它顯示一個空的圖像,並且不會向該文件夾寫入任何內容。另外,我有權將該文件夾設置爲777,並使用兩個.png圖像。 – Graham 2014-09-03 05:01:53

+0

imagepng($ merged_image)的結果是什麼,是空還是拋出其他錯誤? – Parfait 2014-09-03 05:05:40