5

i have n-images and want to create one with php code. I use imagecopymerge(), but can't make it. Some example please?

kruksmail
  • 189
  • 1
  • 3
  • 10

2 Answers2

7

Code:

$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);
kruksmail
  • 189
  • 1
  • 3
  • 10
3

Thanks kruksmail,

I adapted your answer for a specific project in which the images could be unknown. So I made your answer work with an array of images.

It also gives the ability to specify how many rows or columns you want. I added some comments to help also.

$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>";
Bayo
  • 251
  • 2
  • 4
  • Thanks for this, I was able to adapt this to solve a problem I had – Dominic Williams Nov 02 '15 at 11:09
  • 2.5 years and still helpful. Thanks! FYI, if you want to crop the images together from the center instead of taking the left side edge, you can modify the `imagecopymerge()` method to this: `imagecopymerge($output_image, $image_objects[$step], ($width * $x), ($height * $y), imagesx($image_objects[$step])/2 - $width/2, 0, $width, $height, 100);` – justinl Apr 08 '17 at 04:32
  • Still helpful in 2022 ;) FYI, if you want to resize (and fit) the source images (instead of cropping them), use `imagecopyresampled` like this `imagecopyresampled($output_image, $image_objects[$step], ($width * $x), ($height * $y), 0, 0, $width, $height, imagesx($image_objects[$step]), imagesy($image_objects[$step]));` – Cédric Jun 15 '22 at 21:31