Welcome Guest ( Log In | Register)



4 Pages V   1 2 3 > »   
Reply to this topicStart new topic
> Watermark Your Image With Simple Php Script, found it on the net
Rating 5 V
BuffaloHELP
post Apr 24 2006, 02:46 AM
Post #1


Desperately seeking "any key" to continue...
Group Icon

Group: Admin
Posts: 3,438
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042



This script was found on the net http://tips-scripts.com/?tip=watermark#tip B&T's Tips & Scripts site. Just in case the site may not show, I will include the code here:

List of things needed:
1. your image in any format
2. watermark image--in gif format with transparent background
3. script below with name (i.e. watermark.php)
CODE
<?php
// this script creates a watermarked image from an image file - can be a .jpg .gif or .png file
// where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory as this script
// where this script is named watermark.php
// call this script with an image tag
// <img src="watermark.php?path=imagepath"> where path is a relative path such as subdirectory/image.jpg
$imagesource =  $_GET['path'];
$filetype = substr($imagesource,strlen($imagesource)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif")  $image = @imagecreatefromgif($imagesource);  
if($filetype == ".jpg")  $image = @imagecreatefromjpeg($imagesource);  
if($filetype == ".png")  $image = @imagecreatefrompng($imagesource);  
if (!$image) die();
$watermark = @imagecreatefromgif('watermark.gif');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);  
$watermarkwidth =  imagesx($watermark);
$watermarkheight =  imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>


Name this script, i.e. watermark.php and call this script as following:
HTML
<img src="watermark.php?path=image_name.filetype">

the only thing you need to chage is "image_name.filetype" and of course you can have the relative path such as:
HTML
<img src="folder/watermark.php?path=folder/imagename">


The caution here is the script watermark.php and watermark.gif should be in the same location. watermark.gif should have transparent background.

As you can read it from the site, the location where the watermark.gif appears can be modified by adjusting this line of the code:
CODE
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);

To have it appear on the bottom right corner, try this:
CODE
$startwidth = (($imagewidth - $watermarkwidth) );
$startheight = (($imageheight - $watermarkheight) );


Personal note: I have installed Apache2 and PHP5 in my computer and couldn't get this working at first. Later I found out I had to edit php.ini under extension to enable php_gd2.dll in order to make it work under http://localhost/

I hope you find a good usage out of this smile.gif
Go to the top of the page
 
+Quote Post
Dooga
post Apr 24 2006, 03:21 AM
Post #2


Moderator
Group Icon

Group: [MODERATOR]
Posts: 1,327
Joined: 26-December 04
From: Canada
Member No.: 2,940



Hey this can be really useful when it comes to using images. You no longer have to make a watermark for every image that you make in image editing software! Now, does this work with hotlinks? It would be cool if I can put a watermark on an imageshack uploaded image or a hotlinked banner etc...
Go to the top of the page
 
+Quote Post
BuffaloHELP
post Apr 24 2006, 03:32 AM
Post #3


Desperately seeking "any key" to continue...
Group Icon

Group: Admin
Posts: 3,438
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042



Do you mean like this?
HTML
<img src="watermark.php?path=http://site/image.type">

I tested it out and what do you know, it works... wow good question and what a find! But remember that watermark.gif should be with watermark.php in the same location.

I wonder if the script can be modified so that the watermark.gif can be located elsewhere...? For those PHP gurus out there, see if you can modify this script so that you can use different watermark.gif images. So that we can use one line command such that you can use multiple or alternate watermark.gif images:
HTML
<img src="watermark.php?mark=watermark.gif_location&path=image_location">


Again, since I am very new to PHP programming, I'm assuming two different var can be called in one command. So basically, the command would be like: "watermark script, location of watermark.gif, location of image"
Go to the top of the page
 
+Quote Post
Dooga
post Apr 24 2006, 03:36 AM
Post #4


Moderator
Group Icon

Group: [MODERATOR]
Posts: 1,327
Joined: 26-December 04
From: Canada
Member No.: 2,940



Muwhaha now I can steal the Trap17 sigs and said I made them!!

