Adding Watermark to Image on the fly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Adding Watermark to Image on the fly

Post by simonmlewis »

http://php.net/manual/en/image.examples-watermark.php
Found this on PHP.net, but I don't see how you actually echo the resulting image.

It looks like just what we need, as the watermark can be altered easily by the site owner.

But how do you echo the watermarked image?

Code: Select all

// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('/images/productphotos/$row->photoprimary');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
This produces locally:

[text]Warning: imagecreatefromjpeg(/images/productphotos/$row->photoprimary): failed to open stream: No such file or directory in C:\xampp\phpMyAdmin\site\includes\product.inc on line 175

Warning: imagesx() expects parameter 1 to be resource, boolean given in C:\xampp\phpMyAdmin\site\includes\product.inc on line 185

Warning: imagesy() expects parameter 1 to be resource, boolean given in C:\xampp\phpMyAdmin\site\includes\product.inc on line 185

Warning: imagecopy() expects parameter 1 to be resource, boolean given in C:\xampp\phpMyAdmin\site\includes\product.inc on line 185

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\phpMyAdmin\site\index.php:191) in C:\xampp\phpMyAdmin\site\includes\product.inc on line 188

Warning: imagepng() expects parameter 1 to be resource, boolean given in C:\xampp\phpMyAdmin\site\includes\product.inc on line 189

Warning: imagedestroy() expects parameter 1 to be resource, boolean given in C:\xampp\phpMyAdmin\site\includes\product.inc on line 190[/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Adding Watermark to Image on the fly

Post by Celauran »

Variables inside single quotes aren't parsed, so there's the source of the first problem. If you're going to be doing image manipulation, why not make life easier on yourself and just use Imagine?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Adding Watermark to Image on the fly

Post by simonmlewis »

Even this won't work:

Code: Select all

<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng("stamp.png");
$im = imagecreatefrompng("photo.jpg");
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

$imgx = imagesx($im);
$imgy = imagesy($im);
$centerX=round($imgx/2);
$centerY=round($imgy/2);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, $centerX, $centerY, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
[text]The image "http://www........test.php" cannot be displayed, because it contains errors.[/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Adding Watermark to Image on the fly

Post by Celauran »

Code: Select all

imagecreatefrompng("photo.jpg");
That could be problematic.

What errors are being generated?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Adding Watermark to Image on the fly

Post by simonmlewis »

Whether I try this basic form with " or ', I get that quotation I submitted.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply