• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 594
  • Last Modified:

PHP GDLibs Merge 2 images Side by Side into 1 file

I have an image that the size can be variable. I want to take and crop a piece from the left side and the right side

size of the crop is variable but will mainly be full height and 1px wide.

I then want to take those crop pieces and merge them side by side into a single image

so I end up with 1 final image that is 2px wide (or however wide the crop I make was) by the height of the original image

I have code currently that makes the crop images I just have not figured out how to make the merged image

// Original image
$filename = '1.jpg';
$left = 'left-'.$filename;
$right = 'right-'.$filename;
$newname = 'fullimage-'.$filename;

if (file_exists($left)) {
unlink($left);
unlink($right);
}
if (file_exists($newname)) {
unlink($newname);
}

do_right($filename);
do_left($filename);

function do_right($filename){
// Get dimensions of the original image
list($current_width, $current_height) = getimagesize($filename);
 
// The x and y coordinates on the original image where we
// will begin cropping the image
$left = 800; //the source image is currently 1000px so 800 makes a proper 200px crop on the right side
$top = 0;
 
// This will be the final size of the image (e.g. how many pixels
// left and down we will be going)
$crop_width = 200;
$crop_height = $current_height;
 
// Resample the image
$canvas = imagecreatetruecolor($crop_width, $crop_height);
$current_image = imagecreatefromjpeg($filename);
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
imagejpeg($canvas, 'right-'.$filename, 100);
}

function do_left($filename){
// Get dimensions of the original image
list($current_width, $current_height) = getimagesize($filename);
 
// The x and y coordinates on the original image where we
// will begin cropping the image
$left = 0;
$top = 0;
 
// This will be the final size of the image (e.g. how many pixels
// left and down we will be going)
$crop_width = 200;
$crop_height = $current_height;
 
// Resample the image
$canvas = imagecreatetruecolor($crop_width, $crop_height);
$current_image = imagecreatefromjpeg($filename);
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
imagejpeg($canvas, 'left-'.$filename, 100);
}

Open in new window


Thanks for any help
0
TechStudio
Asked:
TechStudio
1 Solution
 
kelvinwkwCommented:
<?php
$file = "-1.jpg";
$file_l = "left".$file;
$file_r = "right".$file;
$file_n = "new".$file;

function merge($file_l, $file_r, $file_n, $size ){
      
      list($width_1, $height_1) = getimagesize($file_l);
      list($width_r, $height_r) = getimagesize($file_r);
      
      if ( $height_l > $height_r )
            $height = $height_l;
      else
            $height = $height_r;
      
      $canvas = imagecreatetruecolor($size * 2, $height);
      $current_image = imagecreatefromjpeg($file_r);
      imagecopy($canvas, $current_image, 0, 0, $width_r - ($size * 2), 0, $size * 2, $height);
      $current_image = imagecreatefromjpeg($file_l);
      imagecopy($canvas, $current_image, 0, 0, 0, 0, $size, $height);      
      imagejpeg($canvas, $file_n, 100 );
}

// the 100 is in pixel you need to crop from each images to merge
merge($file_l, $file_r, $file_n, 100 );


?>
0
 
Ray PaseurCommented:
0
 
TechStudioAuthor Commented:
After some slight code modifications to streamline the script I was working on this worked great. I have never used GDLibs so was new to it and its functions
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now