I am trying to write some code that will take 3 random images and combine them into 1 jpeg with all images side by side.

The code I have so far successfully chooses the 3 images, then imports them as image resources. However, I'm stuck at this point on where to continue. I've tried using imagecopymerge() but without success.

Here's my current code. Please give me some tips.

<? 
header ("Content-type: image/jpeg");

//number of images in library, 1 to X
$numbers = range(1, 3);

//shuffle the array
shuffle($numbers);

//get 3 images using previous array as random order source
for($i=0;$i<count($numbers);$i++){
	$imgsrc[$i] = 'images2/' . $numbers[$i] . '.jpg';
}

for($i=0;$i<count($imgsrc);$i++){
	//input each 3 images
	$img[$i] = imagecreatefromjpeg($imgsrc[$i]);
}

$size = getimagesize("$img[1]");
       $src_height = $size[1];
       $src_width = $size[0];

//imagecopymerge(dest   , src    , dest_x          , dest_y          , src_x           , src_y            , src_width, src_height, quality)
imagecopymerge($img[0], $img[1], imagesx($img[0]), imagesy($img[0]), imagesx($img[1]), imagesy($img[1]), $src_width, $src_height, 100);

//imagejpeg($img[0],'', '100');

imagedestroy($img[0]);

?>

    This should work:

    header ("Content-type: image/jpeg");
    $src_dir = './images2/'; 
    $numbers = range(1, 3); 
    shuffle($numbers); 
    $dest_w = 0;
    foreach ($numbers as $key => $val){
        $src = $src_dir . $val . '.jpg'; 
        $size = getimagesize($src); 
        $src_gds[$key]['img'] = imagecreatefromjpeg($src);
        $src_gds[$key]['w'] = $size[0]; 
        $src_gds[$key]['h'] = $size[1];
        $dest_w += $src_gds[$key]['w'];
        $hts[] = $src_gds[$key]['h']; 
    } 
    $dest_h = max($hts);
    
    $dest_gd = imagecreatetruecolor($dest_w, $dest_h);
    
    $dest_x = 0;
    foreach ($src_gds as $gd){
        imagecopymerge($dest_gd, $gd['img'], $dest_x, 0, 
                       0, 0, $gd['w'], $gd['h'], 99);
        $dest_x += $gd['w'];
        imagedestroy($gd['img']); 
    } 
    
    imagejpeg($dest_gd, '', 90); 
    imagedestroy($dest_gd);

      Awesome. I plugged it in and it worked perfectly.

      Thanks for the code. I've been studying it to understand more about image functions to see where I was going wrong.

      Thanks!

        Further, I've commented the code and expanded it a bit.

        Now you can specify the number of images in your library to choose from, and also the number images you want displayed from the library.

        <? 
        header ("Content-type: image/jpeg");
        
        //number of images to display. Take the first X number of images from the array
        $img_num = 3;
        
        //directory of stored images.
        $src_dir = './images2/'; 
        
        //total number of images in library, 1 to X
        $numbers = range(1, 21); 
        
        //randomize numbers
        shuffle($numbers); 
        
        //only use the first X images from the array.
        $numbers = array_slice($numbers, 0, $img_num);
        
        $dest_w = 0;
        foreach ($numbers as $key => $val){
        //create the string of where the image is stored
            $src = $src_dir . $val . '.jpg'; 
        //get image information(size and dimensions)
            $size = getimagesize($src); 
        //create an image resource based on that jpeg
            $src_gds[$key]['img'] = imagecreatefromjpeg($src);
        
        $src_gds[$key]['w'] = $size[0]; 
        $src_gds[$key]['h'] = $size[1];
        $dest_w += $src_gds[$key]['w'];
        $hts[] = $src_gds[$key]['h']; 
        } 
        $dest_h = max($hts);
        
        //create a new background template of the total size of the combined images
        $dest_gd = imagecreatetruecolor($dest_w, $dest_h);
        
        $dest_x = 0;
        foreach ($src_gds as $gd){
            imagecopymerge($dest_gd, $gd['img'], $dest_x, 0, 0, 0, $gd['w'], $gd['h'], 99);
            $dest_x += $gd['w'];
            imagedestroy($gd['img']); 
        } 
        
        imagejpeg($dest_gd, '', 90); 
        imagedestroy($dest_gd);
        
        
        
        ?>
        

        Good job--it's great to see someone adapt my code, rather than tying to use exactly as-is, and even better to know it's good enough to learn something from (just like I did while writing it).

          16 years later
          Write a Reply...