Merge two images in php - php

I have two images I would like to merge then save to a new location.
I would like the second image to be place directly below the first image.
I have the following so for but the image doesn't even save.
$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 have a width or 316px X 100px
From the above code the $destimg should now be 316x200 but that doesn't happen. Also like it to be a new image and save to another folder.
Thanks for any help.

The best approach for this situation may be to create a new image in memory with the combined dimensions you desire, then copy or resample the existing images to the new image, and then save the new image to disk.
For example:
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');

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

I suggest you to use Image Magick instead (pecl-imagick module or running it as command via shell). I have several reasons:
Imagick is:
faster
knows more format
makes better quality images
have more ability (for example text rotation)
and more...
Your method is Imagick::compositeImage if you use the php module. Manual: http://php.net/manual/en/function.imagick-compositeimage.php

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');

Related

Why my png function appear to not create any image?

I am trying to create a png watermark from text input. I use same function for jpeg and everything works perfectly fine.
I use imagestring(). In the code below I try to make two images, one of them is watermark and second one is resized dafault image.
I uplode photos through my form and they correctly appear in the default photos folder. None of the functions below return an image.
$destination_w is a path to the folder with watermarks. $destination_m is a path to the folder with miniatures. $_SESSION['mime'] and $_SESSION['image'] store correct values as I have tried to var_dump them and they appear in html which is rendered after this functions. Problem is that images do not exist, so they are not visible on my page.
This function is stored in watermark.inc.php file which is included after saving the photos to the default folder and their info in database.
else if ($_SESSION['mime'] === 'image/png'){
$img = imagecreatefrompng($source.'/'.$_SESSION['image']);
$blackcolor = imagecolorallocate($img, 0, 0, 0);
imagestring($img, 3,15, 30, $_SESSION['watermark'], $blackcolor);
imagepng($img, $destination_w.'/'.$_SESSION['image'], 100);
imagedestroy($img);
$img = imagecreatefrompng($source.'/'.$_SESSION['image']);
list($width, $height) = getimagesize($source.'/'.$_SESSION['image']);
$thumb = imagecreatetruecolor(200, 125);
//MINIATURES
imagecopyresized($thumb, $img, 0, 0, 0, 0, 200, 125, $width, $height);
imagepng($thumb, $destination_m.'/'.$_SESSION['image'], 100);
imagedestroy($img);
imagedestroy($thumb);
};

Save image to another server

I currently create and save a custom image using the function below. This works perfectly and saves the image to my server.
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);
// Free memory
imagedestroy($image);
imagedestroy($image_x);
imagedestroy($image_y);
}
Use:
$img1 = '/folder1/folder2/image1.gif';
$img2 = '/folder1/folder2/image2.gif';
$destination = '/dirabc/dirdef/finalImage.jpeg';
merge($img1 , $img2 , $destination );
The issue is, how could I save this image to another server I own?

Image create using gd

I am creating image using GD library all the functions are working fine. But the main problem where i stucked that i want to merge png image over an other image but after overlapping it cannot merge properly and looking like jpg or other instead of png. I cannot upload my image here due to low reputation so click on these links below to see the image.
The image which i want to merge is this
Png image
The image where i merge above image is:
My code is here:
<?php
$im = imagecreate(288,288);
$background_color = imagecolorallocate($im, 230, 248, 248);
$file = 'images/smiley/smile'.$_POST['smiley'].'.png';
$bg = imagecreatefrompng($file);
imagealphablending($im, true);
imagesavealpha($bg, true);
imagecopyresampled($im, $bg, 80, 80, 0, 0, 50, 50, 185, 185);
header("Content-Type: image/png");
$filename = $_SESSION['rand'].'.png';
imagepng($im,$filename);
echo '<img src="'.$filename.'" alt="" />';
?>
Your background image doesn't have an alpha channel. This makes the PHP GD library do all of it's copying operations without using an alpha channel, instead just setting each pixel to be fully opaque or transparent, which is not what you want.
The simplest solution to this is to create a new image of the same size as the background that has an alpha channel, and then copy both the background and face into that one.
$baseImage = imagecreatefrompng("../../var/tmp/background.png");
$topImage = imagecreatefrompng("../../var/tmp/face.png");
// Get image dimensions
$baseWidth = imagesx($baseImage);
$baseHeight = imagesy($baseImage);
$topWidth = imagesx($topImage);
$topHeight = imagesy($topImage);
//Create a new image
$imageOut = imagecreatetruecolor($baseWidth, $baseHeight);
//Make the new image definitely have an alpha channel
$backgroundColor = imagecolorallocatealpha($imageOut, 0, 0, 0, 127);
imagefill($imageOut, 0, 0, $backgroundColor);
imagecopy($imageOut, $baseImage, 0, 0, 0, 0, $baseWidth, $baseHeight); //have to play with these
imagecopy($imageOut, $topImage, 0, 0, 0, 0, $topWidth, $topHeight); //have to play with these
//header('Content-Type: image/png');
imagePng($imageOut, "../../var/tmp/output.png");
That code produces this image:

