views:

79

answers:

2

Hi,

I would like to create a picture in PHP with GD composed by different other pictures. For example I have 6 pictures (or more) and I would like to create ONE picture who contain these different pictures.

The Difficulty is that my final picture must have a fixed width and height (304x179), so if the different pictures are too big they must be cut. This is an example from IconFinder :

This picture have 6 images

This picture is composed by 6 images, but the 3rd bird (green) is cutted, and the 4, 5 and 6 are cutted in the bottom. This is what I want, can you give me some help to write this code in PHP ?

Thanks

+1  A: 

Create your primary image and consider it your "canvas."

From there, use imagecopy() to copy the smaller images into the canvas image.

See this for example:

<?php
header('Content-Type: image/jpg');
$canvas = imagecreatetruecolor(304, 179);
$icon1 = imagecreatefromjpeg('icon.jpg');
$icon2 = imagecreatefromjpeg('icon2.jpg');
// ... add more source images as needed
imagecopy($canvas, $icon1, 275, 102, 0, 0, 100, 100);
imagecopy($canvas, $icon2, 0, 120, 0, 0, 100, 100);
// ... copy additional source images to the canvas as needed
imagejpeg($canvas);
?>

In my example, icon.jpg is a 100x100 image which I am placing in the canvas such that its top left corner is located at 275, 102 in the canvas, which cuts off the right side.

Edit

I adjusted the code to be more similar to what you're doing.

JYelton
Yes but how I add all my first images for creating ONE picture ? And how I count how many images I will need to create the final image ? Because if initial pictures are 128*128 I will need 6 pictures for example, but if they're 32x32 I will need more
Jensen
Thanks It will help me to solve the problem
Jensen
I see what you mean, you only want to add as many as will show up (albeit cropped) in the canvas image. You'll need to write a loop around the portion of code that loads the images and `imagecopy`'s them to the canvas. Are all the images going to be identical sized? I'm not sure how you want to calculate the position of them in the canvas, but you can use `imagesx` and `imagesy` to find the dimensions.
JYelton
A: 

Here a none tested modify spinet from one of my scripts, hope it can be usefull:

    header('Content-type: image/png');

    $image = array() //Populate this array with the image paths

    //Create the Letters Image Objects
      foreach($image as $a){
        $image['obj'][] = imageCreateFromPNG($a);
      }unset($a);

      $canvasW = 300;
      $canvasH = 300;

    //Create Canvas
      $photoImage = imagecreatetruecolor($canvasW,$canvasH);
      imagesavealpha($photoImage, true);
      $trans_color = imagecolorallocatealpha($photoImage, 0, 0, 0, 127);
      imagefill($photoImage, 0, 0, $trans_color);

    //Merge Images
      $Offset_y = 0;
      $images_by_row = 3;
      $images_rows_height = 100; // height of each image row
      $counter = 0;

      foreach($image['obj'] as $a){
        $counter++;

        $width = ceil(imagesx($a));
        $height = ceil(imagesy($a));

        if(!isset($offset)){ $offset = 1; }

        imageComposeAlpha($photoImage, $a, $offset, $Offset_y,$width,$height);

        if($offset >= 1){
          $offset = $offset + $width;
        }

        //Check if new row next time
        if($counter >= $images_by_row){
          if($images_by_row%$counter){
            $offset_y += $images_rows_height;
          }
        }

      }unset($a);

      imagepng($photoImage);
Pablo