Combine two images in php

I have two images that I would like to combine and then save to a new location.
I would like the second image to be placed directly below the first image.
I have the following, but the image is not even saved.

$destimg = imagecreatefromjpeg('images/myimg.jpg'); $src = imagecreatefromgif('images/second.gif'); // Copy and merge imagecopymerge($destimg, $src, 316, 100, 0, 0, 316, 100, 100); 

Both images are either 316px x 100px wide
From the code above, $ destimg should now be 316x200, but this does not happen. Also, as it should be a new image and save to another folder.

Thanks for any help.

+4
source share
5 answers

The best approach for this situation would be to create a new image in memory using the combined sizes you want, then copy or reprogram the existing images to a new image, and then save the new image to disk.

For instance:

 function merge($filename_x, $filename_y, $filename_result) { // Get dimensions for specified images list($width_x, $height_x) = getimagesize($filename_x); list($width_y, $height_y) = getimagesize($filename_y); // Create new image with desired dimensions $image = imagecreatetruecolor($width_x + $width_y, $height_x); // Load images and then copy to destination image $image_x = imagecreatefromjpeg($filename_x); $image_y = imagecreatefromgif($filename_y); imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x); imagecopy($image, $image_y, $width_x, 0, 0, 0, $width_y, $height_y); // Save the resulting image to disk (as JPEG) imagejpeg($image, $filename_result); // Clean up imagedestroy($image); imagedestroy($image_x); imagedestroy($image_y); } 

Example:

 merge('images/myimg.jpg', 'images/second.gif', 'images/merged.jpg'); 
+16
source

I suggest that you use Image Magick instead (pecl-imagick module or run it as a command through the shell). I have several reasons:

Imagick:

  • faster
  • knows more format
  • makes quality images
  • have more features (e.g. text rotation)
  • and much more...

Your Imagick :: compositeImage method if you use php module. Manual: http://php.net/manual/en/function.imagick-compositeimage.php

0
source

I would like to add one more thing if you are using the PHP GD Library, then you should include imagesavealpha() and alphablending() .

0
source

Use this function to combine two images side by side, and it worked perfectly! Thank you very much!

0
source

I found the answer, use GD:

  function merge($filename_x, $filename_y, $filename_result) { // Get dimensions for specified images list($width_x, $height_x) = getimagesize($filename_x); list($width_y, $height_y) = getimagesize($filename_y); // Create new image with desired dimensions $image = imagecreatetruecolor($width_x, $height_x); // Load images and then copy to destination image $image_x = imagecreatefromjpeg($filename_x); $image_y = imagecreatefromgif($filename_y); imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x); // top, left, border,border imagecopy($image, $image_y, 100, 3100, 0, 0, $width_y, $height_y); // Save the resulting image to disk (as JPEG) imagejpeg($image, $filename_result); // Clean up imagedestroy($image); imagedestroy($image_x); imagedestroy($image_y); } 

like this:

 merge('images/myimage.jpg', 'images/second.gif', 'images/merged.jpg'); 
-4
source

Source: https://habr.com/ru/post/1390930/


All Articles