Jump to content

Watermarking using imagecopymerge() or imagecopy() does not add watermark


BlackAce

Recommended Posts

The script below runs without any errors (I've enabled all PHP errors just to be sure). But the file that is saved out does not have the watermark on it anywhere. Just the original source image.

 

Here is a live example for you to view:

 

http://www.ahsnewsline.com/watermark.php

 

<?php

$thumb_dir = "img/";
$perm_name = "wm_news001.jpg";
$filename = $thumb_dir.$perm_name;

echo "<strong>WATERMARK IMAGE:</strong><br/><img src='img/video-icon.png'/>";
echo "<br/><strong>BEFORE:</strong><br/><img src='$filename'/>";
echo "<br/>$filename<br/><br/>";

$watermark = imagecreatefrompng('img/video-icon.png');
$watermark_width = imagesx($watermark);		
$watermark_height = imagesy($watermark);

$image = imagecreatefromjpeg($filename);
$size = getimagesize($filename);
$dest_x = $size[0] - $watermark_width;
$dest_y = $size[1] - $watermark_height;

imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);

imagedestroy($image);
imagedestroy($watermark);

echo "<br/><hr/><br/><strong>AFTER:</strong><br/><img src='$filename'/>";
echo "<br/>$filename";

?>

 

NOTE: This version uses imagecopy() instead of imagecopymerge() due to it's PNG-24 support. And yes, I was using PNG-8 when I was using the imagecopymerge() function.

 

Regardless, it's still not working. Any ideas?

Add a content-type header:

 

...

imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
header('Content-type: image/jpeg');
imagejpeg($image);

...

 

EDIT: Oh, but you'll have to remove the echo's as they'll throw a "headers already sent..." error.

This is part of a larger script that is processing form submissions.

 

I realize that I failed to include the 2nd parameter of imagejpeg() which is the location where I'd like to save the new file. Thus:

 

imagejpeg($image, $filename);

 

Thank you for your help, sometimes it just helps to write this stuff out. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.