IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
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
no avatar
More than meets the eye
******************
Group: Admin
Posts: 3,696
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042
myCENT:19.60



Post #1 post Apr 24 2006, 02:46 AM
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
no avatar
Moderator
***********
Group: [MODERATOR]
Posts: 1,344
Joined: 26-December 04
From: Canada
Member No.: 2,940
myCENT:46.53



Post #2 post Apr 24 2006, 03:21 AM
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
no avatar
More than meets the eye
******************
Group: Admin
Posts: 3,696
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042
myCENT:19.60



Post #3 post Apr 24 2006, 03:32 AM
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
no avatar
Moderator
***********
Group: [MODERATOR]
Posts: 1,344
Joined: 26-December 04
From: Canada
Member No.: 2,940
myCENT:46.53



Post #4 post Apr 24 2006, 03:36 AM
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
no avatar
Advanced Member
*******
Group: Members
Posts: 106
Joined: 1-April 06
Member No.: 21,148
myCENT:ZERO



Post #5 post Apr 24 2006, 05:35 AM
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
no avatar
More than meets the eye
******************
Group: Admin
Posts: 3,696
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042
myCENT:19.60



Post #6 post Apr 24 2006, 07:41 AM
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
no avatar
$p4m 0n j00 $h4m3 m3 0nc3 $p4m 0n m3 $h4m3 m3 7\/\/1c3
*********************
Group: [HOSTED]
Posts: 6,938
Joined: 21-September 04
From: 9r33|\| 399$ 4|\|D 5P4/\/\
Member No.: 1,218
T17 GFX Crew
myCENT:9.20



Post #7 post Apr 24 2006, 09:18 AM
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
no avatar
Privileged Member
*********
Group: Members
Posts: 821
Joined: 6-March 05
Member No.: 4,202



Post #8 post Apr 24 2006, 01:35 PM
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.
Go to the top of the page
+Quote Post
BuffaloHELP
no avatar
More than meets the eye
******************
Group: Admin
Posts: 3,696
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042
myCENT:19.60



Post #9 post Apr 24 2006, 03:10 PM
QUOTE

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

WindAndWater,

Where did you remove your "dot"? Because the first script worked just fine form me. If you are referring to JPEG, who actually uses JPEG extension today except for avi purpose?
Go to the top of the page
+Quote Post
WindAndWater
no avatar
Advanced Member
*******
Group: Members
Posts: 106
Joined: 1-April 06
Member No.: 21,148
myCENT:ZERO



Post #10 post Apr 24 2006, 09:41 PM
Yup I removed the . so that it reads "jpeg" as opposed to ".jpeg" which is 5 characters long, so it will never match a 4 character extension. Truthfully probably no-one uses .jpeg anymore, but it's an old habit that I haven't kicked yet. :-)
Go to the top of the page
+Quote Post

4 Pages V   1 2 3 > » 
Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   12 BooZker 1,136 8th July 2006 - 07:50 AM
Last post by: BooZker
No New Posts   13 galexcd 1,034 14th August 2007 - 01:25 PM
Last post by: Galahad
No New Posts   5 Shibbeh 651 20th August 2004 - 10:04 PM
Last post by: ill
No New Posts   4 Alpha 897 28th December 2008 - 05:12 PM
Last post by: stanny
No New Posts   6 shadowx 1,261 8th December 2008 - 12: