<?php
/* ver. 1.0
** Requires GD Library (constructor checks for GD and returns error if not found)
Description:
Available functions:
*/

// Check if GD is available and exit if not
		if (!extension_loaded('gd') || !function_exists('gd_info')){ exit("<strong>Fatal error:</strong> GD Library is not installed on this server. You can not use ImageResizeGD class."); }

class ImageEditGD {
	var $Filename,$Basename,$Path,$FullPath,$Width,$Height,$Size,$Extension,$Mimetype,$AltText,$RealPath,$DisplayPath;
	
	// Constructor
	/*
	Creating new ImageResizeGD image: create with reference to existing image: ImageResizeGD("../usr/image/prod/img.jpg")
	Find image info and saves them in filename, path, width, height, size, extension
	*/
	function ImageEditGD($nOrigImagePath){
		// set defaults
		$this->FullPath = $this->DisplayPath = $nOrigImagePath;
		$this->AltText = "";
		$this->ResizePath = "";
		$this->SetProperties();
	}
	
	// Class functions
	function SetProperties(){
		if(file_exists($this->FullPath) && is_file($this->FullPath)){
			$nImgSizeARRAY = getimagesize($this->FullPath);
			$nImgInfoARRAY = pathinfo($this->FullPath);
			$this->Width = $nImgSizeARRAY[0];
			$this->Height = $nImgSizeARRAY[1];
			$this->Mimetype = $nImgSizeARRAY["mime"];
			$this->Size = filesize($this->FullPath);
			$this->Extension = strtolower($nImgInfoARRAY["extension"]);
			$this->Filename = $nImgInfoARRAY["filename"];
			$this->Basename = $nImgInfoARRAY["basename"];
			$this->Path = $nImgInfoARRAY["dirname"];
			$this->RealPath = realpath($this->FullPath);
		} else {
			// exit("<strong>Fatal error:</strong> File referenced (".$this->FullPath.") does not exist.");
		}
	}
	function GetTagHTML($nClass="",$nStyle=""){
		$nImgFile = $this->RealPath;
		if($nImgFile && file_exists($nImgFile) && is_file($nImgFile)){ $nImgTagARRAY = getimagesize($nImgFile); }
		$nTag = "<img src=\"{$this->DisplayPath}\" alt=\"{$this->AltText}\" {$nImgTagARRAY[3]} border=\"0\" class=\"$nClass\" style=\"$nStyle\">";
		return $nTag;
	}
	function SetResizePath($nPath){
		// Set path, create it if necessary... if path can't be set/created and validated, return false
		if(!file_exists($nPath)){ mkdir($nPath); }
		if(file_exists($nPath)){ $this->ResizePath = "$nPath"; } else { return false; }
		return true;
	}
	function Resize($nMaxWidth,$nMaxHeight,$nQuality=80){
		$source_image_path = $this->Path."/".$this->Basename;
		if(!file_exists($source_image_path) || !is_file($source_image_path)){ return false; }
		if($this->ResizePath && file_exists($this->ResizePath)){
			$thumbnail_image_path = $this->ResizePath.$this->Basename;
		} else {
			exit("No valid resize path set for image.");
		}
		$nRemoveTempImg = false;
		// Check if resize already exists, and if so, return
		if(file_exists($thumbnail_image_path) && is_file($thumbnail_image_path)){
			$nImgSizeARRAY = getimagesize($thumbnail_image_path);
			if($nMaxWidth==$nImgSizeARRAY[0] || $nMaxHeight==$nImgSizeARRAY[1]){
				// Update class properties with old resized image
				$this->FullPath = $this->DisplayPath = $thumbnail_image_path;
				$this->SetProperties();
				return true;
			}
		}
		list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $source_image_path );
	    // Get correct dimensions
		$source_aspect_ratio = $source_image_width / $source_image_height;
	    $thumbnail_aspect_ratio = $nMaxWidth / $nMaxHeight;
	    if($source_image_width <= $nMaxWidth && $source_image_height <= $nMaxHeight){
	      $nMaxWidth = $source_image_width;
	      $nMaxHeight = $source_image_height;
	    } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio){
	      $nMaxWidth = (int)($nMaxHeight * $source_aspect_ratio);
	    } else {
	      $nMaxHeight = (int)($nMaxWidth / $source_aspect_ratio);
	    }
	    // Create new image
		switch($source_image_type){
	      case IMAGETYPE_GIF:
			$thumbnail_gd_image = imagecreatetruecolor( $nMaxWidth, $nMaxHeight );
			imagealphablending( $thumbnail_gd_image, false );
			imagesavealpha( $thumbnail_gd_image, true );
			$source_gd_image = imagecreatefromgif($source_image_path); if($source_gd_image === false){ return false; }
			imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $nMaxWidth, $nMaxHeight, $source_image_width, $source_image_height);
			$nPNGQuality = round(($nQuality/10))-1;
			imagepng($thumbnail_gd_image, $thumbnail_image_path, $nPNGQuality);
	        break;
	      case IMAGETYPE_JPEG:
	        $source_gd_image = imagecreatefromjpeg($source_image_path); if($source_gd_image === false){ return false; }
			$thumbnail_gd_image = imagecreatetruecolor( $nMaxWidth, $nMaxHeight );
			imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $nMaxWidth, $nMaxHeight, $source_image_width, $source_image_height );
	    	imagejpeg( $thumbnail_gd_image, $thumbnail_image_path, $nQuality );
	        break;
	      case IMAGETYPE_PNG:
			$thumbnail_gd_image = imagecreatetruecolor( $nMaxWidth, $nMaxHeight );
			imagealphablending( $thumbnail_gd_image, false );
			imagesavealpha( $thumbnail_gd_image, true );
			$source_gd_image = imagecreatefrompng($source_image_path); if($source_gd_image === false){ return false; }
			imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $nMaxWidth, $nMaxHeight, $source_image_width, $source_image_height);
			$nPNGQuality = round(($nQuality/10))-1;
			imagepng($thumbnail_gd_image, $thumbnail_image_path, $nPNGQuality);
	        break;
	    }
	    imagedestroy( $source_gd_image );
	    imagedestroy( $thumbnail_gd_image );
		if($nRemoveTempImg){ unlink( $source_image_path ); }
	    // Update class properties with newly resized image
		$this->FullPath = $this->DisplayPath = $thumbnail_image_path;
		$this->SetProperties();
		return true;
	}
} // end class
?>