How can I merge 4 image ?

See all posts Reply

How can I merge 4 image ? new!
by Orkan, 13 years, 5 months ago
Hello,

I want to merge 4 image file like this http://img543.imageshack.us/img543/1565/55055879.gif.

I need some margins between images and save as png transparent format, How can I do this ?

Thank you for any reply.Reply
Re: How can I merge 4 image ? new!
by colin, 13 years, 5 months ago
I think that the class is not really the good tool for that. You could probably use the watermark feature of the class to achieve your goal, calling process() 4 times. But you will loose some quality each time.

You should use Imagemagick. See for instance this page.Reply
Re: How can I merge 4 image ? new!
by Orkan, 13 years, 5 months ago
thank you for your reply, I will try to do this with Imagemagick.Reply
Re: How can I merge 4 image ? new!
by Orkan, 13 years, 5 months ago
Hello,

I have done, here is solution for photo merge

function mergeImages($images,$width,$height) {
  $imageData = array();
  $len = count($images);
  $wc = ceil(sqrt($len));
  $hc = floor(sqrt($len/2));
  $maxW = array();
  $maxH = array();
  for($i = 0; $i < $len; $i++) {
    $imageData[$i] = getimagesize($images[$i]);
    $found = false;
    for($j = 0; $j < $i; $j++) {
      if ( $imageData[$maxW[$j]][0] < $imageData[$i][0] ) {
        $farr = $j > 0 ? array_slice($maxW, $j-1, $i) : array();
        $maxW = array_merge($farr, array($i), array_slice($maxW, $j));
        $found = true;
        break;
      }
    }
    if ( !$found ) {
      $maxW[$i] = $i;
    }
    $found = false;
    for($j = 0; $j < $i; $j++) {
      if ( $imageData[$maxH[$j]][1] < $imageData[$i][1] ) {
        $farr = $j > 0 ? array_slice($maxH, $j-1, $i) : array();
        $maxH = array_merge($farr, array($i), array_slice($maxH, $j));
        $found = true;
        break;
      }
    }
    if ( !$found ) {
      $maxH[$i] = $i;
    }
  }
 
  /*
  for($i = 0; $i < $wc; $i++) {
    $width = 573;//$imageData[$maxW[$i]][0]+20;
  }

  for($i = 0; $i < $hc; $i++) {
    $height = 412;//$imageData[$maxH[$i]][1]+20;
  }
  */
 
  $im = imagecreatetruecolor($width, $height);

  $red = imagecolorallocate($im, 255, 0, 0);
  $black = imagecolorallocate($im, 0, 0, 0);
  
  // Make the background transparent
  imagecolortransparent($im, $black);

  $wCnt = 0;
  $startWFrom = 0;
  $startHFrom = 0;

  for( $i = 0; $i < $len; $i++ ) {
    $tmp = imagecreatefromjpeg($images[$i]);
    imagecopyresampled($im, $tmp, $startWFrom, $startHFrom, 0, 0, $imageData[$i][0], $imageData[$i][1], $imageData[$i][0], $imageData[$i][1]);
    $wCnt++;
    // Margin
    if($wCnt == 1) {
      $startWFrom = 301;
      $startHFrom = 0;
    }
    if($wCnt == 2) {
      $startWFrom = 0;
      $startHFrom = 219;
    }
    if($wCnt == 3) {
      $startWFrom = 301;
      $startHFrom = 219;
    }
    /*
    if ( $wCnt == $wc ) {
      $startWFrom = 0;
      $startHFrom += $imageData[$maxH[0]][1];
      $wCnt = 0;
    } else {
      $startWFrom += $imageData[$i][0];
    }
    */
  }
 
  return $im;
}
 
$im = mergeImages(array('image 14.jpg', 'image 15.jpg','image 17.jpg','image 18.jpg'),'573','412');
 
header('Content-type: image/gif');

imagepng($im);
Reply
Re: How can I merge 4 image ? new!
by Orkan, 13 years, 5 months ago
Above sample code work perfect but its can't save as transparent I tried to save images as transparent but its remove black pixels from images.Reply