一个完整的图片上传php类

开发技术 作者: 2024-07-27 12:10:01
一个完整的图片上传php类

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

<?PHP
/**************************************
 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,'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,$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,$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;
         }
     }
  
}
  
?>

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_51679.html

猜你喜欢