TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-19-2007, 03:04 PM   #1 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default 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.
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
The Following User Says Thank You to Rendair For This Useful Post:
Nor (12-19-2007)
Old 12-19-2007, 03:07 PM   #2 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Once again, a very nice article! You really should start a fan club Dale.
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
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-19-2007, 03:58 PM   #3 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

Quote:
Originally Posted by ReSpawN View Post
Once again, a very nice article! You really should start a fan club Dale.
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 haha joking.

I shall expand on that script to do the things you wish and among other things.
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-19-2007, 04:18 PM   #4 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Thanks Will look into this :).
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 02-02-2008, 07:21 PM   #5 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Default

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
Send a message via MSN to CΛSTΞX
CΛSTΞX is offline  
Reply With Quote
Old 02-02-2008, 08:09 PM   #6 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

Quote:
Originally Posted by CΛSTΞX View Post
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
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 02-02-2008, 09:16 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Old 02-02-2008, 10:02 PM   #8 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Default

So what should I do ? Is it possible for me to run this script ?
Send a message via MSN to CΛSTΞX
CΛSTΞX is offline  
Reply With Quote
Old 02-02-2008, 10:10 PM   #9 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:24 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design