Watermark Images on the Fly in PHP

DocDave,

Any chance you could post the code you actually use, please? I still haven’t managed to sort out my issues, mainly because I have very little time to do it.

TIA
CJ

Ok here you go, the variable $photo_file is the file that was uploaded earlier in the routine. There is other stuff in the routine to size the image and what not. Hope this helps.


// Get the uploaded photos dimensions
$photo_size = getimagesize($photo_file);
$photo_width = $photo_size[0];
$photo_height = $photo_size[1];

// Create an image from the uploaded jpg
$photo_image = imagecreatefromjpeg($photo_file);
			
// Turn on Alpha Blending for the uploaded jpg
ImageAlphaBlending($photo_image, true);
			
// Check that the uploaded photo's width is larger than the 
// requested fullsize photo width, otherwise use the original width
if ($photo_width > $INFO['max_img_width'])
{
        $pic_width = $INFO['max_img_width'];
}
else
{
        $pic_width = $photo_width;
}
			
// Calculate the the fullsize photo height based on the width
$pic_height = round($photo_height / ($photo_width / $pic_width));
			
// Create the new image
$pic_img = imagecreatetruecolor($pic_width,$pic_height);
			imagecopyresized($pic_img,$photo_image,0,0,0,0,$pic_width,$pic_height,$photo_width,$photo_height);
			
// Define the watermark png file
$logo_file = $INFO['server_path']."/".$INFO['gallery_dir']."/htm/watermark.png";
			
// Get the logo dimensions from the file
$logo_size = getimagesize($logo_file);
$logo_width = $logo_size[0];
$logo_height = $logo_size[1];
			
// Create an image from the watermark png file
$logo_image = ImageCreateFromPNG($logo_file);
			
// Copy watermark logo image onto the photo image
ImageCopy($pic_img, $logo_image, $INFO['mark_x'], $INFO['mark_y'], 0, 0, $logo_width, $logo_height);

// Define the location of the jpg file to be created
$pic_file = $INFO['server_path']."/".$INFO['gallery_dir']."/pic/".$save_as_name;
			
// Create and store a jpg at the fullsize pic quality from the resized image created
imagejpeg($pic_img, $pic_file, $INFO['img_quality']);

// Clean-up any other left over images
ImageDestroy($photo_image);
ImageDestroy($logo_image);
ImageDestroy($pic_img);
			


:wink:

Perfection! Thanks Dave

I’m stuck with the following issue:

I’ve got two servers, one (free of cost) server containing some image archives (from now on referred to as server 1). This server does not run PHP and all access I have to that server is an FTP login. The other server (from now on referred to as server 2) is a true web server with PHP installed, and this one costs money.

So now I want to watermark the images from my PHP-less server 1 and show them on my site on server 2 (which does have PHP), WITHOUT using the bandwith of server 2, since then it will cost money you see :wink:

I’m thinking about this for some time now and I guess what I want is impossible. Can anyone confirm that?

Please send a mail to alpha@zoepe.nl -> thank you!

I thought that I was a decent PHP programmer, yet I can’t get this bloody thing to generate anything but errors. I’ve got like 200 pictures to watermark, I’ll be doing it for a while, unless one of you dudes can make sence of this.

I screwed with it for a while longer… and now I have white where it should be clear. I seem to remember someone with the same type of problem but with black… I’ll have to refer back in the thread.

Keep ya posted. :wink:

[edit: now its black too lol I might have to try and use DocDaves script and see what comes of it]

Any of the gd dudes still around?

I’ve still got this black bg haning around… I wonder if its my png file.

I tried adding:

$black = ImageColorAllocate ($watermark, 0, 0, 0);
 
 ImageColorTransparent($watermark , $black);

but it didn’t change a thing.

Thanks again dudes.

d3.

I was having the same problem with the background. Turns out my issue was that the sample code has the merge percentage at 100. I set this to 20 and it made a nice transparent version of my PNG as advertised!

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 20);

I fix it to run with transparent watermarks

<?php
header(‘content-type: image/jpeg’);
$watermark = imagecreatefrompng(‘watermark.png’);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($_GET[‘photo_file’]);
$size = getimagesize($_GET[‘photo_file’]);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopy($image, $watermark, $dest_x , $dest_y, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>

Claudio Heidel
www.heidel.com.ar

I have tried this code as it seems to be just what I want but i keep getting this error

imagecreatefrompng(): gd-png: fatal libpng error: Incompatible libpng version in application and library

I have php version 4.3.4 and GD 2.0.15 ( compatible) installed on the server I am using.

many thanks

Ratty

Just one problem - a watermarked picture is at reduced quality… I checked this with 1024x768 Porsche wallpaper. I use GD 2.0.15. Anyone knows how to preserve original image quality?

Gytis
www.nfstuning.com

Why i have this error ?? :

Warning: Cannot modify header information - headers already sent by (output started at /home/cirque2/public_html/aucirque/o/water.php:4) in /home/cirque2/public_html/aucirque/o/water.php on line 32
ÿØÿà…

I want only put a design :cry:

I keep getting this error

<br />
<b>Fatal error</b>: Call to undefined function: imagecreatefrompng() in <b>/root/public_html/watermark.php3</b> on line <b>5</b><br />

anyone else getting a error like this?

The imagejpeg($image); takes three values. If you change it to imagejpeg($image,‘’,100); it will keep your quality.

If you are getting an error which says imagecreatefrompng() your server most likely does not have GD 2.0 installed.

Would anyone care for a function to use during image upload?

A function during upload would be great…

Can I know how I can implement a transparent background for the png watermark I use? Currently the png is merged as a rectangular block.

use png-8. GD has errors if using png-24

getimagesize is a PHP-builtin function. imagesx and imagesy are not.

for the rest, a very good article!

this does not work on my site… where do you put the code and do you ahve to edit the <img tag