Return to Snippet

Revision: 26709
at May 7, 2010 14:30 by leonelsantos


Updated Code
<?php
/**
 * A class for uploading images and creating thumbnails
 * 
 * @author Leonel Santos
 * @email [email protected]
 * @copyright Leonel Santos 2010
 * @version 1.1
 */
class img {
	protected $_tempName;
	protected $_tempTmp;
	protected $_tempType;
	protected $_filesize;
	protected $_errors;
	protected $_extension;
	protected $_fileTmp;
	protected $_filename;
	protected $_width = 600;
	protected $_height = 450;
	protected $_moved = false;
	protected $_cropToSquare = false;
	
	// default: same folder where the script file is at
	protected $_uploadFolder;

	// default: 1M
	protected $_limitSize = 1000000;

	public function __construct($field){
		$this->_tempTmp = $_FILES[$field]['tmp_name'];
		$this->_tempType = $_FILES[$field]['type'];
		$this->_filesize = $_FILES[$field]['size'];
		$this->_tempName = $_FILES[$field]['name'];
		$this->_extension = '.'.strtolower(end(explode('.', $this->_tempName)));
		$this->_filename = substr($this->_tempName, 0, -strlen($this->_extension));

		if($this->_tempType != "image/jpeg"){
			$this->_errors = 'You can only upload JPG images. Try again.';
			throw new Exception('You can only upload JPG images. Try again.');
			exit();
		}
		if($this->_filesize>=$this->_limitSize){
			throw new Exception('The file size of the file you are trying to upload is over limit. Your file size = '.$this->_filesize.' bytes. File size limit = '.$this->_limitSize.' bytes. Try again.');	
			exit();
		}


	}

	public function setUploadFolder($location){
		$this->_uploadFolder = $location;
	}

	public function setLimitSize($bytes){
		if(!is_int($bytes) and $bytes < 0 and $bytes > 20000000){
			$this->_errors = 'The size limit must be bigger than 0 and less than 20000000';
		}else{
			$this->_limitSize = $bytes;
		}
	}

	public function setFilename($filename){
		$this->_filename = $filename;
	}

	public function cropToSquare(){
		$this->_cropToSquare = true;
	}

	public function setDimensions($width, $height){
		$this->_width = $width;
		$this->_height = $height;
	}

	public function thumb($thumbWidth=80, $thumbHeight=80){
		if($this->_moved==false){
			throw new Exception('You have to use the upload() function before you use the thumb() function');
			exit();
		}
		$image_data = imagecreatefromjpeg($this->_fileTmp);
		$image = $this->_uploadFolder . $this->_filename . $this->_extension;
		$w = $thumbWidth;
		$h = $thumbHeight;
		$size = getimagesize($image);
		$original_width = $size[0];
		$original_height = $size[1];
		$imageRatio=$original_width/$original_height; // calculate the image ratio

		if($this->_cropToSquare==false){	
			if($imageRatio>1){// landscape
				$h = $w/$imageRatio;
			}else{// portrait
				$w = $w*$imageRatio;
			}
			$x_offset = 0;
			$y_offset = 0;
			$src_w = $original_width;
			$src_h = $original_height;
		}else{
			if($original_width > $original_height){//landscape
				$x_offset = ($original_width - $original_height) / 2;
				$y_offset = 0;
				$src_w = $original_width - ($x_offset * 2);
				$src_h = $src_w;
				$w=$thumbWidth;
				$h=$w;
			}else{ // portrait and squre
				$x_offset = 0;
				$y_offset = ($original_height - $original_width) / 2;
				$src_w = $original_height - ($y_offset * 2);
				$src_h = $src_w;
				$h=$thumbHeight;
				$w=$h;
			}
		}
		$canvas = imagecreatetruecolor($w, $h);
		imagecopyresampled($canvas, $image_data, 0, 0, $x_offset, $y_offset, $w, $h, $src_w, $src_h);
		imagejpeg($canvas, $this->_uploadFolder . strtolower($this->_filename . '_thumb' . $this->_extension), 100);
		imagedestroy($canvas);
		imagedestroy($image_data);
	}