I'm just kidding... but it is a very useful find!
Go to the top of the page
 
+Quote Post
WindAndWater
post Apr 24 2006, 05:35 AM
Post #5


Advanced Member
*******

Group: Members
Posts: 106
Joined: 1-April 06
Member No.: 21,148



Here's one with a variable path as requested. It works on my Trap17 account. I cleaned up the code, added some idiot proofing, and made it so that the watermark could be a .png which also supports alpha channels (transparency) and which won't dither like gifs do. Like the original, it only supports images with .gif/.jpg/.jpeg/.png extensions. I left the original author's (bad) naming scheme.

CODE
<?php
    // this script creates a watermarked image from an image file - can be a .jpg .gif or .png file
    // where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory as this script
    // where this script is named watermark.php
    // call this script with an image tag
    // <img src="watermark.php?path=imagepath"> where path is a relative path such as subdirectory/image.jpg
    $imagesource =  $_GET['path'];
    $watermarkPath = $_GET['watermark'];
    $filetype = substr($imagesource,strlen($imagesource)-4,4);
    $filetype = strtolower($filetype);
    $watermarkType = substr($watermarkPath,strlen($watermarkPath)-4,4);
    $watermarkType = strtolower($watermarkType);
    
    if($filetype == ".gif")  
        $image = @imagecreatefromgif($imagesource);
    else  
        if($filetype == ".jpg" || $filetype == "jpeg")  
            $image = @imagecreatefromjpeg($imagesource);
        else
            if($filetype == ".png")  
                $image = @imagecreatefrompng($imagesource);
            else
                die();  
    
    if(!$image)
        die();
    
    if($watermarkType == ".gif")
        $watermark = @imagecreatefromgif($watermarkPath);
    else
        if($watermarkType == ".png")
            $watermark = @imagecreatefrompng($watermarkPath);
        else
            die();
        
    if(!$watermark)
        die();
        
    $imagewidth = imagesx($image);
    $imageheight = imagesy($image);  
    $watermarkwidth =  imagesx($watermark);
    $watermarkheight =  imagesy($watermark);
    $startwidth = (($imagewidth - $watermarkwidth)/2);
    $startheight = (($imageheight - $watermarkheight)/2);
    imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
    imagejpeg($image);
    imagedestroy($image);
    imagedestroy($watermark);
?>

It can be accessed by using
CODE
<img src="watermark.php?path=imagePath.ext&watermark=watermarkPath.gif>
or
<img src="watermark.php?path=imagePath.ext&watermark=watermarkPath.png>


edit: Removed a "." and now the entire script's correct.

This post has been edited by WindAndWater: Apr 24 2006, 12:29 PM
Go to the top of the page
 
+Quote Post
BuffaloHELP
post Apr 24 2006, 07:41 AM
Post #6


Desperately seeking "any key" to continue...
Group Icon

Group: Admin
Posts: 3,438
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042



WindAndWater,

Oh good lord! That worked beautifully! Thank you. And you're right: although I would not use png at this moment (since my watermark will be simple and small) the need for making a script that can be adaptable is very crucial to a perfect script.

Thanks again!
Go to the top of the page
 
+Quote Post
Saint_Michael
post Apr 24 2006, 09:18 AM
Post #7


$p4m 0n j00 $h4m3 m3 0nc3 $p4m 0n m3 $h4m3 m3 7\/\/1c3
*********************

Group: [HOSTED]
Posts: 6,308
Joined: 21-September 04
From: 9r33|\| 399$ 4|\|D 5P4/\/\
Member No.: 1,218
T17 GFX Crew



hmmm interesting script but from what I search up on this type of script you can use gd support and also htaccess as well to make your water makr even more dynamic and what not.

here are some example links
htaccess version

gd support
Go to the top of the page
 
+Quote Post
Sprnknwn
post Apr 24 2006, 01:35 PM
Post #8


Privileged Member
*********

Group: Members
Posts: 822
Joined: 6-March 05
Member No.: 4,202



Cool. I didnŽt know that you could do that with a php script.

Thanks for showing it to us, maybe IŽll give it a try.