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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.