Search code examples
phpwatermarkphp-gd

adding multiple pngs to image php


I am adding a line of text onto an image. Is it possible to overlay multiple lines (line2.png & line3.png) onto the same image?

$stamp = imagecreatefrompng('img/line1.png');
$im = imagecreatefromjpeg('tom.jpg');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 40;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

Solution

  • Yes, you can by using imagecopy.

    Typical usage:

    $copyimage = imagecreatefromjpeg("path/to/image/you/want/to/put/into/an/image.jpg");
    //$copyimage just needs to be a GD object, so you can also use imagecreatefrompng etc.
    imagecopy($image, $copyimage, $position_x, 
                $position_y, 0, 0, $image_width, $image_height);
    

    http://php.net/imagecopy