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
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
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)
 



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 10:00 AM.

 
     

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