2014-09-03 12 views
2

2 개의 이미지를 하나씩 (겹치지 않고) 나란히 놓고 싶습니다. 내가하고 싶은 것의 예는 전후 그림 같은 것입니다 (두 이미지는 모두 같은 너비의 너비를가집니다). PHP GD 이미지 라이브러리로 수행하는 방법에 대한 예제 코드는 크게 감사하겠습니다. 감사!PHP GD 라이브러리, 2 개의 이미지를 나란히 나란히 넣기

답변

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

+0

고마워요.하지만 실제로 작동하지 않습니다. 스크립트를 실행하면 빈 이미지가 표시되고 폴더에는 아무 것도 쓰지 않습니다. 또한 777로 설정된 폴더에 대한 사용 권한이 있고 두 .png 이미지를 사용하고 있습니다. – Graham

+0

imagepng ($ merged_image)의 결과는 비어 있습니까? 아니면 다른 오류가 발생합니까? – Parfait

관련 문제