Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
* * * * * 1 votes

Watermark Your Image With Simple Php Script


42 replies to this topic

#1 BuffaloHelp

    Sterling Archer

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 4,088 posts
  • Gender:Male
  • myCENT:50.18

Posted 24 April 2006 - 02:46 AM

This script was found on the net http://tips-scripts....p=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)
<?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:
<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:
<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:
$startwidth = (($imagewidth - $watermarkwidth)/2); 
$startheight = (($imageheight - $watermarkheight)/2); 
To have it appear on the bottom right corner, try this:
$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 :lol:

#2 Dooga

    Coolio

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,346 posts
  • Gender:Male
  • Location:Canada
  • Interests:Eating SPAM
  • myCENT:66.95

Posted 24 April 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...

#3 BuffaloHelp

    Sterling Archer

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 4,088 posts
  • Gender:Male
  • myCENT:50.18

Posted 24 April 2006 - 03:32 AM

Do you mean like this?
<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:
<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"

#4 Dooga

    Coolio

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,346 posts
  • Gender:Male
  • Location:Canada
  • Interests:Eating SPAM
  • myCENT:66.95

Posted 24 April 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!

#5 WindAndWater

    Advanced Member

  • Kontributors
  • PipPipPipPipPipPipPip
  • 106 posts

Posted 24 April 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.

<?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
<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.

Edited by WindAndWater, 24 April 2006 - 12:29 PM.


#6 BuffaloHelp

    Sterling Archer

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 4,088 posts
  • Gender:Male
  • myCENT:50.18

Posted 24 April 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!

#7 Saint_Michael

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

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 7,459 posts
  • Gender:Male
  • Location:9r33|\| 399$ 4|\|D 5P4/\/\
  • Interests:$p4m 0n j00 $h4m3 m3 0nc3 $p4m 0n m3 $h4m3 m3 7\/\/1c3
  • myCENT:71.24

Posted 24 April 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

#8 Sprnknwn

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 821 posts
  • Interests:Music

Posted 24 April 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.

#9 BuffaloHelp

    Sterling Archer

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 4,088 posts
  • Gender:Male
  • myCENT:50.18

Posted 24 April 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?

#10 WindAndWater

    Advanced Member

  • Kontributors
  • PipPipPipPipPipPipPip
  • 106 posts

Posted 24 April 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. :-)




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users