PHP Class: ImageUpload


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

Usage coming soon.


Copy this code and paste it in your HTML
  1. <?php
  2. /**************************************
  3.  seesaw associates | http://seesaw.net
  4.  
  5.  client:
  6.  file:
  7.  description:
  8.  
  9.  Copyright (C) 2008 Matt Kenefick(.com)
  10. **************************************/
  11.  
  12. class mk_imageUpload{
  13.  
  14. var $max_size;
  15. var $allowed_types;
  16. var $thumb_height;
  17. var $dest_dir;
  18. var $extensions;
  19. var $max_height;
  20. var $max_main_height;
  21.  
  22. var $last_ext;
  23. var $last_pid;
  24. var $last_uid;
  25.  
  26. var $image_file;
  27. var $image_field;
  28.  
  29. function __construct( $maxHeightMain, $maxHeightThumb, $destDir ){
  30. $this->max_size = (1024/2)*1000; // 750kb
  31. $this->allowed_types = array( 'jpeg', 'jpg', 'pjpeg', 'gif', 'png' );
  32. $this->extensions = array(
  33. 'image/jpeg' => '.jpg',
  34. 'image/gif' => '.gif',
  35. 'image/png' => '.png',
  36. 'image/x-png' => '.png',
  37. 'image/pjpeg' => '.jpg'
  38. );
  39. $this->dest_dir = $destDir;
  40. $this->max_height = $maxHeightThumb;
  41. $this->max_main_height = $maxHeightMain;
  42. }
  43.  
  44. function putImage( $formname, $newName ){
  45. $this->image_field = $formname;
  46. if( $this->getImage() ){
  47.  
  48. // Check for errors
  49. if ( !$this->checkType() )
  50. return $this->throwError(2);
  51.  
  52. if ( !$this->checkFileSize() )
  53. return $this->throwError(1);
  54.  
  55.  
  56. // Get Image
  57. $img = $this->image_file;
  58.  
  59. // Check seize
  60. if ( !$this->checkImageSize() )
  61. return $this->throwError(3);
  62.  
  63. // Get image dimensinos
  64. $size = $this->getImageSize();
  65. $size['width'] = $size[0];
  66. $size['height'] = $size[1];
  67. $ratio = $this->max_height/$size['height'];
  68.  
  69. $maxheight = $this->max_height;
  70. $maxwidth = $size['width'] * $ratio;
  71.  
  72. // Create Thumbnail
  73. $s_t = $this->resizeImage( $size, $img, $maxwidth, $maxheight,$newName,'s' );
  74.  
  75. if( $size['height'] > $this->max_main_height ){
  76. $ratio = $this->max_main_height/$size['height'];
  77. $maxheight = $this->max_main_height;
  78. $maxwidth = $size['width'] * $ratio;
  79. }else{
  80. $maxheight = $size['height'];
  81. $maxwidth = $size['width'];
  82. }
  83.  
  84. // Create Large rescaled
  85. $s_l = $this->resizeImage( $size, $img, $maxwidth, $maxheight,$newName,'l' );
  86.  
  87. // remove temporary file
  88. unlink($img['tmp_name']);
  89.  
  90. if( $s_t && $s_l ){
  91. $nm = split('_',$newName);
  92. $this->last_ext = $this->extensions[$size['mime']];
  93. $this->last_pid = $nm[0];
  94. $this->last_uid = $nm[1];
  95. return 'ok';
  96. }else{
  97. return $this->throwError( 4 );
  98. }
  99. }else{
  100. return $this->throwError( 2 );
  101. }
  102. }
  103.  
  104. function resizeImage($size,$img,$maxwidth,$maxheight,$newName,$ext){
  105. // Create a thumbnail
  106. if($size['mime'] == "image/pjpeg" || $size['mime'] == "image/jpeg"){
  107. $new_img = imagecreatefromjpeg($img['tmp_name']);
  108. }elseif($size['mime'] == "image/x-png" || $size['mime'] == "image/png"){
  109. $new_img = imagecreatefrompng($img['tmp_name']);
  110. }elseif($size['mime'] == "image/gif"){
  111. $new_img = imagecreatefromgif($img['tmp_name']);
  112. }
  113.  
  114. if (function_exists('imagecreatetruecolor')){
  115. $resized_img = imagecreatetruecolor($maxwidth,$maxheight);
  116. }else{
  117. return("Error: Please make sure your server has GD library ver 2+");
  118. }
  119.  
  120. imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $maxwidth, $maxheight, $size['width'], $size['height']);
  121. if($size['mime'] == "image/pjpeg" || $size['mime'] == "image/jpeg"){
  122. $success = ImageJpeg ($resized_img,$this->dest_dir.$newName.'_'.$ext.$this->extensions[$size['mime']]);
  123. }elseif($size['mime'] == "image/x-png" || $size['mime'] == "image/png"){
  124. $success = ImagePNG ($resized_img,$this->dest_dir.$newName.'_'.$ext.$this->extensions[$size['mime']]);
  125. }elseif($size['mime'] == "image/gif"){
  126. $success = ImageGIF ($resized_img,$this->dest_dir.$newName.'_'.$ext.$this->extensions[$size['mime']]);
  127. }
  128.  
  129. // Remove temporary images
  130. ImageDestroy ($resized_img);
  131. ImageDestroy ($new_img);
  132.  
  133. return $success;
  134. }
  135.  
  136. function getImage(){
  137. if( isset($_FILES[$this->image_field]) && is_uploaded_file($_FILES[$this->image_field]['tmp_name']) ){
  138. $this->image_file = $_FILES[$this->image_field];
  139. return true;
  140. }
  141. return false;
  142. }
  143.  
  144. function returnImg(){
  145. return $this->image_file;
  146. }
  147.  
  148. function getImageSize(){
  149. $img = $this->returnImg();
  150.  
  151. return getimagesize($img['tmp_name']);
  152. }
  153.  
  154. function checkImageSize(){
  155. $size = $this->getImageSize();
  156.  
  157. if( $size[1] < $this->max_height )
  158. return false;
  159. return true;
  160. }
  161.  
  162. function checkFileSize(){
  163. $img = $this->returnImg();
  164.  
  165. if( $img['size'] > $this->max_size )
  166. return false;
  167. return true;
  168. }
  169.  
  170. function checkType(){
  171. $img = $this->returnImg();
  172.  
  173. $type = split('/',$img['type']);
  174. if( !in_array( $type[1], $this->allowed_types ) )
  175. return false;
  176. return true;
  177. }
  178.  
  179. function throwError($val){
  180. switch($val){
  181. case 1: return 'Error: File size is too large';
  182. break;
  183. case 2: return 'Error: Improper file format';
  184. break;
  185. case 3: return 'Error: Your image is too small';
  186. break;
  187. case 4: return 'Error: There was an error creating the images';
  188. break;
  189. }
  190. }
  191.  
  192. }
  193.  
  194. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.