php----画像アップロードパッケージ類:1枚、複数の画像アップロード、サムネイル生成

28317 ワード


/**
 *   :   ,      ,     
 * Class ImgUpload
 */
class ImgUpload
{
    private $file;                  //    
    private $fileList;            //    
    private $inputName;     //    
    private $uploadPath;    //    
    private $fileMaxSize;    //    
    private $uploadFiles;   //    

    //         
    private $allowExt = array('bmp', 'jpg', 'jpeg', 'png', 'gif');

    /**
     * @param $inputName input   name  
     * @param $uploadPath       
     */
    public function __construct($inputName, $uploadPath)
    {
        $this->inputName = $inputName;
        $this->uploadPath = $uploadPath;
        $this->fileList = array();
        $this->file = $file = array(
            'name' => null,
            'type' => null,
            'tmp_name' => null,
            'size' => null,
            'errno' => null,
            'error' => null
        );
    }

    /**
     *              
     * @param $allowExt       
     */
    public function setAllowExt($allowExt)
    {
        if (is_array($allowExt)) {
            $this->allowExt = $allowExt;
        } else {
            $this->allowExt = array($allowExt);
        }
    }

    /**
     *            
     * @param $fileMaxSize       
     */
    public function setMaxSize($fileMaxSize)
    {
        $this->fileMaxSize = $fileMaxSize;
    }

    /**
     *            
     * @return mixed
     */
    public function getUploadFiles()
    {
        return $this->uploadFiles;
    }

    /**
     *            
     * @return array|mixed
     */
    public function getErrorMsg()
    {
        if (count($this->fileList) == 0) {
            return $this->file['error'];
        } else {
            $errArr = array();
            foreach ($this->fileList as $item) {
                array_push($errArr, $item['error']);
            }
            return $errArr;
        }
    }

    /**
     *        
     * @param $isList
     */
    private function initFile($isList)
    {
        if ($isList) {
            foreach ($_FILES[$this->inputName] as $key => $item) {
                for ($i = 0; $i
< count($item); $i++) {
                    if ($key == 'error') {
                        $this->
    fileList[$i]['error'] = null;
                        $this->fileList[$i]['errno'] = $item[$i];
                    } else {
                        $this->fileList[$i][$key] = $item[$i];
                    }
                }
            }
        } else {
            $this->file['name'] = $_FILES[$this->inputName]['name'];
            $this->file['type'] = $_FILES[$this->inputName]['type'];
            $this->file['tmp_name'] = $_FILES[$this->inputName]['tmp_name'];
            $this->file['size'] = $_FILES[$this->inputName]['size'];
            $this->file['errno'] = $_FILES[$this->inputName]['error'];
        }
    }

    /**
     *       
     * @param $errno
     * @return null|string
     */
    private function errorCheck($errno)
    {
        switch ($errno) {
            case UPLOAD_ERR_OK:
                return null;
            case UPLOAD_ERR_INI_SIZE:
                return '           ';
            case UPLOAD_ERR_FORM_SIZE:
                return '          ';
            case UPLOAD_ERR_PARTIAL:
                return '         ';
            case UPLOAD_ERR_NO_FILE:
                return '       ';
            case UPLOAD_ERR_NO_TMP_DIR:
                return '        ';
            case UPLOAD_ERR_CANT_WRITE:
                return '      ';
            case UPLOAD_ERR_EXTENSION:
                return '         ';
        }
    }

    /**
     *       
     * @param $file
     * @throws Exception
     */
    private function fileCheck($file)
    {
        //          
        if ($file['errno'] != 0) {
            $error = $this->errorCheck($file['errno']);
            throw new Exception($error);
        }
        //          
        if (!empty($this->fileMaxSize) && $file['size'] > $this->fileMaxSize) {
            throw new Exception('      ' . ($this->fileMaxSize / 1024) . 'KB');
        }
        //          
        $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
        if (!in_array($ext, $this->allowExt)) {
            throw new Exception('          ');
        }
        //         HTTP
        if (!is_uploaded_file($file['tmp_name'])) {
            throw new Exception('      HTTP     ');
        }
        //        
        if (!getimagesize($file['tmp_name'])) {
            throw new Exception('      ');
        }
        //          
        if (!file_exists($this->uploadPath)) {
            mkdir($this->uploadPath, null, true);
        }
    }

