Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2008
    Location
    Somerset, England
    Posts
    93
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Class for handling image uploads

    Okay, so this class has two static functions.

    upload_image: Takes an uploaded file ensures it's an allowed type and file size, then names the file with the specified name or a default value and moves it to the required directory

    resize_image: Takes an uploaded file ensures it's an allowed type and file size then creates a scaled version with the width and height set by the user, this is then saved with a specified or default name in the specified directory.

    HINT: To keep the same aspect ratio as the original
    PHP Code:
    list($src_x$src_y) = getimagesize($_FILES['img']['tmp_name']);
    $ratio $src_x $src_y;
    //fixed width
    $width 200;
    $height $width $ratio;
    //fixed height
    $height 200
    $width 
    $height $ratio
    Now call the image_resize function with the width and height attributes set accordingly.

    Class
    PHP Code:
    class Image_Handler extends System_Config {
        private static 
    $default_img_name;
        
        private static function 
    set_name($type) {
            
    $t date('Y-m-d-H-i-s') . '.'
            
    $t .= (substr($type6) == 'jpeg' || substr($img['type'], 6) == 'pjpeg') ? 'jpg' substr($type6);
            
    self::$default_img_name $t;
        }

        public static function 
    upload_image($img$dst_dir$name '') {
            if(
    $name == '') {
                
    self::set_name($img['type']);
                
    $name self::$default_img_name;
            } else {
                
    $name $name '.';
                
    $name .= (substr($img['type'], 6) == 'jpeg' || substr($img['type'], 6) == 'pjpeg') ? 'jpg' substr($img['type'], 6);
            }
            
    $destination $dst_dir '/' $name;
            if(
    $img['error'] == 0) {
                if(!
    in_array($img['type'], self::$allowed_image_types)) {
                    throw new 
    mailException('Not a valid image type ' $img['type'], basename($_SERVER['PHP_SELF']), get_class($this));
                } elseif(
    $img['size'] > self::$max_upload_size) {
                    throw new 
    mailException('File size exceeds limit'basename($_SERVER['PHP_SELF']), get_class($this));
                } elseif(!@
    move_uploaded_file($img['tmp_name'], $destination)) {
                    throw new 
    mailException('Upload failed moving' $img['tmp_name'] . ' to ' $destinationbasename($_SERVER['PHP_SELF']), get_class($this));
                } else {
                    return 
    $destination;
                }
            } else {
                switch(
    $img['error']) {
                    case 
    1:
                    throw new 
    mailException('The file is too large'basename($_SERVER['PHP_SELF']), get_class($this));
                    break;
                    
                    case 
    2:
                    throw new 
    mailException('The file is too large'basename($_SERVER['PHP_SELF']), get_class($this));
                    break;
                    
                    case 
    3:
                    throw new 
    mailException('The file was only partially uploaded'basename($_SERVER['PHP_SELF']), get_class($this));
                    break;
                    
                    case 
    4:
                    throw new 
    mailException('No file was uploaded'basename($_SERVER['PHP_SELF']), get_class($this));
                    break;
                    
                    default:
                    throw new 
    mailException('An unspecified error has occured'basename($_SERVER['PHP_SELF']), get_class($this));
                }
            }
        }
        
        public static function 
    resize_image($img$dst_dir$width$height$name ''$pngbkg '') {
            if(
    $name == '') {
                
    self::set_name($img['type']);
                
    $name self::$default_img_name;
            } else {
                
    $name $name '.';
                
    $name .= (substr($img['type'], 6) == 'jpeg' || substr($img['type'], 6) == 'pjpeg') ? 'jpg' substr($img['type'], 6);
            }            
            
    $destination $dst_dir '/' $name;
            if(
    $img['error'] != 0) {
                switch(
    $img['error']) {
                    case 
    1:
                    throw new 
    mailException('The file is too large'basename($_SERVER['PHP_SELF']), get_class($this));
                    break;
                    
                    case 
    2:
                    throw new 
    mailException('The file is too large'basename($_SERVER['PHP_SELF']), get_class($this));
                    break;
                    
                    case 
    3:
                    throw new 
    mailException('The file was only partially uploaded'basename($_SERVER['PHP_SELF']), get_class($this));
                    break;
                    
                    case 
    4:
                    throw new 
    mailException('No file was uploaded'basename($_SERVER['PHP_SELF']), get_class($this));
                    break;
                    
                    default:
                    throw new 
    mailException('An unspecified error has occured'basename($_SERVER['PHP_SELF']), get_class($this));
                }
            } else {
                if(!
    in_array($img['type'], self::$allowed_image_types)) {
                    throw new 
    mailException('Not a valid image type ' $img['type'], basename($_SERVER['PHP_SELF']), get_class($this));
                } elseif(
    $img['size'] > self::$max_upload_size) {
                    throw new 
    mailException('File size exceeds limit'basename($_SERVER['PHP_SELF']), get_class($this));
                } else {
                    
    $ext substr($img['type'], 6);
                    switch(
    $ext) {
                        case 
    'png':
                        
    $create 'imagecreatefrompng';
                        
    $save 'imagepng';
                        break;
                        
                        case 
    'gif':
                        
    $create 'imagecreatefromgif';
                        
    $save 'imagegif';
                        break;
                        
                        default:
                        
    $create 'imagecreatefromjpeg';
                        
    $save 'imagejpeg';
                        break;
                    }
                    
    $src_img $create($img['tmp_name']);
                    list(
    $src_width$src_height) = getimagesize($img['tmp_name']);
                    
    $dest_x $width;    
                    
    $dest_y $height;
                    if(
    $pngbkg == '') {
                        
    $dst_img imagecreatetruecolor($dest_x$dest_y);
                    } else {
                        
    $dst_img imagecreatefrompng($pngbkg);
                    }
                    
    imagecopyresampled($dst_img$src_img0000$dest_x$dest_y$src_width$src_height);
                    if(!@
    $save($dst_img$destination)) {
                        throw new 
    mailException('The image could not be resized'basename($_SERVER['PHP_SELF']), get_class($this));
                    } else {
                        return 
    $name;
                    }
                }
            }
        }

    Note: PHP 5 required

    Note: This class extends a system_config class to load some values such as allowed image types and max file sizes, these can be set as private member variables instead.

    Note: mailException is a custom exception I wrote which mails me the exception whilst displaying a user friendly error to the user. Use Exception instead if you like.

    If you have any questions or suggestions regarding functionality I'll be happy to help
    Last edited by RMcLeod; 03-18-2008 at 06:02 PM.

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Posts
    103
    Thanks
    1
    Thanked 8 Times in 8 Posts
    very nice! I may use this for reference when I make a similar script for my site!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •