Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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');
like image 744
sepehr2121 Avatar asked May 21 '13 17:05

sepehr2121


2 Answers

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');
like image 151
Najmul Hosain Avatar answered Oct 12 '22 23:10

Najmul Hosain


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.

like image 27
Jessie Green Avatar answered Oct 13 '22 00:10

Jessie Green