Location: PHPKode > scripts > Image Uploader and Resizer > image-uploader-and-resizer/ImageUploader.php
<?php
if (!defined("PATH")) {define("PATH", "../");}
/** Class::ImageUploader
	* @author Feighen Oosterbroek
	* @author hide@address.com
	* @copyright 2008 onwards Feighen Daniel Oosterbroek ta Noon Day Sun Programming
	* @license Creative Commons BSD license
	* @license http://creativecommons.org/licenses/BSD/ -> Summary License
	* @license http://opensource.org/licenses/bsd-license.php
*/
class ImageUploader {
	//: Variables
	protected $dir;
	protected $src;
	protected $dest;
	protected $thumb = true;
	protected $config = array(
		'std'=>array('wide'=>array(0=>450, 1=>330), 'long'=>array(0=>330, 1=>450)),
		'thumb'=>array('wide'=>array(0=>150, 1=>100), 'long'=>array(0=>100, 1=>150))
	);
	
	//: Public Functions
	/** __construct($opts)
		* @param $opts[dir] string destination directory
		* @param $opts[src] string src input file
		* @param $opts[dest] string destination file name
		* @param $opts[thumb] bool whether a thumbnail is created
		* @return true on success or false otherwise
	*/
	public function __construct($opts) {
		foreach ($opts as $key=>$val) {$this->$key = $val;}
		
		# we need to check to see if this directory exists and create it if necessary
		if (!is_dir($this->dir)) {mkdir($this->dir, 0777);}
		
		if ((move_uploaded_file($this->src, $this->dir.$this->dest)) === false) {
			return false;
		}
		# we need to get the image file extension
		$ext = substr($this->dest, strrpos($this->dest, ".")+1);
		switch (strtolower($ext)) {
			case "gif":
			$in = "imagecreatefromgif";
			$out = "imagegif";
			break;
			case "jpg":
			case "jpe":
			case "jpeg":
			$in = "imagecreatefromjpeg";
			$out = "imagejpeg";
			break;
			case "png":
			$in = "imagecreatefrompng";
			$out = "imagepng";
			break;
		}
		if (!function_exists($in)) {throw new Exception('GD extensions are not loaded'); return false;}
		# we now need to do the actual work
		$src = $in($this->dir.$this->dest);
		list($width, $height) = getimagesize($this->dir.$this->dest);
		# uploaded image
		if ($width > $height) {$split = $this->config['std']['wide'];} else {$split = $this->config['std']['long'];}
		$tmp = imagecreatetruecolor($split[0], $split[1]);
		imagecopyresampled($tmp, $src, 0, 0, 0, 0, $split[0], $split[1], $width, $height);
		$out($tmp, $this->dir.$this->dest, 100);
		# clean up
		imagedestroy($tmp);
		if ($this->thumb) {
			# the thumbnail
			if ($width > $height) {
				$tmp = imagecreatetruecolor($this->config['thumb']['wide'][0], $this->config['thumb']['wide'][1]);
				imagecopyresampled($tmp, $src, 0, 0, 0, 0, $this->config['thumb']['wide'][0], $this->config['thumb']['wide'][1], $width, $height);
			} else {
				$tmp = imagecreatetruecolor($this->config['thumb']['long'][0], $this->config['thumb']['long'][1]);
				imagecopyresampled($tmp, $src, 0, 0, 0, 0, $this->config['thumb']['long'][0], $this->config['thumb']['long'][1], $width, $height);
			}
			$out($tmp, $this->dir.'tn_'.$this->dest, 100);
			# clean up
			imagedestroy($tmp);
		}
	}
}
Return current item: Image Uploader and Resizer