Loading...


bookmark - Watermark Your Image With Simple Php Script found it on the net

Watermark Your Image With Simple Php Script - found it on the net

 
 Discussion by BuffaloHelp with 42 Replies.
 Last Update: February 3, 2012, 3:28 am ( View Rated (3) ) (View Latest)
Page 1 of 2 pages.
bookmark - Watermark Your Image With Simple Php Script found it on the net  
Quickly Post to Watermark Your Image With Simple Php Script found it on the net w/o signup Share Info about Watermark Your Image With Simple Php Script found it on the net using Facebook, Twitter etc. email your friend about Watermark Your Image With Simple Php Script found it on the net Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print


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:

CODE

<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:

CODE

<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 :lol:

   Mon Apr 24, 2006    Reply         

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

   Mon Apr 24, 2006    Reply         

Do you mean like this?

CODE

<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:

CODE

<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"

   Mon Apr 24, 2006    Reply         


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

I'm just kidding... but it is a very useful find!

   Mon Apr 24, 2006    Reply         

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.

   Mon Apr 24, 2006    Reply         

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!

   Mon Apr 24, 2006    Reply         


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

   Mon Apr 24, 2006    Reply         

Cool. I didnt know that you could do that with a php script.

Thanks for showing it to us, maybe Ill give it a try.

   Mon Apr 24, 2006    Reply         

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?

   Mon Apr 24, 2006    Reply         

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. :-)

   Mon Apr 24, 2006    Reply         

Hi,

I am storing the images in database and the codes is following :

CODE

<?
include "connection.php";

$sql="select image from sportsevent where id='$_GET[id]'";

$rs=mysql_query($sql);

$row=mysql_fetch_object($rs);

header("(anti-spam-(anti-spam-content-type:)) image/jpg");
echo $row->image;
mysql_free_result($rs);
?>


Could you tell me how to watermark images while outputting image in this way???

Thanks

   Sat Jun 23, 2007    Reply         

so this basically covers your image with a watermark using php? cool. might want to try it with the pictures i would use later in my account. thanks a lot!

   Sat Jun 23, 2007    Reply         

Ah good old GD library saves the day again, but I think I'm about to bring up a point that nobody has thought of yet... If you are watermarking your image so nobody else can use the original, all somebody would have to do is look at the url of the image, and tadah, they have the path of the original picture! So much for protecting your images :)

I'd suggest something i bit more secure. But other than that its great!

   Sat Jun 23, 2007    Reply         

No, Images are not saved in file system. they are saved in database

   Sat Jun 23, 2007    Reply         

Oh haha, my bad! :) I saw the example of the path of the image in the url and I thought that it meant it was actually accessing that url to get the image.

   Sat Jun 23, 2007    Reply         

So do you have any idea how to do this ???

   Sat Jun 23, 2007    Reply         

Can you name a php script with a ".jpg" extension and have the server parse it as php? I don't like having the source of my pictures as a php file. I don't know just a thing I have. Doesn't seem right. Seems like a security hole too.

   Sat Jun 23, 2007    Reply         

QUOTE (reconraiders)

Can you name a php script with a ".jpg" extension and have the server parse it as php? I don't like having the source of my pictures as a php file. I don't know just a thing I have. Doesn't seem right. Seems like a security hole too.
Link: view Post: 332427



Hi,

I can do that. But I want to watermark images stored in database while displaying on web.


Gajendra

   Sat Jun 23, 2007    Reply         

will CSS work ??
i think no other way...may be wrong :)

   Sat Jun 23, 2007    Reply         

And you call that simple! LOL! I am a PHP programer and that is a really advance script... I would never have thought to use that ever... Mmmm... good thinking.. thanks for sharing I might just use it... oh yeah need help with php i am ur guy...

   Sun Jun 24, 2007    Reply         

QUOTE (reconraiders)

Can you name a php script with a ".jpg" extension and have the server parse it as php? I don't like having the source of my pictures as a php file. I don't know just a thing I have. Doesn't seem right. Seems like a security hole too.
Link: view Post: 332427


unfortunately you cannot do this without modifying some server files, eh... not sure which ones so don't ask me. There is a much easier way of tricking the user into thinking its a regular jpg image when it's really a php file. Make a new folder on your server called name.jpg and inside of it put your php file, but rename it to index.php. In many cases, on websites with dynamic images, you can add /index.php after the image and it will not result in a 404 error, because they use this.

   Thu Jul 5, 2007    Reply         

QUOTE (alex7h3pr0gr4m3r)

unfortunately you cannot do this without modifying some server files, eh... not sure which ones so don't ask me. There is a much easier way of tricking the user into thinking its a regular jpg image when it's really a php file. Make a new folder on your server called name.jpg and inside of it put your php file, but rename it to index.php. In many cases, on websites with dynamic images, you can add /index.php after the image and it will not result in a 404 error, because they use this.
Link: view Post: 334457



