/ Published in: PHP
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();
}
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();
}
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?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']; 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.'); } 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.'); } } public function setUploadFolder($location){ $this->_uploadFolder = $location; } public function setLimitSize($bytes){ $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'); } $image = $this->_uploadFolder . $this->_filename . $this->_extension; $w = $thumbWidth; $h = $thumbHeight; $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; } } imagejpeg($canvas, $this->_uploadFolder . strtolower($this->_filename . '_thumb' . $this->_extension), 100); } public function upload(){ $this->_fileTmp = $this->_uploadFolder . $this->_filename . $this->_extension; $this->_moved = true; $image = $this->_uploadFolder . $this->_filename . $this->_extension; $w = $this->_width; $h = $this->_height; $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; } } } if(!imagejpeg($canvas, $this->_uploadFolder . strtolower($this->_filename . $this->_extension), 100)){ } } } } else { $this->_errors = 'Could not upload image'; } } public function getErrors(){ return $this->_errors; } } ?>