codegeek
codegeek

Reputation: 201

Unable to water mark images with php

I was unable to watermark my images. I used the php manual found at this link

http://www.php.net/manual/en/image.examples-watermark.php

And also tried the sitepoint tutorial here

http://www.sitepoint.com/watermark-images-php/

but getting the same error that the image cannot be displayed because its have some errors. can somebody let me know whats wrong with the code or suggest me some better solution. My code is here :

header('content-type: image/jpeg');  
$watermark = imagecreatefrompng('wm.png');  
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);  
$image = imagecreatetruecolor($watermark_width, $watermark_height);  
$image = imagecreatefromjpeg('img.jpg');  
$size = getimagesize('img.jpg');  
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;  
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);  
imagejpeg($image);  
imagedestroy($image);  
imagedestroy($watermark);

Upvotes: 1

Views: 114

Answers (1)

VolkerK
VolkerK

Reputation: 96159

Check all the return values of the functions you call, e.g.

$watermark = imagecreatefrompng('wm.png');  
if ( !$watermark ) {
    die('Sorry ' . __LINE__); // you might want to use something else here - just an example....
}

and then set the content type to image/jpeg not before you're actually trying to send the image

if ( headers_sent($file, $line) ) {
  die('oops '.__LINE__);
}
else {
    header('content-type: image/jpeg');  
    imagejpeg($image);  
}

...makes it easier to pin-point the error.

Upvotes: 1

Related Questions