PHP image upload Class (Support zoom, crop, picture thumbnail function)

Source: Internet
Author: User
Tags imagejpeg

  • Code:
    /*** @author [Lee] <[<[email protected]>]>* 1, automatically verify whether the file is a form submitted by a file or a file submitted by a base64 stream * 2, verify that the picture type is legal * 3, Verify that the picture size is legal * 4, verify that the picture size is legal * 5, Support zoom function * 6, support cropping function * 7, support thumbnail function */class fileuploader{private $file;p rivate $type;p ublic $suffix = Array ();p ublic $measure = Array ();p ublic $size = Array ();p ublic $scale = Array (' Is_scale ' =>0, ' ratio ' = >1);p ublic $crop = Array (' Is_crop ' =>0, ' width ' =>0, ' height ' =>0);p ublic $thumb = Array (' Is_thumb ' =>0, ' width ' =>0, ' height ' =>0);p ublic function __construct ($file) {$t    His->file = $file; $this->gettype ();}    /* Internal method: Get Picture Type 1, multiple file 2, single file 3, Base64 stream file */private function GetType () {$file = $this->file;        if (Is_array ($file)) {//Multiple file if (Is_array ($file [' name '])) {$type = 1;        }else{//Single File $type = 2;    }}else{$type = 3; } $this->type = $type;} /* Internal method: Get the picture suffix @param path picture @return suffix suffix name: JPG */private function Getsuffix ($path) {$file = fopen ($path, "RB"); $bin = Fread ($file, 2);    Read-only 2-byte fclose ($file);    $strInfo = @unpack ("C2chars", $bin);    $typeCode = Intval ($strInfo [' chars1 ']. $strInfo [' chars2 ']);    $suffix = "Unknow";    if ($typeCode = = 255216) {$suffix = "jpg";    }elseif ($typeCode = = 7173) {$suffix = "gif";    }elseif ($typeCode = = 13780) {$suffix = "png";    }elseif ($typeCode = = 6677) {$suffix = "bmp";    }elseif ($typeCode = = 7798) {$suffix = "EXE";    }elseif ($typeCode = = 7784) {$suffix = "midi";    }elseif ($typeCode = = 8297) {$suffix = "rar";    }elseif ($typeCode = = 7368) {$suffix = "MP3";    }elseif ($typeCode = = 0) {$suffix = "mp4";    }elseif ($typeCode = = 8273) {$suffix = "wav"; } return $suffix;} /* Internal method: Save stream file @param stream file stream @param suffix suffix @param dir save folder @return Path file */private function uploadBase64 ($strea  M, $suffix, $dir) {if (empty ($stream)) return false;  if (Preg_match ('/^ data: (\s)? ( IMAGE|IMG) \ (\w+), base64,)/', $stream, $str)) {$path = $dir. MD5 (rand (100000, 999999)). ".        {$suffix} ";        if (File_put_contents ($path, Base64_decode (Str_replace ($str [1], ", $stream))) {return $path;        } else {return false;    }} else {return false;    }}/* Check whether the file type is legal @return RET true: Valid false: illegal */public function Checktype () {$file = $this->file;    $type = $this->type;    $validSuffix = $this->suffix;    $ret = true;            if ($type ==1) {//multi-file foreach ($file [' Tmp_name '] as $v) {$suffix = $this->getsuffix ($v);                if (!in_array ($suffix, $validSuffix)) {$ret = false;            Break        }}}elseif ($type ==2) {//single file $suffix = $this->getsuffix ($file [' tmp_name ']);        if (!in_array ($suffix, $validSuffix)) {$ret = false;        }}else{//base64 file $suffix = $this->getsuffix ($file);if (!in_array ($suffix, $validSuffix)) {$ret = false; }} return $ret;}    /* Verify file size is legal @return ret true: Valid false: illegal */public function checkmeasure () {$file = $this->file;    $type = $this->type;    $validMeasure = $this->measure;    $ret = true;            if ($type ==1) {//multi-file foreach ($file [' Tmp_name '] as $v) {$measure = getimagesize ($v);            $width = $measure [0];            $height = $measure [1]; if ($width < $validMeasure [' width '][0] | | $width > $validMeasure [' Width '][1]) | | ($height < $validMeasure [' height '][0] | | $height > $validMeasure [' Height '][1])                {$ret = false;            Break        }}}elseif ($type ==2) {//single file $measure = getimagesize ($file [' tmp_name ']);        $width = $measure [0];        $height = $measure [1]; if ($width < $validMeasure [' width '][0] | | $width > $validMeasure [' Width '][1]) | | ($height < $validMeasure [' height '][0] | | $height >$validMeasure [' height '][1]) {$ret = false;        }}else{//base64 file $measure = getimagesize ($file);        $width = $measure [0];        $height = $measure [1]; if ($width < $validMeasure [' width '][0] | | $width > $validMeasure [' Width '][1]) | | ($height < $validMeasure [' height '][0] | | $height > $validMeasure [' Height '][1])        {$ret = false; }} return $ret;}    /* Verify file size is legal @return ret true: Valid false: illegal */public function checksize () {$file = $this->file;    $type = $this->type;    $validSize = $this->size;    $ret = true;            if ($type ==1) {//multi-file foreach ($file [' Tmp_name '] as $v) {$size = FileSize ($v); if ($size < $validSize [' min ']*1024*1024) | | ($size > $validSize [' Max ']*1024*1024)]                {$ret = false;            Break        }}}elseif ($type ==2) {//single file $size = FileSize ($file [' tmp_name ']); if ($size < $validSize [' min ']*1024*1024) | | ($size > $validSize [' Max ']*1024*1024)} {$ret = false;        }}else{//base64 file $size = filesize ($file); if ($size < $validSize [' min ']*1024*1024) | | ($size > $validSize [' Max ']*1024*1024)]        {$ret = false; }} return $ret;}      /* Internal method: Zoom picture */private function Scale ($path, $suffix, $ratio) {list ($width, $height) = getimagesize ($path);    $new _w = $ratio * $width;    $new _h = $ratio * $height;    $new _s = Imagecreatetruecolor ($new _w, $new _h);    if (In_array ($suffix, array (' jpg ', ' jpeg '))) {$img = Imagecreatefromjpeg ($path);    }elseif ($suffix = = ' png ') {$img = Imagecreatefrompng ($path);    }elseif ($suffix = = ' gif ') {$img = Imagecreatefromgif ($path);    }else{return false;      } $ret 1 = imagecopyresized ($new _s, $img, 0, 0, 0, 0, $new _w, $new _h, $width, $height);    if (In_array ($suffix, array (' jpg ', ' jpeg '))) {$ret 2 = imagejpeg ($new _s, $path); }elseif ($suffix = = ' png ') {$ret 2 = imagepng ($new _s, $path);    }elseif ($suffix = = ' gif ') {$ret 2 = imagegif ($new _s, $path);    }else{return false;      } Imagedestroy ($new _s);    Imagedestroy ($IMG);    if ($ret 1 && $ret 2) {return $path;    }else{return false;    }}/* Internal method: Crop picture */private function crop ($path, $suffix, $cut _width, $cut _height) {$cut _x;    $cut _y;    $min;    $size = getimagesize ($path);    $width = $size [0];    $height = $size [1];    $min = min ($width, $height);    $cut _width = ($cut _width > $min) $min: $cut _width;    $cut _height = ($cut _height > $min) $min: $cut _height;    $cut _x = ($width-$cut _width)/2;    $cut _y = ($height-$cut _height)/2;    if (In_array ($suffix, array (' jpg ', ' jpeg '))) {$img = Imagecreatefromjpeg ($path);    }elseif ($suffix = = ' png ') {$img = Imagecreatefrompng ($path);    }elseif ($suffix = = ' gif ') {$img = Imagecreatefromgif ($path);    }else{return false;    } $new _s = Imagecreatetruecolor ($cut _width, $cut _height); $rEt1 = imagecopyresampled ($new _s, $img, 0, 0, $cut _x, $cut _y, $cut _width, $cut _height, $cut _width, $cut _height);    if (In_array ($suffix, array (' jpg ', ' jpeg '))) {$ret 2 = imagejpeg ($new _s, $path);    }elseif ($suffix = = ' png ') {$ret 2 = imagepng ($new _s, $path);    }elseif ($suffix = = ' gif ') {$ret 2 = imagegif ($new _s, $path);    }else{return false;    } Imagedestroy ($new _s);    Imagedestroy ($IMG);    if ($ret 1 && $ret 2) {return $path;    }else{return false;    }}/* internal methods: Generate thumbnails */private function thumb ($path, $suffix, $cut _width, $cut _height) {$cut _x;    $cut _y;    $ratio = 1;    $size = getimagesize ($path);    $width = $size [0];    $height = $size [1];    $CW;    $ch;    if ($width/$height >= $cut _width/$cut _height) {$ratio = $cut _height/$height;    }else{$ratio = $cut _width/$width;    } $path = $this->scale ($path, $suffix, $ratio, $path);    $width *= $ratio;    $height *= $ratio; $cut _x = ABS ($cut _width-$width)/2;    $cut _y = ABS ($cut _height-$height)/2;    if (In_array ($suffix, array (' jpg ', ' jpeg '))) {$img = Imagecreatefromjpeg ($path);    }elseif ($suffix = = ' png ') {$img = Imagecreatefrompng ($path);    }elseif ($suffix = = ' gif ') {$img = Imagecreatefromgif ($path);    }else{return false;    } $new _s = Imagecreatetruecolor ($cut _width, $cut _height);      $ret 1 = imagecopyresampled ($new _s, $img, 0, 0, $cut _x, $cut _y, $cut _width, $cut _height, $cut _width, $cut _height);    if (In_array ($suffix, array (' jpg ', ' jpeg '))) {$ret 2 = imagejpeg ($new _s, $path);    }elseif ($suffix = = ' png ') {$ret 2 = imagepng ($new _s, $path);    }elseif ($suffix = = ' gif ') {$ret 2 = imagegif ($new _s, $path);    }else{return false;    } Imagedestroy ($new _s);    Imagedestroy ($IMG);    if ($ret 1 && $ret 2) {return $path;    }else{return false; }}/* Save @param dir stored folder such as: './' @return RET store file path such as: './test.jpg ' */public function Save ($dir) {$file = $this->file;    $type = $this->type;    $scale = $this->scale;    $crop = $this->crop;    $thumb = $this->thumb;    $is _scale = $scale [' Is_scale '];    $is _crop = $crop [' Is_crop '];    $is _thumb = $thumb [' Is_thumb '];    $ratio = $scale [' ratio '];    $crop _width = $crop [' width '];    $crop _height = $crop [' height '];    $thumb _width = $thumb [' width '];    $thumb _height = $thumb [' height '];            if ($type ==1) {//multi-file foreach ($file [' Tmp_name '] as $k = + $v) {$suffix = $this->getsuffix ($v); $name = $dir. MD5 (rand (100000,999999)).            $suffix;            $flag = File_put_contents ($name, file_get_contents ($v));            if (! $flag) {$ret = false;                }else{if ($is _scale) {$name = $this->scale ($name, $suffix, $ratio);                if ($is _crop) {$name = $this->crop ($name, $suffix, $crop _width, $crop _height);            } if ($is _thumb) {        $name = $this->thumb ($name, $suffix, $thumb _width, $thumb _height);            } $ret [$k] = $name;        }}}elseif ($type ==2) {//single file $suffix = $this->getsuffix ($file [' tmp_name ']); $name = $dir. MD5 (rand (100000,999999)).        $suffix;        $flag = File_put_contents ($name, file_get_contents ($file [' tmp_name ']);        if (! $flag) {$ret = false;            }else{if ($is _scale) {$name = $this->scale ($name, $suffix, $ratio);            if ($is _crop) {$name = $this->crop ($name, $suffix, $crop _width, $crop _height);            if ($is _thumb) {$name = $this->thumb ($name, $suffix, $thumb _width, $thumb _height);        } $ret = $name;        }}else{//base64 file $suffix = $this->getsuffix ($file); $name = $dir. MD5 (rand (100000,999999)).        $suffix;        $flag = File_put_contents ($name, file_get_contents ($file));if (! $flag) {$ret = false;            }else{if ($is _scale) {$name = $this->scale ($name, $suffix, $ratio);            if ($is _crop) {$name = $this->crop ($name, $suffix, $crop _width, $crop _height);            if ($is _thumb) {$name = $this->thumb ($name, $suffix, $thumb _width, $thumb _height);        } $ret = $name; }} return $ret;}}
  • Example usage:
    $file = $_FILES[‘file‘];$fileuploader = new fileuploader($file);$fileuploader->thumb = array(    ‘is_thumb‘=>1,    ‘width‘=>200,    ‘height‘=>500);$ret = $fileuploader->save(‘./‘);var_dump($ret);
  • PHP image upload Class (Support zoom, crop, picture thumbnail function)

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.