PHP Image Upload Create Thumbnails Class


/ Published in: PHP
Save to your folder(s)

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();
}


Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * A class for uploading images and creating thumbnails
  4.  *
  5.  * @author Leonel Santos
  6.  * @email leonelsantosnet@gmail.com
  7.  * @copyright Leonel Santos 2010
  8.  * @version 1.1
  9.  */
  10. class img {
  11. protected $_tempName;
  12. protected $_tempTmp;
  13. protected $_tempType;
  14. protected $_filesize;
  15. protected $_errors;
  16. protected $_extension;
  17. protected $_fileTmp;
  18. protected $_filename;
  19. protected $_width = 600;
  20. protected $_height = 450;
  21. protected $_moved = false;
  22. protected $_cropToSquare = false;
  23.  
  24. // default: same folder where the script file is at
  25. protected $_uploadFolder;
  26.  
  27. // default: 1M
  28. protected $_limitSize = 1000000;
  29.  
  30. public function __construct($field){
  31. $this->_tempTmp = $_FILES[$field]['tmp_name'];
  32. $this->_tempType = $_FILES[$field]['type'];
  33. $this->_filesize = $_FILES[$field]['size'];
  34. $this->_tempName = $_FILES[$field]['name'];
  35. $this->_extension = '.'.strtolower(end(explode('.', $this->_tempName)));
  36. $this->_filename = substr($this->_tempName, 0, -strlen($this->_extension));
  37.  
  38. if($this->_tempType != "image/jpeg"){
  39. $this->_errors = 'You can only upload JPG images. Try again.';
  40. throw new Exception('You can only upload JPG images. Try again.');
  41. exit();
  42. }
  43. if($this->_filesize>=$this->_limitSize){
  44. 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.');
  45. exit();
  46. }
  47.  
  48.  
  49. }
  50.  
  51. public function setUploadFolder($location){
  52. $this->_uploadFolder = $location;
  53. }
  54.  
  55. public function setLimitSize($bytes){
  56. if(!is_int($bytes) and $bytes < 0 and $bytes > 20000000){
  57. $this->_errors = 'The size limit must be bigger than 0 and less than 20000000';
  58. }else{
  59. $this->_limitSize = $bytes;
  60. }
  61. }
  62.  
  63. public function setFilename($filename){
  64. $this->_filename = $filename;
  65. }
  66.  
  67. public function cropToSquare(){
  68. $this->_cropToSquare = true;
  69. }
  70.  
  71. public function setDimensions($width, $height){
  72. $this->_width = $width;
  73. $this->_height = $height;
  74. }
  75.  
  76. public function thumb($thumbWidth=80, $thumbHeight=80){
  77. if($this->_moved==false){
  78. throw new Exception('You have to use the upload() function before you use the thumb() function');
  79. exit();
  80. }
  81. $image_data = imagecreatefromjpeg($this->_fileTmp);
  82. $image = $this->_uploadFolder . $this->_filename . $this->_extension;
  83. $w = $thumbWidth;
  84. $h = $thumbHeight;
  85. $size = getimagesize($image);
  86. $original_width = $size[0];
  87. $original_height = $size[1];
  88. $imageRatio=$original_width/$original_height; // calculate the image ratio
  89.  
  90. if($this->_cropToSquare==false){
  91. if($imageRatio>1){// landscape
  92. $h = $w/$imageRatio;
  93. }else{// portrait
  94. $w = $w*$imageRatio;
  95. }
  96. $x_offset = 0;
  97. $y_offset = 0;
  98. $src_w = $original_width;
  99. $src_h = $original_height;
  100. }else{
  101. if($original_width > $original_height){//landscape
  102. $x_offset = ($original_width - $original_height) / 2;
  103. $y_offset = 0;
  104. $src_w = $original_width - ($x_offset * 2);
  105. $src_h = $src_w;
  106. $w=$thumbWidth;
  107. $h=$w;
  108. }else{ // portrait and squre
  109. $x_offset = 0;
  110. $y_offset = ($original_height - $original_width) / 2;
  111. $src_w = $original_height - ($y_offset * 2);
  112. $src_h = $src_w;
  113. $h=$thumbHeight;
  114. $w=$h;
  115. }
  116. }
  117. $canvas = imagecreatetruecolor($w, $h);
  118. imagecopyresampled($canvas, $image_data, 0, 0, $x_offset, $y_offset, $w, $h, $src_w, $src_h);
  119. imagejpeg($canvas, $this->_uploadFolder . strtolower($this->_filename . '_thumb' . $this->_extension), 100);
  120. imagedestroy($canvas);
  121. imagedestroy($image_data);
  122. }
  123.  
  124. public function upload(){
  125. $this->_fileTmp = $this->_uploadFolder . $this->_filename . $this->_extension;
  126. if(move_uploaded_file($this->_tempTmp, $this->_fileTmp)){
  127. $this->_moved = true;
  128. $image_data = imagecreatefromjpeg($this->_fileTmp);
  129. $image = $this->_uploadFolder . $this->_filename . $this->_extension;
  130. $w = $this->_width;
  131. $h = $this->_height;
  132. $size = getimagesize($image);
  133. $original_width = $size[0];
  134. $original_height = $size[1];
  135. $imageRatio=$original_width/$original_height; // calculate the image ratio
  136.  
  137. if($this->_cropToSquare==false){
  138. if($imageRatio>1){
  139. $xWidth = $w; //landscape
  140. $h = $xWidth/$imageRatio;
  141. }else{ // portrait
  142. $xWidth = $h;
  143. $w = $xWidth*$imageRatio;
  144. }
  145.  
  146. $x_offset = 0;
  147. $y_offset = 0;
  148. $src_w = $original_width;
  149. $src_h = $original_height;
  150. }else{
  151. if($original_width > $original_height){//landscape
  152. $x_offset = ($original_width - $original_height) / 2;
  153. $y_offset = 0;
  154. $src_w = $original_width - ($x_offset * 2);
  155. $src_h = $src_w;
  156. $w=$this->_width;
  157. $h=$w;
  158. }else{ // portrait and squre
  159. $x_offset = 0;
  160. $y_offset = ($original_height - $original_width) / 2;
  161. $src_w = $original_height - ($y_offset * 2);
  162. $src_h = $src_w;
  163. $h=$this->_height;
  164. $w=$h;
  165. }
  166. }
  167.  
  168. $canvas = imagecreatetruecolor($w, $h);
  169. if(!imagecopyresampled($canvas, $image_data, 0, 0, $x_offset, $y_offset, $w, $h, $src_w, $src_h)){
  170. exit('imagecopyresampled error');
  171. }
  172. if(!imagejpeg($canvas, $this->_uploadFolder . strtolower($this->_filename . $this->_extension), 100)){
  173. exit('imagejpeg error');
  174. }
  175. if(!imagedestroy($canvas)){
  176. exit('imagedestroy error');
  177. }
  178. if(!imagedestroy($image_data)){
  179. exit('imagedestroy error');
  180. }
  181.  
  182. } else {
  183. $this->_errors = 'Could not upload image';
  184. }
  185. }
  186.  
  187. public function getErrors(){
  188. return $this->_errors;
  189. }
  190. }
  191. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.