TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Watermaking Images (http://www.talkphp.com/advanced-php-programming/1768-watermaking-images.html)

Rendair 12-19-2007 03:04 PM

Watermaking Images
 
Hey all, time for another GD tutorial. This time i am going to go through "watermarking" your images.


DEMO: HERE

Firstly create a watermark image and save it as png-24 for best quality.

Firstly of course we want to set the actual image we want to watermark.

PHP Code:

$file $_GET["src"]; // set it to a variable 

That will take the source of the image from a variable set in the url, which we will talk about later.

Next we want to create the watermark image and load it up into RAM.

PHP Code:

$watermark imagecreatefrompng("watermark.png"); 

As i said PNG work best. We do have a problem with using PNG-24 as the new GD doesnt seem to like it. Usally it just gives the image a white background, but we can get around this by doing the following.

PHP Code:

imagealphablending($watermarktrue); 

Now we want to create the actual image we are adding the watermark to and load it into RAM.

PHP Code:

$image=imagecreatefromjpeg($file); 

Now we just want to get the height and width of the image so we can place the watermark correctly.

PHP Code:

$imageWidth=imageSX($image);
$imageHeight=imageSY($image); 

and we also want to do the same for the actual watermark image itself.

PHP Code:

$watermarkWidth=imageSX($watermark);
$watermarkHeight=imageSY($watermark); 

Now we need to set up some variables that will tell us where to place the watermark on the image.

PHP Code:

$coordinate_X = ($imageWidth 5) - ($watermarkWidth);
$coordinate_Y = ($imageHeight 5) - ($watermarkHeight);
//we want to be at least 5 pixels from the edge, but to make sure
// we shall add abit more so also take away the watermark dimensions 

Now its a matter of place the watermark on the actual image.

PHP Code:

imagecopy($image$watermark$coordinate_X$coordinate_Y00$watermarkWidth$watermarkHeight); 

Now we need to set a header type as a image as we do with all GD scripts and also display the image, but also clear any information we may still have in the RAM.

PHP Code:

header('content-type: image/jpeg'); 
imagejpeg ($image);
imagedestroy($image);
imagedestroy($watermark); 

Usage

This script can easily be used by using the following script.

PHP Code:

<img src=watermark.php?src=image.jpg>
//change the image.jpg with the location of the image you want to
//watermark 

And there you go a nicely watermarked image :-)

FULL CODE
PHP Code:

<?php

    $file 
$_GET["src"];
    
$watermark imagecreatefrompng("watermark.png");
    
    
imagealphablending($watermarktrue);

    
$image=imagecreatefromjpeg($file);

    
$imageWidth=imageSX($image);
    
$imageHeight=imageSY($image);

    
$watermarkWidth=imageSX($watermark);
    
$watermarkHeight=imageSY($watermark);

    
$coordinate_X = ($imageWidth 5) - ($watermarkWidth);
    
$coordinate_Y = ($imageHeight 5) - ($watermarkHeight);

    
imagecopy($image$watermark$coordinate_X$coordinate_Y00$watermarkWidth$watermarkHeight);
    
    
header('content-type: image/jpeg'); 
    
imagejpeg ($image);
    
imagedestroy($image);
    
imagedestroy($watermark);

?>

This only currently creates watermarks for jpegs, but can easily be changed to support all types.

ReSpawN 12-19-2007 03:07 PM

Once again, a very nice article! You really should start a fan club Dale. :-P
Can you extend this function that you can either use text, or upload an PNG/GIF (transparent) image and then watermark it that way? Since I've got my own logo and all!

Keep it up man! Talk to ya on MSN. :-)

Mark

Rendair 12-19-2007 03:58 PM

Quote:

Originally Posted by ReSpawN (Post 6861)
Once again, a very nice article! You really should start a fan club Dale. :-P
Can you extend this function that you can either use text, or upload an PNG/GIF (transparent) image and then watermark it that way? Since I've got my own logo and all!

Keep it up man! Talk to ya on MSN. :-)

Mark

Haha i am the GD Guru :-P haha joking.

I shall expand on that script to do the things you wish and among other things. :-)

Nor 12-19-2007 04:18 PM

Thanks Will look into this :).

CΛSTΞX 02-02-2008 07:21 PM

Warning: imagesx(): supplied argument is not a valid Image resource in /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php on line 11

Warning: imagesy(): supplied argument is not a valid Image resource in /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php on line 12

Warning: imagecopy(): supplied argument is not a valid Image resource in /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php on line 20

Warning: Cannot modify header information - headers already sent by (output started at /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php:11) in /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php on line 22

Warning: imagejpeg(): supplied argument is not a valid Image resource in /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php on line 23

Warning: imagedestroy(): supplied argument is not a valid Image resource in /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php on line 24

Rendair 02-02-2008 08:09 PM

Quote:

Originally Posted by CΛSTΞX (Post 10148)
Warning: imagesx(): supplied argument is not a valid Image resource in /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php on line 11

Warning: imagesy(): supplied argument is not a valid Image resource in /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php on line 12

Warning: imagecopy(): supplied argument is not a valid Image resource in /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php on line 20

Warning: Cannot modify header information - headers already sent by (output started at /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php:11) in /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php on line 22

Warning: imagejpeg(): supplied argument is not a valid Image resource in /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php on line 23

Warning: imagedestroy(): supplied argument is not a valid Image resource in /usr/hosts/domains/propaket/bodrumlife_pro/www.forumistan.net/httpdocs/sifreli/Yansima/process_thumb.php on line 24


I would say this means the version of PHP you are using or GD isn't up to date or higher

Salathe 02-02-2008 09:16 PM

The errors listed by CΛSTΞX would occur if the file cannot be loaded in the line: $image=imagecreatefromjpeg($file); Perhaps introduce some basic condition and/or error checking to prevent errors piling up on top of themselves.

CΛSTΞX 02-02-2008 10:02 PM

So what should I do ? Is it possible for me to run this script ?

Salathe 02-02-2008 10:10 PM

I don't see why not. Just point your code to image and watermark files that are on your server and everything should be fine.


All times are GMT. The time now is 07:21 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0