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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With