Reply to Re: How can I merge 4 image ?

Re: How can I merge 4 image ? new!
by Orkan, 13 years, 11 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

Your reply

Name *
Email 
Title *
Text *
CAPTCHA image
Enter the code displayed on the image:
Click on the image to generate another one if it is hard to read it. The case is important