How to Insert a Signature on the Bottom of an Image in php?

I want to insert a signature (saved as png file) on the bottom of a letter (saved as jpg file) in a php site.
I used imagecopymerge, but it creates a black image file instead of my request.
I used this code too, but no result.
function merge($filename_x, $filename_y, $filename_result) {
list($width_x, $height_x) = getimagesize($filename_x);
list($width_y, $height_y) = getimagesize($filename_y);
$image = imagecreatetruecolor($width_x + $width_y, $height_x);
$image_x = imagecreatefromjpeg($filename_x);
$image_y = imagecreatefromgif($filename_y);
imagecopy($image, $image_x, 0, 20, 30, 50, $width_x, $height_x);
imagecopy($image, $image_y, $width_x, 0, 10, 0, $width_y, $height_y);
imagejpeg($image, $filename_result);
imagedestroy($image);
imagedestroy($image_x);
imagedestroy($image_y);
}
merge('myimg.jpeg', 'first.gif', 'merged.jpg');
Please try this function, I have customized yours.
function merge($filename_x, $filename_y, $filename_result) {
$source = imagecreatefromjpeg($filename_x);
$tobeMerged = imagecreatefromgif($filename_y);
//add signature on bottom right
imagecopymerge($source, $tobeMerged, imagesx($source) - imagesx($tobeMerged), imagesy($source) - imagesy($tobeMerged), 0, 0, imagesx($tobeMerged), imagesy($tobeMerged), 100);
//save your merged image
imagejpeg($source, $filename_result);
//destroy image resources to free memory
imagedestroy($source);
imagedestroy($tobeMerged);
}
merge('myimg.jpeg', 'first.gif', 'merged.jpg');
This function works for me. Since I have not seen your images, I can tell you what I am using to test it.
bg.jpg = 400X400 jpg
fg.gif = 200X200 gif (With transparent background)
function merge($filename_x, $filename_y, $filename_result) {
list($width_x, $height_x) = getimagesize($filename_x);
list($width_y, $height_y) = getimagesize($filename_y);
$image = imagecreatetruecolor($width_x, $height_x);
$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, 0, 0, 0, 0, $width_y, $height_y);
imagejpeg($image, $filename_result);
imagedestroy($image);
imagedestroy($image_x);
imagedestroy($image_y);
}
merge('bg.jpg', 'Untitled.gif', 'merged.jpg');
This seems to work fine. I am assuming maybe you are having some positioning issues. Try everything at starting position 0 then start moving until you get the desired effect.
Are you able to run command line tools (eg via exec)? If so, imagemagick command line tools can do just about any image manipulation you need. The layering functionality sounds like what you are after:
echo exec('composite -geometry +5+10 image1.jpg image2.png image2.png');
Your gif might have a color palette and is not a true color image. If your php version is 5+ check with imageistruecolor and in case use imagepalettetotruecolor.

PHP GD - resizing image frame/canvas but not the actual image

I have an image with size 88x31 and would like to make it 100x100 without resizing the actual image but only its canvas/frame, maybe by copying it in the center of the new blank white image with 100x100 size. Any ideas how to do it?
The correct method is to create a new image and then copy the old image into the middle of it (assuming the starting image is is a JPEG and smaller than 100x100):
$oldimage = imagecreatefromjpeg($filename);
$oldw = imagesx($oldimage);
$oldh = imagesy($oldimage);
$newimage = imagecreatetruecolor(100, 100); // Creates a black image
// Fill it with white (optional)
$white = imagecolorallocate($newimage, 255, 255, 255);
imagefill($newimage, 0, 0, $white);
imagecopy($newimage, $oldimage, (100-$oldw)/2, (100-$oldh)/2, 0, 0, $oldw, $oldh);
You can see here: Thumbnail generation with PHP tutorial

Categories