    /**
     *      ,    true
     * @return bool
     */
    public function acceptSingleFile()
    {
        $this->initFile(false);
        try {
            $this->fileCheck($this->file);
           // $md_name = md5(uniqid(microtime(true), true)) . '.' . pathinfo($this->file['name'], PATHINFO_EXTENSION);

           //        
            $md_name = uniqid() . '.' . pathinfo($this->file['name'], PATHINFO_EXTENSION);

            if (move_uploaded_file($this->file['tmp_name'], $this->uploadPath . $md_name)) {
                $this->uploadFiles = array($this->uploadPath . $md_name);
            } else {
                throw new Exception('      ');
            }
        } catch (Exception $e) {
            $this->file['error'] = $e->getMessage();
        } finally {
            if (file_exists($this->file['tmp_name'])) {
                unlink($this->file['tmp_name']);
            }
        }
        return empty($this->file['error']) ? true : false;
    }

    /**
     *      ,      true
     * @return bool
     */
    public function acceptMultiFile()
    {
        $this->initFile(true);
        $this->uploadFiles = array();
        for ($i = 0; $i
    < count($this->
        fileList); $i++) {
            try {
                $this->fileCheck($this->fileList[$i]);
                $ext = pathinfo($this->fileList[$i]['name'], PATHINFO_EXTENSION);
                // $md_name = md5(uniqid(microtime(true), true)) . '.' . $ext;

                //        
                $md_name = uniqid() . '.' . $ext;

                if (move_uploaded_file($this->fileList[$i]['tmp_name'], $this->uploadPath . $md_name)) {
                    array_push($this->uploadFiles, $this->uploadPath . $md_name);
                } else {
                    throw new Exception('      ');
                }
            } catch (Exception $e) {
                $this->fileList[$i]['error'] = $e->getMessage();
            } finally {
                if (file_exists($this->fileList[$i]['tmp_name'])) {
                    unlink($this->fileList[$i]['tmp_name']);
                }
            }
        }
        foreach ($this->fileList as $item) {
            if (!empty($item['error'])) {
                return false;
            }
        }
        return true;
    }



     /**
     *      :
     * @param $picName    .            .   : $imgUpload->getUploadFiles()[0]
     * @param $max_w      
     * @param $max_h      
     * @param $pre         
     * @return bool
     */
    public function imgUpdateSize($picName, $max_w=100, $max_h=100, $pre='_s'){
        #1          
        $info = getimagesize($picName);
        $w = $info[0];
        $h = $info[1];

        #2        
        switch ($info[2]){
            case 1:         #gif
                $im = imagecreatefromgif($picName);
                break;
            case 2:         #jpeg
                $im = imagecreatefromjpeg($picName);
                break;
            case 3:         #png
                $im = imagecreatefrompng($picName);
                break;
            default :
                die('image style error!');
        }

        #3       
        if(($max_w/$w) > ($max_h/$h)){
            $b = $max_h/$h;
        }else{
            $b = $max_w/$w;
        }

        #4         
        $nw = floor($w*$b);
        $nh = floor($h*$b);

        #5           -    
        $nim = imagecreatetruecolor($nw, $nh);

        #      
        imagecopyresampled($nim, $im, 0, 0, 0, 0, $nw, $nh, $w, $h);

        #6      -         
        $picInfo = pathinfo($picName);        #             

        $new_picName = $picInfo['dirname'].'/'.$picInfo['filename'] .$pre.'.'.$picInfo['extension'];    #      

        switch ($info[2]){
            case 1:         #gif
                imagejpeg($nim, $new_picName);
                break;
            case 2:         #jpeg
                imagejpeg($nim, $new_picName);
                break;
            case 3:         #png
                imagepng($nim, $new_picName);
                break;
            default :
                die('image style error!');
        }

        #7       
        imagedestroy($im);
        imagedestroy($nim);

        return $new_picName;
    }
}

テストgithub