Guest
Public paste!

PHP Watermark Script

By: a guest | Mar 30th, 2010 | Syntax: PHP | Size: 2.67 KB | Hits: 150 | Expires: Never
Copy text to clipboard
  1. <?php
  2.     // this script creates a watermarked image from an image file - can be a .jpg .gif or .png file
  3.     // where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory as this script
  4.     // where this script is named watermark.php
  5.     // call this script with an image tag
  6.     // <img src="watermark.php?src=imagepath"> where path is a relative path such as subdirectory/image.jpg
  7.  
  8.     $imagesource =  $_GET['src'];
  9.    
  10.         // this is where your sql login details go.
  11.         $dbserver = 'localhost';
  12.         $dbuser = '';
  13.         $dbpass = '';
  14.         $dbname = '';
  15.        
  16.                 /*
  17.                
  18.                 ##### CREATE THIS TABLE FIRST: #####
  19.                
  20.                
  21.                 CREATE TABLE IF NOT EXISTS `HotlinkTracking` (
  22.                   `date` datetime NOT NULL,
  23.                   `referrer` text NOT NULL,
  24.                   `image` text NOT NULL
  25.                 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  26.                
  27.                
  28.                 */     
  29.        
  30.         $link = mysql_connect($dbserver, $dbuser, $dbpass) or die(mysql_error());
  31.         mysql_select_db($dbname) or die(mysql_error());
  32.        
  33.     $mysqldate = date( 'Y-m-d H:i:s' );
  34.     $referrer = $_SERVER['HTTP_REFERER'];
  35.         mysql_query("INSERT INTO HotlinkTracking
  36.         (referrer, date, image) VALUES('{$referrer}', '{$mysqldate}','{$imagesource}' ) ")
  37.         or die(mysql_error());
  38.  
  39.     $watermarkPath = 'watermark.png';
  40.     $filetype = substr($imagesource,strlen($imagesource)-4,4);
  41.     $filetype = strtolower($filetype);
  42.     $watermarkType = substr($watermarkPath,strlen($watermarkPath)-4,4);
  43.     $watermarkType = strtolower($watermarkType);
  44.    
  45.     if($filetype == ".gif")  
  46.         $image = @imagecreatefromgif($imagesource);
  47.     else  
  48.         if($filetype == ".jpg" || $filetype == "jpeg")  
  49.             $image = @imagecreatefromjpeg($imagesource);
  50.         else
  51.             if($filetype == ".png")  
  52.                 $image = @imagecreatefrompng($imagesource);
  53.             else
  54.                 die();  
  55.    
  56.     if(!$image)
  57.         die();
  58.    
  59.     if($watermarkType == ".gif")
  60.         $watermark = @imagecreatefromgif($watermarkPath);
  61.     else
  62.         if($watermarkType == ".png")
  63.             $watermark = @imagecreatefrompng($watermarkPath);
  64.         else
  65.             die();
  66.        
  67.     if(!$watermark)
  68.         die();
  69.        
  70.     $imagewidth = imagesx($image);
  71.     $imageheight = imagesy($image);  
  72.     $watermarkwidth =  imagesx($watermark);
  73.     $watermarkheight =  imagesy($watermark);
  74.     //$startwidth = (($imagewidth - $watermarkwidth)/2);
  75.     //$startheight = (($imageheight - $watermarkheight)/2);
  76.     $startwidth = 0;
  77.     $startheight = 0;
  78.     imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
  79.     imagejpeg($image);
  80.     imagedestroy($image);
  81.     imagedestroy($watermark);
  82. ?>