2012-05-01 4 views
3

나는 이미지가 있고 PHP 코드로 이미지를 만들고 싶습니다. imagecopymerge()를 사용하지만 만들 수 없습니다. 몇 가지 예를 들어 주시겠습니까?PHP - 이미지에서 하나의 이미지 만들기

+6

시도해 보신 것을 게시하십시오. – Nadh

+0

여기를 참조하십시오 http://stackoverflow.com/questions/1394061/how-to-merge-transparent-png-with-image-using-php –

+0

이미지를 복사하고 크기를 조정하려면 튜토리얼 http를 따르십시오. : //blog.webtech11.com/2012/04/21/upload-and-resize-an-image-with-php.html –

답변

8

코드 : 나는 이미지를 알 수있는 특정 프로젝트에 대한 답변을 채택

$numberOfImages = 3; 
$x = 940; 
$y = 420; 
$background = imagecreatetruecolor($x, $y*3); 


$firstUrl = '/images/upload/photoalbum/photo/1.jpg'; 

$secondUrl = '/images/upload/photoalbum/photo/2.jpg'; 

$thirdUrl = '/images/upload/photoalbum/photo/3.jpg'; 

$outputImage = $background; 

$first = imagecreatefromjpeg($firstUrl); 
$second = imagecreatefromjpeg($secondUrl); 
$third = imagecreatefromjpeg($thirdUrl); 



imagecopymerge($outputImage,$first,0,0,0,0, $x, $y,100); 
imagecopymerge($outputImage,$second,0,$y,0,0, $x, $y,100); 
imagecopymerge($outputImage,$third,0,$y*2,0,0, $x, $y,100); 

imagejpeg($outputImage, APPLICATION_PATH .'/images/upload/photoalbum/photo/test.jpg'); 

imagedestroy($outputImage); 
2

덕분에 kruksmail. 그래서 저는 여러분의 답을 일련의 이미지들로 작업하게 만들었습니다.

또한 원하는 행이나 열 수를 지정할 수 있습니다. 나는 또한 도움이되는 몇 가지 코멘트를 추가했다.

$images = array('/images/upload/photoalbum/photo/1.jpg','/images/upload/photoalbum/photo/2.jpg','/images/upload/photoalbum/photo/3.jpg'); 
$number_of_images = count($images); 

$priority = "columns"; // also "rows" 

if($priority == "rows"){ 
    $rows = 3; 
    $columns = $number_of_images/$rows; 
    $columns = (int) $columns; // typecast to int. and makes sure grid is even 
}else if($priority == "columns"){ 
    $columns = 3; 
    $rows = $number_of_images/$columns; 
    $rows = (int) $rows; // typecast to int. and makes sure grid is even 
} 
$width = 150; // image width 
$height = 150; // image height 

$background = imagecreatetruecolor(($width*$columns), ($height*$rows)); // setting canvas size 
$output_image = $background; 

// Creating image objects 
$image_objects = array(); 
for($i = 0; $i < ($rows * $columns); $i++){ 
    $image_objects[$i] = imagecreatefromjpeg($images[$i]); 
} 

// Merge Images 
$step = 0; 
for($x = 0; $x < $columns; $x++){ 
    for($y = 0; $y < $rows; $y++){ 
    imagecopymerge($output_image, $image_objects[$step], ($width * $x), ($height * $y), 0, 0, $width, $height, 100); 
    $step++; // steps through the $image_objects array 
    } 
} 

imagejpeg($output_image, 'test.jpg'); 
imagedestroy($output_image); 

print "<div><img src='test.jpg' /></div>"; 
+0

감사합니다.이 문제를 해결하기 위해이 문제를 해결할 수있었습니다. –

+0

2.5 년 여전히 도움이됩니다. 감사! 참고로 왼쪽 가장자리를 찍는 대신 중앙에서 이미지를 자르려면'imagecopymerge()'메소드를 다음과 같이 수정할 수 있습니다 :'imagecopymerge ($ output_image, $ image_objects [$ step], (width * $ x), ($ height * $ y), imagesx ($ image_objects [$ step])/2 - $ width/2, 0, $ width, $ height, 100); – justinl

관련 문제