Question

I've created a watermark function as below :

index.php

<?php
include 'core.inc.php';

do_watermark('bg1.jpg', 'logo1.png');
?>

core.inc.php

<?php
header ( 'Content-type: image/jpeg' );

function do_watermark($source_image, $logo) {

$watermark = imagecreatefrompng ( $logo );
$watermark_width = imagesx ( $logo );
$watermark_height = imagesy ( $logo );

$image = imagecreatetruecolor ( $watermark_width, $watermark_height );
$image = imagecreatefromjpeg ( $source_image );

$image_size = getimagesize ( $source_image );

$x = $image_size [0] - $watermark_width - 10;
$y = $image_size [1] - $watermark_height - 10;

imagecopymerge ( $image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_height, 40 );
imagejpeg ( $image );
}
?>

But when calling do_watermark('bg1.jpg', 'logo1.png'), show nothing.

bg1.jpg and logo1.png is along index.php.

Any help would be great.

Was it helpful?

Solution

Isn't the watermark image usually going to be smaller than the image being watermarked? You have created your working image to be the watermark's size rather than the source image's size -- is that what you really want? I'm not sure about the x and y coordinates -- the larger the watermark is, the further right and (up or down?) it will be, as x and y increase with the size of the watermark. I would think about positioning it (x and y) as a function of both the image size and the watermark size.

You also have it hard coded for JPEG image and PNG watermark. You could get the image types from the file names or from the getimagesize() call ('mime' entry). Consider making it more flexible this way.

OTHER TIPS

There were some major problems in the code you had written, this should work, but compare it to yours to see what was wrong.

<?php

function do_watermark($source_image, $logo) {

    $watermark      = imagecreatefrompng($logo);
    $watermark_width    = imagesx($watermark);
    $watermark_height   = imagesy($watermark);

    $image          = imagecreatefromjpeg($source_image);
    $image_width        = imagesx($image);
    $image_height       = imagesy($image);

    $x = $image_width - $watermark_width - 10;
    $y = $image_height - $watermark_height - 10;

    imagecopymerge($image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_height, 40 );

    header('Content-Type: image/jpeg');
    imagejpeg($image);

    imagedestroy($image);

}

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top