	public function upload(){
		$this->_fileTmp = $this->_uploadFolder . $this->_filename . $this->_extension;
		if(move_uploaded_file($this->_tempTmp, $this->_fileTmp)){
			$this->_moved = true;
			$image_data = imagecreatefromjpeg($this->_fileTmp);
			$image = $this->_uploadFolder . $this->_filename . $this->_extension;
			$w = $this->_width;
			$h = $this->_height;
			$size = getimagesize($image);
			$original_width = $size[0];
			$original_height = $size[1];
			$imageRatio=$original_width/$original_height; // calculate the image ratio

			if($this->_cropToSquare==false){	
				if($imageRatio>1){
					$xWidth = $w; //landscape
					$h = $xWidth/$imageRatio;
				}else{ // portrait
					$xWidth = $h;
					$w = $xWidth*$imageRatio;
				}
			
				$x_offset = 0;
				$y_offset = 0;
				$src_w = $original_width;
				$src_h = $original_height;
			}else{
				if($original_width > $original_height){//landscape
					$x_offset = ($original_width - $original_height) / 2;
					$y_offset = 0;
					$src_w = $original_width - ($x_offset * 2);
					$src_h = $src_w;
					$w=$this->_width;
					$h=$w;
				}else{ // portrait and squre
					$x_offset = 0;
					$y_offset = ($original_height - $original_width) / 2;
					$src_w = $original_height - ($y_offset * 2);
					$src_h = $src_w;
					$h=$this->_height;
					$w=$h;
				}
			}

			$canvas = imagecreatetruecolor($w, $h);
			if(!imagecopyresampled($canvas, $image_data, 0, 0, $x_offset, $y_offset, $w, $h, $src_w, $src_h)){
				exit('imagecopyresampled error');
			}
			if(!imagejpeg($canvas, $this->_uploadFolder . strtolower($this->_filename . $this->_extension), 100)){
				exit('imagejpeg error');
			}
			if(!imagedestroy($canvas)){
				exit('imagedestroy error');
			}
			if(!imagedestroy($image_data)){
				exit('imagedestroy error');
			}

		} else {
			$this->_errors = 'Could not upload image';
		}
	}

    public function getErrors(){
        return $this->_errors;
    }
}
?>

Revision: 26708
at May 7, 2010 09:15 by leonelsantos


Initial Code
<?php
/**
 * A class for uploading images and creating thumbnails
 * 
 * @author Leonel Santos
 * @email [email protected]
 * @copyright Leonel Santos 2010
 * @version 1.0
 */
class img {
	protected $_tempName;
	protected $_tempTmp;
	protected $_tempType;
	protected $_fileSize;
	protected $_errors;
	protected $_extension;
	protected $_fileTmp;
	protected $_filename;
	protected $_width = 600;
	protected $_height = 450;
	protected $_moved = false;
	
	// default: same folder where the script file is at
	protected $_uploadFolder;

	// default: 2M
	protected $_limitSize = 2000000;

	public function __construct($field){
		$this->_tempTmp = $_FILES[$field]['tmp_name'];
		$this->_tempType = $_FILES[$field]['type'];
		$this->_fileSize = $_FILES[$field]['size'];
		$this->_tempName = $_FILES[$field]['name'];
		$this->_extension = '.'.strtolower(end(explode('.', $this->_tempName)));
		$this->_filename = substr($this->_tempName, 0, -strlen($this->_extension));

		if($this->_tempType != "image/jpeg"){
			$this->_errors = 'You can only upload JPG images. Try again.';
			throw new Exception('You can only upload JPG images. Try again.');
			exit();
		}


	if($this->_fileSize>=$this->_limitSize){
		throw new Exception('The file size of the file you are trying to upload is over limit. Your file size = '.$fileSize.' bytes. File size limit = '.$this->_limitSize.' bytes. Try again.');	
		exit();
	}


	}

	public function setUploadFolder($location){
		$this->_uploadFolder = $location;
	}

