Jump to content

Having Trouble Creating Multiple Watermark... Can Anyone Help?


webster08

Recommended Posts

I am trying to add two watermarks to one image, but the second watermark is not being merged. Can anyone help me correct my code or send me in the right directions; to correcting this issue?

 

PS: I know that the header is set to plain text, but I have a reason for that.

 

<?php

header('content-type: text/plain text');

$watermark = imagecreatefrompng('watermark.png');
$watermark2 = imagecreatefromgif('watermark.gif');

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

$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($_GET['src']);

$size = getimagesize($_GET['src']);

$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;


$pic = imagecopymerge($image, $watermark, 1, 0, 0, 0, $watermark_width, $watermark_height, 65);
imagecopymerge($pic, $watermark2, 1, 0, 0, 0, $watermark_width, $watermark_height, 65);

imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
imagedestroy($watermark2);

?>[code=php:0]

Link to comment
Share on other sites

Well, I see at least one problem:

$pic = imagecopymerge($image, $watermark, 1, 0, 0, 0, $watermark_width, $watermark_height, 65);
imagecopymerge($pic, $watermark2, 1, 0, 0, 0, $watermark_width, $watermark_height, 65);

 

imagecopymerge() copies part of one image into another image - it doesn't return an image (it only returns true/false). In that second line you are trying to copy watermark2 into the return value of the first one. So, you should be doing this:

imagecopymerge($image, $watermark, 1, 0, 0, 0, $watermark_width, $watermark_height, 65);
imagecopymerge($image, $watermark2, 1, 0, 0, 0, $watermark_width, $watermark_height, 65);

 

But, for that to work, watermark 2 will need to have transparency - otherwise it will be covering watermark 1. Although, I've never done that so I'm not positive if transparency is even available when doing that.

Link to comment
Share on other sites

I tried your code mjdamato; but it still does not display the second watermark. The second watermark is a transparent gif; so it should not cover the main image.

 

So here's what I got with your code:

<?php

header('content-type: text/plain text');

$watermark = imagecreatefrompng('watermark.png');
$watermark2 = imagecreatefromgif('watermark.gif');

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

$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($_GET['src']);

$size = getimagesize($_GET['src']);

$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;

imagecopymerge($image, $watermark, 1, 0, 0, 0, $watermark_width, $watermark_height, 65);
imagecopymerge($image, $watermark2, 1, 0, 0, 0, $watermark_width, $watermark_height, 65);

imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
imagedestroy($watermark2);

?>

 

That is actually what I did at first; the code you presented, but I added in the variable $pic, because the original two lines were not working.

Link to comment
Share on other sites

Oh... ok... here we go...  I got it.... figured it out.

 

Here is the final code:

 

<?php

header('content-type: text/plain text');

$watermark = imagecreatefrompng('watermark.png');
$watermark2 = imagecreatefromgif('watermark.gif');

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

// here is the fix
$watermark_width2 = imagesx($watermark2);
$watermark_height2 = imagesy($watermark2);

$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($_GET['src']);

$size = getimagesize($_GET['src']);

$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;

imagecopymerge($image, $watermark, 1, 0, 0, 0, $watermark_width, $watermark_height, 65);
imagecopymerge($image, $watermark2, 1, 0, 0, 0, $watermark_width2, $watermark_height2, 65);

imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
imagedestroy($watermark2);

?>

 

Thanks for the help mjdamato!

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.