This php image upload function is very complete and can fully meet the needs of various image uploads
- /**************************************
- Seesaw associates | http://seesaw.net
-
- Client:
- File:
- Description:
-
- Copyright (C) 2008 Matt Kenefick (. com)
- **************************************/
-
- Class mk_imageUpload {
-
- Var $ max_size;
- Var $ allowed_types;
- Var $ thumb_height;
- Var $ dest_dir;
- Var $ extensions;
- Var $ max_height;
- Var $ max_main_height;
-
- Var $ last_ext;
- Var $ last_pid;
- Var $ last_uid;
-
- Var $ image_file;
- Var $ image_field;
-
- Function _ construct ($ maxHeightMain, $ maxHeightThumb, $ destDir ){
- $ This-> max_size = (1024/2) * 1000; // 750kb
- $ This-> allowed_types = array ('jpeg ', 'jpg', 'pjpeg ', 'GIF', 'PNG ');
- $ This-> extensions = array (
- 'Image/jpeg '=> '.jpg ',
- 'Image/GIF' => '.gif ',
- 'Image/png '=> '.png ',
- 'Image/x-png '=> '.png ',
- 'Image/pjpeg '=> '.jpg'
- );
- $ This-> dest_dir = $ destDir;
- $ This-> max_height = $ maxHeightThumb;
- $ This-> max_main_height = $ maxHeightMain;
- }
-
- Function putImage ($ formname, $ newName ){
- $ This-> image_field = $ formname;
- If ($ this-> getImage ()){
-
- // Check for errors
- If (! $ This-> checkType ())
- Return $ this-> throwError (2 );
-
- If (! $ This-> checkFileSize ())
- Return $ this-> throwError (1 );
-
-
- // Get Image
- $ Img = $ this-> image_file;
-
- // Check seize
- If (! $ This-> checkImageSize ())
- Return $ this-> throwError (3 );
-
- // Get image dimensinos
- $ Size = $ this-> getImageSize ();
- $ Size ['width'] = $ size [0];
- $ Size ['height'] = $ size [1];
- $ Ratio = $ this-> max_height/$ size ['height'];
-
- $ Maxheight = $ this-> max_height;
- $ Maxwidth = $ size ['width'] * $ ratio;
-
- // Create Thumbnail
- $ S_t = $ this-> resizeImage ($ size, $ img, $ maxwidth, $ maxheight, $ newName,'s ');
-
- If ($ size ['height']> $ this-> max_main_height ){
- $ Ratio = $ this-> max_main_height/$ size ['height'];
- $ Maxheight = $ this-> max_main_height;
- $ Maxwidth = $ size ['width'] * $ ratio;
- } Else {
- $ Maxheight = $ size ['height'];
- $ Maxwidth = $ size ['width'];
- }
-
- // Create Large rescaled
- $ S_l = $ this-> resizeImage ($ size, $ img, $ maxwidth, $ maxheight, $ newName, 'L ');
-
- // Remove temporary file
- Unlink ($ img ['tmp _ name']);
-
- If ($ s_t & $ s_l ){
- $ Nm = split ('_', $ newName );
- $ This-> last_ext = $ this-> extensions [$ size ['Mime '];
- $ This-> last_pid = $ nm [0];
- $ This-> last_uid = $ nm [1];
- Return 'OK ';
- } Else {
- Return $ this-> throwError (4 );
- }
- } Else {
- Return $ this-> throwError (2 );
- }
- }
-
- Function resizeImage ($ size, $ img, $ maxwidth, $ maxheight, $ newName, $ ext ){
- // Create a thumbnail
- If ($ size ['Mime '] = "image/pjpeg" | $ size ['Mime'] = "image/jpeg "){
- $ New_img = imagecreatefromjpeg ($ img ['tmp _ name']);
- } Elseif ($ size ['Mime '] = "image/x-png" | $ size ['Mime'] = "image/png "){
- $ New_img = imagecreatefrompng ($ img ['tmp _ name']);
- } Elseif ($ size ['Mime '] = "image/gif "){
- $ New_img = imagecreatefromgif ($ img ['tmp _ name']);
- }
-
- If (function_exists ('imagecreatetruecolor ')){
- $ Resized_img = imagecreatetruecolor ($ maxwidth, $ maxheight );
- } Else {
- Return ("Error: Please make sure your server has GD library ver 2 + ");
- }
-
- Imagecopyresized ($ resized_img, $ new_img, 0, 0, 0, $ maxwidth, $ maxheight, $ size ['width'], $ size ['height']);
- If ($ size ['Mime '] = "image/pjpeg" | $ size ['Mime'] = "image/jpeg "){
- $ Success = ImageJpeg ($ resized_img, $ this-> dest_dir. $ newName. '_'. $ ext. $ this-> extensions [$ size ['Mime ']);
- } Elseif ($ size ['Mime '] = "image/x-png" | $ size ['Mime'] = "image/png "){
- $ Success = ImagePNG ($ resized_img, $ this-> dest_dir. $ newName. '_'. $ ext. $ this-> extensions [$ size ['Mime ']);
- } Elseif ($ size ['Mime '] = "image/gif "){
- $ Success = ImageGIF ($ resized_img, $ this-> dest_dir. $ newName. '_'. $ ext. $ this-> extensions [$ size ['Mime ']);
- }
-
- // Remove temporary images
- ImageDestroy ($ resized_img );
- ImageDestroy ($ new_img );
-
- Return $ success;
- }
-
- Function getImage (){
- If (isset ($ _ FILES [$ this-> image_field]) & is_uploaded_file ($ _ FILES [$ this-> image_field] ['tmp _ name']) {
- $ This-> image_file =$ _ FILES [$ this-> image_field];
- Return true;
- }
- Return false;
- }
-
- Function returnImg (){
- Return $ this-> image_file;
- }
-
- Function getImageSize (){
- $ Img = $ this-> returnImg ();
-
- Return getimagesize ($ img ['tmp _ name']);
- }
-
- Function checkImageSize (){
- $ Size = $ this-> getImageSize ();
-
- If ($ size [1] <$ this-> max_height)
- Return false;
- Return true;
- }
-
- Function checkFileSize (){
- $ Img = $ this-> returnImg ();
-
- If ($ img ['size']> $ this-> max_size)
- Return false;
- Return true;
- }
-
- Function checkType (){
- $ Img = $ this-> returnImg ();
-
- $ Type = split ('/', $ img ['type']);
- If (! In_array ($ type [1], $ this-> allowed_types ))
- Return false;
- Return true;
- }
-
- Function throwError ($ val ){
- Switch ($ val ){
- Case 1: return 'Error: File size is too large ';
- Break;
- Case 2: return 'Error: Improper file format ';
- Break;
- Case 3: return 'Error: Your image is too small ';
- Break;
- Case 4: return 'Error: There was an Error creating the images ';
- Break;
- }
- }
-
- }
-
- ?>
|