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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.