2012-05-01 181 views
3

我有n圖像,並且想用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