|
|
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. | ||
![]() |
|
|
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
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"
I'm just kidding... but it is a very useful find!
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.
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!
here are some example links
htaccess version
gd support
Thanks for showing it to us, maybe Ill give it a try.
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?
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
I'd suggest something i bit more secure. But other than that its great!
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
i think no other way...may be wrong
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.
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
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
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.
Thanks for that, does the trick nicely! 1 and 1 puts it in the bottom right
Many thanks,
Matt
thank you!
-siva rama krishna
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/
<?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
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̐)Du83J�2mC4hL\/qڬ{hF!Thanks,
Sean
Similar Topics:
Very Simple Online Now Script
Php Help For A Classifieds Website
Php From File To Javascript
Question About The Mail() Function (2)
|
(6) Running Php Web Scripts On A Local Machine
|
Loading...
HOME 





Converting Videos Howto - FFMPEG
Dreamweaver Tutorial : How to Add a Background Image to a Web Page With Dreamweaver
The YouTube API: Upload, Player APIs and more!
Joomla 1.5 Tutorial - Creating a gallery using Phoca Gallery
YouTube Player APIs and Tools Tutorial
Dynamic header in Artisteer generated Joomla templates
YouTube's API and The News

