Location: PHPKode > scripts > Image Manipulation > image-manipulation/image.class.php
<?
class Image
{
	var $AllowedExtensions = array(".jpg",".gif",".png");
	var $JpegQuality = 100;
	var $ImageUrl;
	var $ImageObj;
	var $ImageType;
	var $ResizedImage;
	
	function Image($Image)
	{
		if($this->IsImage($Image))
		{
			$this->GetImageType($Image);
			$this->ImageUrl = $Image;
			$this->Create();	
			return true;
		}
		else
		{return false;}
	}
	
	function __destruct()
	{
		if($this->IsImage($this->ImageUrl))
		{
			imagedestroy($this->ImageObj);
			if(!empty($this->ResizedImage))
			{imagedestroy($this->ResizedImage);}
		}
	}
	
	function Create()
	{
		switch($this->ImageType)
		{
			case ".gif":
				$this->ImageObj = imagecreatefromgif($this->ImageUrl);			
			break;
			
			case ".jpg":
				$this->ImageObj = imagecreatefromjpeg($this->ImageUrl);
			break;
			
			case ".png":
				$this->ImageObj = imagecreatefrompng($this->ImageUrl);
			break;
		}
	}
	
	function Resize($Percent)
	{
		// Get new dimensions
		list($Width, $Height) = getimagesize($this->ImageUrl);
		$NewWidth = intval(($Width * $Percent) / 100);
		$NewHeight = intval(($Height * $Percent) / 100);
		
		$this->ResizedImage = imagecreatetruecolor($NewWidth, $NewHeight);
		imagecopyresampled($this->ResizedImage, $this->ImageObj, 0, 0, 0, 0, $NewWidth, $NewHeight, $Width, $Height);
		switch($this->ImageType)
		{
			case ".gif":
				imagegif($this->ResizedImage,$this->ImageUrl);
			break;
			
			case ".jpg":
				imagejpeg($this->ResizedImage,$this->ImageUrl,$this->JpegQuality);
			break;
			
			case ".png":
				imagepng($this->ResizedImage,$this->ImageUrl);
			break;
		}
	}
	
	function ResizeWH($W,$H)
	{
		if($this->IsImage($this->ImageUrl))
		{
			// Get new dimensions
			list($Width, $Height) = getimagesize($this->ImageUrl);		
			$NewWidth = $W;
			$NewHeight = $H;
			
			$this->ResizedImage = imagecreatetruecolor($NewWidth, $NewHeight);
			imagecopyresampled($this->ResizedImage, $this->ImageObj, 0, 0, 0, 0, $NewWidth, $NewHeight, $Width, $Height);
			switch($this->ImageType)
			{
				case ".gif":
					imagegif($this->ResizedImage,$this->ImageUrl);
				break;
				
				case ".jpg":
					imagejpeg($this->ResizedImage,$this->ImageUrl,$this->JpegQuality);
				break;
				
				case ".png":
					imagepng($this->ResizedImage,$this->ImageUrl);
				break;
			}
		}
	}
	
	function IsImage($File)
	{
		if($this->ImageExists($File))
		{
			$Extensions = implode(",",$this->AllowedExtensions);
			
			$Extension = strtolower(substr ($File, strrpos ($File, ".")));
			$this->Extension = $Extension;
			return $Extension;
			
			$this->AllowedExtensions = explode(",",strtolower($Extensions));
			if(!empty($this->AllowedExtensions))
			{
				if(!in_array($this->GetExtension(),$this->AllowedExtensions))
				{return false;}
				else
				{return true;}
			}
			else
			{return true;}
		}
		else
		{
			return false;
		}
	}
	
	function ImageExists($Image)
	{
		if(file_exists($Image))
		{return true;}
		else
		{return false;}
	}
	
	function GetImageType($Image)
	{
		if($this->IsImage($Image))
		{
			$Extension = strtolower(substr ($Image, strrpos ($Image, ".")));
			switch($Extension)
			{
				case ".gif":
					$this->ImageType = ".gif";
				break;
				
				case ".jpg":
					$this->ImageType = ".jpg";
				break;
				
				case ".png":
					$this->ImageType = ".png";
				break;
				
				default:
					return false;
				break;
			}
		}
	}
}
?>
Return current item: Image Manipulation