All pastes #395139 Raw Edit

Someone

public text v1 · immutable
·published 2007-03-14 20:43 UTC
rendered paste body
<?php

// ----------------- ~ Tango ~ ----------------- \\
// File: kernel/lib/libimage.php
// Author: Alex Cartwright
// Created: 13th September 2006
// ----------------- ~ Tango ~ ----------------- \\

	class libimage {

		var $error;
		var $fileName;
		var $fullPath;
		var $sourceImage;
		
		var $imageX;
		var $imagey;
		var $imageType;		
		
		//---
		// openImage will open an image to be used for further functions
		// $filePath is the directory to the file
		//---
		function openImage( $filePath ) {
		
			// Check the GD Libary is enabled
			if ( !extension_loaded( 'gd' ) ) {				
				$this->error = 'You must have the GD Libary installed, please contact your system administrator';
				return $this->error;
			}
			
			// Check the file exists
			if ( file_exists( $filePath ) ) {
						
				list( $this->imageX, $this->imageY, $this->imageType ) = getimagesize( $filePath );
				
				// Get the source image
				switch ( $this->imageType ) {		              
	                case "2": $this->sourceImage = imagecreatefromjpeg( $filePath ); break;
	                case "3": $this->sourceImage = imagecreatefrompng( $filePath ); break;
	                default: 
							$this->error = 'Unable to create source image, please select a jpg/jpeg or png file';						
	                		return $this->error;
	            }				
				
				// Check we have a source image
				if ( !$this->sourceImage ) {			
					$this->error .= 'Unable to create source image';
					return $this->error;					
				} else {					
					// Get Width and Height
					$this->imageX = imagesx( $this->sourceImage );
					$this->imageY = imagesy( $this->sourceImage );										
				}
			
			} else {			
				$this->error = "Could not open image, file doesn't exist: ".$filePath;
				return $this->error;
			}			
		
		}
		
		//---
		// saveImage will attempt to save the new image - and overwrite old one if needed
		// $image is the Image Resource
		// $pathTo - the Path where the image will be saved
		//---
		function saveImage( $image, $pathTo ) {
		
			$this->saveImage = $image;
			
			// Check if the file exists, if so - delete it
			if ( file_exists( $pathTo ) ) {
				unlink( $pathTo );
			}
			
			// Save image
			switch ( $this->imageType ) {			         
				case "2": imagejpeg( $this->saveImage, $pathTo ); break;
				case "3": imagepng( $this->saveImage, $pathTo ); break;
			}
						
		}
		
		//---
		// destroy will destroy an image
		// ---
		function destroy( $image ) {
			imagedestroy( $image );
		}
		
		//---
		// thumnail is very similar to resize, but with a thumbnail you usualy specify exact Width and Height Sizes
		// The problem with this is that the image could then look distorted. the thumbnail() function will make sure
		// that the image does not look distorted buy scaling the image first - then placing it in a black box of the 
		// requested size.	
		//
		// $pathTo - the full path ( Excluding file name ) to where the new image should be saved
		// $fileName - the filename the file should be saved as	
		// $width - the width of the new image
		// $height - the height of the new image
		//---  
		function thumbnail( $pathTo, $fileName, $width='80', $height='80' ) {
		
			// Check we have the GD libary
			if ( !extension_loaded( 'gd' ) ) {
				$this->error = 'You must have the GD libary installed, please contact your system administrator';
				return $this->error;				
			} else {
					
				// Scale the actual image so it will still look in proportion for the thumbnail
				if ( $this->imageY > $this->imageX ) {
					// The width is now the smallest axis
					$newHeight = $height;					
					$newWidth = ( $height / $this->imageY ) * $this->imageX;
					
					// Work out how much Black Padding we will need either side of the image ( Hence the / 2 )
					$paddingWidth = ( $width - $newWidth ) / 2;	
					$paddingHeight = 0;				
				} else if ( $this->imageX > $this->imageY ) {
					// The height is now smallest axis
					$newWidth = $width;
					$newHeight = ( $width / $this->imageX ) * $this->imageY;
					
					// Work out how much Black Padding we will need at the Top/Bottom of the image ( Hence the / 2 )
					$paddingHeight = ( $height - $newHeight ) / 2;
					$paddingWidth = 0;					
				}				
				
				// Create the new image with the specified Height/Widths				
				if ( !$newImage = ImageCreateTrueColor( $width, $height ) ) {						
					$this->error = 'Unable to create True Color';
					return $this->error;
				} else {
							
					if ( !imagecopyresampled( $newImage, $this->sourceImage, $paddingWidth,$paddingHeight+1,0,0, $newWidth, $newHeight, $this->imageX, $this->imageY ) ) {					
						$this->error='Unable to copy image with resampling';
						return $this->error;
					} else {									
						// Save the image
						$this->saveImage( $newImage, $pathTo.'thumb_'.$fileName );
					}
					
				}			
				
			}		
			
		}		
	
		//---
		// resize will resize an image to either specific widths/heights or it will scale it
		// depending on which values are given.
		//
		// $pathTo - the full path ( Excluding file name ) to where the new image should be saved
		// $fileName - the filename the file should be saved as
		// $prefix - attatch a prefix to the filename beforesaving
		// $width - the width of the new image
		// $height - the height of the new image
		//---  
		function resize( $pathTo, $fileName, $prefix='', $width='auto', $height='auto' ) {
		
			// Check we have the GD libary
			if ( !extension_loaded( 'gd' ) ) {
				$this->error = 'You must have the GD libary installed, please contact your system administrator';
				return $this->error;				
			} else {
					
				// Resize the image				
				if ( $width == 'auto' && $height == 'auto' ) {
					// If they are both set to Auto, keep the original size
					$newWidth = $this->imageX;
					$newHeight = $this->imageY;
				} else if ( $width != 'auto' && $height != 'auto' ) {
					// An Exact size is specified					
					$newWidth = $width;
					$newHeight = $height;														
				} else if ( $width == 'auto' ) {				
					$newHeight = $height;					
					$newWidth = ( $height / $this->imageY ) * $this->imageX;				
				} else if ( $height == 'auto' ) {
					$newWidth = $width;					
					$newHeight = ( $width / $this->imageX ) * $this->imageY;
				} else {
					$newWidth = $width;
					$newHeight = $height;		
				}
				
				// Create the new image				
				if ( !$newImage = ImageCreateTrueColor( $newWidth, $newHeight ) ) {						
					$this->error = 'Unable to create True Color';
					return $this->error;
				} else {
							
					if ( !imagecopyresampled( $newImage, $this->sourceImage, 0,0,0,0, $newWidth, $newHeight, $this->imageX, $this->imageY ) ) {					
						$this->error='Unable to copy image with resampling';
						return $this->error;
					} else {									
						// Save the image
						$this->saveImage( $newImage, $pathTo.$prefix.$fileName );
					}
					
				}			
				
			}		
			
		}
		
		//---
		// addInfo will attempt to add a black bar to the bottom of an image, then dd white text on it left justified
		// $fileName is the absolute path to the file, so we know which file to replace
		// $text is the text to be added to the notice bar
		//---
		function addInfo( $fileName, $text ) {
		
			// Check we have the GD libary
			if ( !extension_loaded( 'gd' ) ) {
				$this->error = 'You must have the GD libary installed, please contact your system administrator';
				return $this->error;				
			} else {
			
				// Make sure the file we need to attatch it to exists
				if ( !file_exists( $fileName ) ) {
					$this->error = 'File is missing or invalid';
					return $this->error;					
				} else {
					
					// Create a new image, but 20 pixels more in height for the black bar
					$newImage = imageCreateTrueColor( $this->imageX, $this->imageY + 20 );
									
					// Generate Colors
					$black = imageColorAllocate( $newImage, 0,0,0 );
					$white = imageColorAllocate( $newImage, 255,255,255 ); 
					
					imagefill( $newImage, 0, 0, $black );
					imagecopy( $newImage, $this->sourceImage, 0,0,0,0, $this->imageX, $this->imageY );
					
					// Add the text
					imagettftext( $newImage, 8, 0, 5 , $this->imageY + 14, $white, 'kernel/lib/verdana.ttf', $text );					
					
					// Save the new image
					$this->saveImage( $newImage, $fileName );					
					
				}
				
			}	
		
		}
		
	}

?>