Hi,

Thanks for your reply. but my problem is different. I want to make a watermark image while feching the orignal image from database

   Thu Jul 5, 2007    Reply         

Hi!

This is a fab script...using it now on my site. However is there a way to position the watermark? Currently it displays in the centre, when preferably I would like the image to be displayed in the bottom right corner.

Does anyone know how to do this?

Thanks!
Matt

   Mon Jul 9, 2007    Reply         

QUOTE

$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);


If I read and understand the script correctly, adjusting these start points in the script will affect the positioning of the watermark.
As an example, remove the "/2" in the width and height and see where the watermark displays on the image.
Try it and let us know if it works for you.

   Tue Jul 10, 2007    Reply         

Hi jlhaslip,

Thanks for that, does the trick nicely! 1 and 1 puts it in the bottom right :D

Many thanks,
Matt

   Tue Jul 10, 2007    Reply         

How to create watermark on pdf files in php? can you provide me php script please.............
thank you!

-siva rama krishna

   Fri Oct 12, 2007    Reply         

QUOTE (Trap FeedBacker)

How to create watermark on pdf files in php? can you provide me php script please.............
thank you!

-siva rama krishna
Link: view Post: 353269


I use a pdf creation software named cutepdf to enhance my pdf output. Actually, i have the cutepdf paid version, might be the paid version which has all the nice features, but I think the basic, free version can 'stamp' pdf pages, too.

http://www.cutepdf.com/

   Thu Nov 1, 2007    Reply         

Great! can't wait to try this out! Thank you!

   Thu Nov 1, 2007    Reply         

Hello people thank you for this script and as I made one that works for me for watermarking multiple picture on upload .

<?php

$watermarkPath = '/root_path_to_png_watermark/'.$watermark_name.'.png';
$target = '/root_path_to_where_upload_after_watermarking/'.$uploaded_picture_name;
$quality = "100";
$imagesource = '/root_path_to_where_upload_after_watermarking/'.$uploaded_picture_name;
$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("a murit nu am scos imaginea");

if(!$image)
die("a murit nu avem imagine");

if($watermarkType == ".gif")
$watermark = @imagecreatefromgif($watermarkPath);
else
if($watermarkType == ".png")
$watermark = @imagecreatefrompng($watermarkPath);
else
die("a murit $watermark nu e");

if(!$watermark)
die("a murit lipsa watermark");

$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/to);
$startheight = (($imageheight - $watermarkheight)/to);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($image,$target,$quality);
imagedestroy($image);
imagedestroy($watermark);
?>



Hope this works for you.

-Costin Iordache

   Tue Dec 4, 2007    Reply         

Thanks for posting the code, I am using it on most of my site.
I was wondering if you guys might have an idea on how to pull watermarking off on a href tag. (click a thumbnail to display the large image)

Here is what I tried

CODE

<a href="watermark.php?path=images/image1.jpg"><img src="images/image1_thumbnail.jpg" />


Clicking the link yields the raw gd output similar to this

QUOTE

�JFIF�������>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality �C� $.' ",#(7),01444'9=82<.342�C 2!!22222222222222222222222222222222222222222222222222�y "������������ ����}�!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������� ���w�!1AQaq"2B #3Rbr $4%&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���?�ReI0#L #w̐)DŒu83J�2mC4󀍽hL\/qڬ{hF!


Thanks,

Sean

   Fri Jan 4, 2008    Reply         

Quickly Post to Watermark Your Image With Simple Php Script found it on the net w/o signup Share Info about Watermark Your Image With Simple Php Script found it on the net using Facebook, Twitter etc. email your friend about Watermark Your Image With Simple Php Script found it on the net Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print


Similar Topics:

Very Simple Online Now Script

Hi all, Its Aldo. anyways, I wont be using the method of pagination, i will just tell you how to make a basic online now script. When someone logs in, now take into consideration that the name of the username input is username (<input type.... name=username) First , ...more

   29-Aug-2007    Reply         

Php Help For A Classifieds Website

Ok i am currently helping to setup a new website called Flog-It (its a new classified website). I have a few things i need help with. 1. I need to put the latest ads section which is located at the end of the page in the recent ads box at the top. But the text which is associated with ...more

   02-Jun-2009    Reply         

Php From File To Javascript

Hey All, I'm stuck! I am trying to use PHP variables accessed from an external file in javascript code. I can set the array from the external file. I can get PHP variables to javascript. I can get PHP array variables to javascript. Something goes wrong when ...more

   03-Dec-2009    Reply         

Question About The Mail() Function    Question About The Mail() Function (2) (6) Running Php Web Scripts On A Local Machine   Running Php Web Scripts On A Local Machine