	public function setLimitSize($size){
		if(!is_int($size) and $size < 0 and $size > 20000000){
			$this->_errors = 'The size limit must be bigger than 0 and less than 20000000';
		}else{
			$this->_limitSize = $size;
		}
	}

	public function setFilename($filename){
		$this->_filename = $filename;
	}

	public function setDimensions($width, $height){
		$this->_width = $width;
		$this->_height = $height;
	}

	public function thumb($thumbWidth=100, $thumbHeight=100){
		if($this->_moved==false){
			throw new Exception('You have to use the upload() function before you use the thumb() function');
			exit();
		}
			$newImage = imagecreatefromjpeg($this->_fileTmp);
			$image = $this->_uploadFolder . $this->_filename . $this->_extension;
			$w = $thumbWidth;
			$h = $thumbHeight;
			$size = getimagesize($image);
			$width = $size[0];
			$height = $size[1];
			$imageRatio=$width/$height; // calculate the image ratio
	
			// if photo is horizontal
			if($imageRatio>1){
				$xWidth = $w;
				$h = $xWidth/$imageRatio;
			// if photo is vertical
			}else{
				$xWidth = $h;
				$w = $xWidth*$imageRatio;
			}
			
			$resizedImage = imagecreatetruecolor($w, $h);
			imagecopyresampled($resizedImage, $newImage, 0, 0, 0, 0, $w, $h, $width, $height);

			imagejpeg($resizedImage, $this->_uploadFolder . strtolower($this->_filename . '_thumb' . $this->_extension), 100);
			imagedestroy($resizedImage);
			imagedestroy($newImage);
	}

	public function upload(){
		$this->_fileTmp = $this->_uploadFolder . $this->_filename . $this->_extension;
		if(move_uploaded_file($this->_tempTmp, $this->_fileTmp)){
			$this->_moved = true;
			// FIX THE FOLLOWING
			// else($tempType == "image/x-png" || $tempType == "image/png") {
			//	$newImage = imagecreatefromjpeg($fileTmp); }
			// if($tempType == "image/gif"){$newImage = imagecreatefromgif($fileTmp);}
			// if($tempType == "image/png" || $tempType == "image/x-png"){$newImage = imagecreatefrompng($fileTmp);}
			$newImage = imagecreatefromjpeg($this->_fileTmp);
			$image = $this->_uploadFolder . $this->_filename . $this->_extension;
			$w = $this->_width;
			$h = $this->_height;
			$size = getimagesize($image);
			$width = $size[0];
			$height = $size[1];
			$imageRatio=$width/$height; // calculate the image ratio
	
			// if photo is horizontal
			if($imageRatio>1){
				$xWidth = $w;
				$h = $xWidth/$imageRatio;
			// if photo is vertical
			}else{
				$xWidth = $h;
				$w = $xWidth*$imageRatio;
			}
			
			$resizedImage = imagecreatetruecolor($w, $h);
			imagecopyresampled($resizedImage, $newImage, 0, 0, 0, 0, $w, $h, $width, $height);
			imagejpeg($resizedImage, $this->_uploadFolder . strtolower($this->_filename . $this->_extension), 100);
			imagedestroy($resizedImage);
			imagedestroy($newImage);

		} else {
			$this->_errors = 'Could not upload image';
		}
	}

    public function getErrors(){
        return $this->_errors;
    }
}
?>

Initial URL

                                

Initial Description
A class for uploading images and creating thumbnails.

Example:
		try{
			$img = new img('photo');
			if($_FILES['photo']['size']>=1000000){
				exit('The file size of the file you are trying to upload is over limit.
				Your file size = '.$_FILES['photo']['size'].' bytes. File size limit = '.
				$this->_limitSize.' bytes. Try again.');
			}
			$img->setUploadFolder('img/');
			$img->setFilename($id);
			$img->setDimensions(250,175);
			$img->cropToSquare();
			$img->upload();
			$img->thumb();
			$errors = $img->getErrors();
		} catch (Exception $e) {
			$errors = $e->getMessage();
		}

Initial Title
PHP Image Upload Create Thumbnails Class

Initial Tags
php, image

Initial Language
PHP