WEB watermarking thumbnails created.

Discussion in 'OT Technology' started by drpepper, Oct 12, 2007.

  1. drpepper

    drpepper Active Member

    Joined:
    Nov 13, 2006
    Messages:
    38,035
    Likes Received:
    23
    Location:
    San Antonio
    how would i go about watermarking thumbnails on the fly with php?
    is it a simple piece of code? any scripts?
     
  2. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    Here's my image class
    Code:
    <?php
    
    
    class img {
    	
    	var $image = '';
    	var $name = '';
    	var $temp = '';
    	
    	function img($sourceFile){
    		if(file_exists($sourceFile)){
    			$filetype = substr($sourceFile,strlen($sourceFile)-4,4);
    			$filetype = strtolower($filetype);
    			if($filetype == ".gif")  $this->image = @imagecreatefromgif($sourceFile);  
    			if($filetype == ".jpg")  $this->image = @imagecreatefromjpeg($sourceFile);  
    			if($filetype == ".png")  $this->image = @imagecreatefrompng($sourceFile);
    			if (!$this->image)	$this->errorHandler("Incorrect file type.");
    			$imagenamearray = explode("/", $sourceFile);
    			$imagename = $imagenamearray[(sizeof($imagenamearray) - 1)];
    			$imagenamearray = explode(".", $imagename);
    			$imagename = $imagenamearray[0];
    			$this->name = $imagename;
    		} else {
    			$this->errorHandler("Image not found.");
    		}
    		return;
    	}
    	
    	function resize($dim = array(100, 100), $aspectradio = true){
    		list($width, $height) = $dim;
    		$o_wd = imagesx($this->image);
    		$o_ht = imagesy($this->image);
    		if(isset($aspectradio)&&$aspectradio) {
    			if ($o_wd > $width || $o_ht > $height) {
    				$w = round($o_wd * $height / $o_ht);
    				$h = round($o_ht * $width / $o_wd);
    			
    				if(($height-$h)<($width-$w)){
    					$width =& $w;
    				} else {
    					$height =& $h;
    				}
    			} else {
    				$width = $o_wd;
    				$height = $o_ht;
    			}
    		}
    		$this->temp = imagecreatetruecolor($width,$height);
    		imagecopyresampled($this->temp, $this->image,
    		0, 0, 0, 0, $width, $height, $o_wd, $o_ht);
    		$this->sync();
    		return;
    	}
    	
    	function sync(){
    		$this->image =& $this->temp;
    		unset($this->temp);
    		$this->temp = '';
    		return;
    	}
    	
    	function show(){
    		$this->_sendHeader();
    		if (!$this->image)	$this->errorHandler("An error occured while attempting to display this image.");
    		imagejpeg($this->image, NULL, 95);
    		imagedestroy($this->image);
    		unset($this->name);
    		return;
    	}
    	
    	function _sendHeader(){
    		header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
    		header('Content-type: image/jpeg');
    		header('Content-Disposition: inline; filename='.$this->name);
    	}
    	
    	function errorHandler($eString = "Error"){
    		// we may want to add some type of error recording here
    		echo $eString;
    		exit();
    	}
    	
    	function store($file){
    		imagejpeg($this->image, NULL, 95);
    		imagedestroy($this->temp);
    		imagedestroy($this->image);
    		unset($this->name);
    		return;
    	}
    	
    	function watermark($pngImage, $left = 0, $top = 0){
    		if (!$this->image)	$this->errorHandler("This image could not be watermarked successfully.");
    		$filetype = substr($pngImage,strlen($pngImage)-4,4);
    		$filetype = strtolower($filetype);
    		if($filetype == ".gif")  $layer = @imagecreatefromgif($pngImage);  
    		if($filetype == ".jpg")  $layer = @imagecreatefromjpeg($pngImage);  
    		if($filetype == ".png")  $layer = @imagecreatefrompng($pngImage);
    		if (!$layer) $this->errorHandler("Incorrect watermark filetype.");
    		$logoW = imagesx($layer); 
    		$logoH = imagesy($layer); 
    		imagecopymerge($this->image, $layer, $left, $top, 0, 0, $logoW, $logoH, 100); 
    	}
    } // END img CLASS
    
    ?>
    
    
    and then

    Code:
    $img = new img($SITE_URL_IMAGES_SHORT.($_GET['p'] ? $_GET['p'] : $SITE_URL_DEFAULT_IMG));
    $img->watermark($SITE_URL_WATERMARK);
    $img->save($SAVE_PATH);
    //$img->show();
    
    You will need the latest GD library installed with your PHP
     

Share This Page