Location: PHPKode > scripts > Process Images > process-images/z-process.php
<?php

	/**
	 * Image resizing and web page creation library
	 * @package HermanGomez.com
	 * @author Herman Gomez
	 * @version 1.0.0 4-Jan-2008
	 * @copyright email to hide@address.com	 
	 */
	class ProcessImages {
		
		/**
		 * Reads the folder contents and return the list of images found (.gif, .jpg, .jpeg) - case insensitive
		 * @return Array
		 */
		private static function get_image_list() {			
			$files = array();
			if ($handle = opendir('.')) {			
			    while (FALSE !== ($file = readdir($handle))) {
			    	$extension = strtolower(ProcessImages::get_file_extension($file));
			    	switch($extension) {
			    		case ".jpg":
			    		case ".jpeg":
			    		case ".gif":
			    			$files[] = $file; break;
			    	}			        
			    }						
			    closedir($handle);
			}			
			return $files;			
		}
		
		/**
		 * Calculates the percent or reduction.
		 * @param Int $width
		 * @param Int $height
		 * @return Real percent
		 */
		private static function calculate_percent($width,$height) {			
			$total = $width * $height;
			
			$w = $width / 124;
			$h = $height / 72;
			if ($w < $h) {
				$times = $w;
			} else {
				$times = $h;
			}
			
			return 1 / $times;
		}
		
		/**
		 * Reduce a JPEG image. 
		 * The ouput file will have the same name plus the extension '.resize.<ext>' where <ext> will be the same as the original extension.
		 * @param String $path
		 * @param Real $percent ( == 0 --> calculate reduce percent automaticaly. > 0 --> it will use the given percent. example: 0.1 = 10%)
		 */
		private static function resize_jpg($path, $percent = 0) {
									
			// Get new sizes
			list($width, $height) = getimagesize($path);
			
			//calculate percent
			if (0 >= $percent) $percent = ProcessImages::calculate_percent($width,$height);
			
			//calculate new size
			$newwidth 	= $width * $percent;
			$newheight 	= $height * $percent;
			
			// Load
			$temp 	= imagecreatetruecolor($newwidth, $newheight);
			$source = imagecreatefromjpeg($path);
			
			// Resize
			imagecopyresized($temp, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
			
			// Output
			$extension = ProcessImages::get_file_extension($path);
			imagejpeg($temp,"$path.resized" . $extension);
		}
		
		/**
		 * Reduce a GIF image. 
		 * The ouput file will have the same name plus the extension '.resize.<ext>' where <ext> will be the same as the original extension.
		 * @param String $path
		 * @param Real $percent ( == 0 --> calculate reduce percent automaticaly. > 0 --> it will use the given percent. example: 0.1 = 10%)
		 */
		private static function resize_gif($path, $percent = 0) {
			
			// Get new sizes
			list($width, $height) = getimagesize($path);
			
			//calculate percent
			if (0 >= $percent) $percent = ProcessImages::calculate_percent($width,$height);
			
			//calculate new size
			$newwidth 	= $width * $percent;
			$newheight 	= $height * $percent;
			
			// Load
			$temp 	= imagecreatetruecolor($newwidth, $newheight);
			$source = imagecreatefromgif($path);
			
			// Resize
			imagecopyresized($temp, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
			
			// Output
			$extension = ProcessImages::get_file_extension($path);
			imagegif($temp,"$path.resized" . $extension);
		}
		
		/**
		 * return the file extension (including the last dot. example: .JPEG, .gif)
		 * A file can have no extension, so it will return "". 
		 * A file can be like 'name.', so it will return "." (the dot).
		 * @param String $path
		 * @return String extension including the last dot, or "" if no extension..
		 */
		static private function get_file_extension($path) {			
			$i = strrpos($path,".");
			if (FALSE === $i) return NULL;
			return substr($path,$i);
		}
		
		/**
		 * Resize the given image file list.
		 * The ouput files will have the same name plus the extension '.resize.<ext>' where <ext> will be the same as the original extension.
		 * Important: ANY IMAGE NO JPEG OR JPG WILL BE PROCESSED AS GIF.
		 * @param Array $files
		 * @param Real $percent ( == 0 --> calculate reduce percent automaticaly. > 0 --> it will use the given percent. example: 0.1 = 10%)
		 */
		static public function resize_images($files = array(), $percent = 0) {
			foreach($files as $file) {
				echo "Resizing $file<br>\n";
		    	$extension = strtolower(ProcessImages::get_file_extension($file));
		    	switch($extension) {
		    		case ".jpg":
		    		case ".jpeg":
		    			ProcessImages::resize_jpg($file,$percent); break;
		    		default: //case ".gif":
		    			ProcessImages::resize_gif($file,$percent);
		    	}			        				
			}
			echo "Images has been resized.<br>\n";
		}

		/**
		 * Writes a line to the given file plus newline and linefeed.
		 * @param Variant $handle
		 * @param String $line
		 */
		static private function write_file($handle,$line) {
		    if (fwrite($handle, "$line\r\n") === FALSE) {
		        echo "Cannot write to file."; exit;
		    }
		}

		/**
		 * Creates a web page with the given image file list.
		 * This routine add my special standard for this kind of pages, but you can change it to anything you need.
		 * @param Array $files (string path)
		 * @param String $path (output web page name, default = "new_page.inc.php")
		 */
		static public function create_page($files = array(), $path = "new_page.inc.php") {
					
			//unlink($path);
			if (!$handle = fopen($path, 'w')) {
		         echo "Cannot open file ($path)"; exit;
		    }

			//I use a table to sort the images, but maybe you will want to change it.
			ProcessImages::write_file($handle,"<html>");
			ProcessImages::write_file($handle,"<head>");
			ProcessImages::write_file($handle,"<title>Untitled Document</title>");
			ProcessImages::write_file($handle,"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">");
			ProcessImages::write_file($handle,"<link rel=\"stylesheet\" href=\"css/style.css\" type=\"text/css\">");
			ProcessImages::write_file($handle,"</head>");
			ProcessImages::write_file($handle,"<body bgcolor=\"#FFFFFF\" text=\"#000000\">");
			ProcessImages::write_file($handle,"<!--separator1-->"); //maybe you won't need this
			ProcessImages::write_file($handle,"<p>(((Comment)))</p>");			
			ProcessImages::write_file($handle,"<table width=\"500\" border=\"1\">");			
											
			foreach($files as $file) {
			
				echo "Adding $file<br>\n";				
				$extension = ProcessImages::get_file_extension($file);						
				$resized = "$file.resized" . $extension;
				list($width, $height) = getimagesize($resized);
				
				//I use a table to sort the images, but maybe you will want to change it.
				ProcessImages::write_file($handle,"<tr>");					
				ProcessImages::write_file($handle," <td>");
				ProcessImages::write_file($handle,"  <div align=\"center\">");
				ProcessImages::write_file($handle,"   <p><a href=\"./$file\" target=\"photo\"><img src=\"./$resized\" width=\"$width\" height=\"$height\" border=\"0\"></a></p>"); //maybe you'll prefer to change this
				ProcessImages::write_file($handle,"   <p>(((Titulo)))</p>");
				ProcessImages::write_file($handle,"  </div>");
				ProcessImages::write_file($handle," </td>");
				
				//useful for me, but maybe you won't use this two blank cells or you can change the code to fill it automaticaly.
				ProcessImages::write_file($handle," <td>"); 
				ProcessImages::write_file($handle,"  <div align=\"center\">");
				ProcessImages::write_file($handle,"   <p>&nbsp;</p>");
				ProcessImages::write_file($handle,"  </div>");
				ProcessImages::write_file($handle," </td>");
				ProcessImages::write_file($handle," <td>");
				ProcessImages::write_file($handle,"  <div align=\"center\">");
				ProcessImages::write_file($handle,"   <p>&nbsp;</p>");
				ProcessImages::write_file($handle,"  </div>");
				ProcessImages::write_file($handle," </td>");
				
				ProcessImages::write_file($handle,"</tr>");					
			}

			ProcessImages::write_file($handle,"</table>");
			ProcessImages::write_file($handle,"<!--separator2-->"); //maybe you won't need this
			ProcessImages::write_file($handle,"</body>");
			ProcessImages::write_file($handle,"</html>");
		    fclose($handle);			
		    echo "Page has been created.<br>\n";
		}
		
		/**
		 * Runs the image reducing and web page creation processes.
		 * @param Boolean $do_resize (true to run the image resizing process)
		 * @param Boolean $do_create (true to run the web page creation process)
		 * @param Real $percent ( == 0 --> calculate reduce percent automaticaly. > 0 --> it will use the given percent. example: 0.1 = 10%)
		 * @param String $path (output web page name, default = "new_page.inc.php")
		 */
		static public function process_images($do_resize = TRUE, $do_create = TRUE, $path = "new_page.inc.php",$percent = 0) {
			$files = ProcessImages::get_image_list();
			if ($do_resize) ProcessImages::resize_images($files,$percent);
			if ($do_create) ProcessImages::create_page($files,$path);
			echo "Done.";			
		}
		
		/**
		 * Runs only the image resizing process with the calculated percent.
		 * @param String $path (output web page name, default = "new_page.inc.php")
		 */
		static public function only_images($path = "new_page.inc.php") {
			ProcessImages::process_images(FALSE,TRUE,$path);
		}
		
		/**
		 * Runs only the image resizing process.
		 * @param Real $percent ( == 0 --> calculate reduce percent automaticaly. > 0 --> it will use the given percent. example: 0.1 = 10%)
		 */
		static public function only_page($percent = 0) {
			ProcessImages::process_images(TRUE,FALSE,"",$percent);
		}
	
	} //end of class ProcessImages
	
	//running
	//ProcessImages::process_images();

?> 
Return current item: Process Images