- /**
- * Upload Images
- */
- Class imgupload{
- static protected $a;
- protected $formName; Form name
- protected $directory; File Upload to Directory
- protected $maxSize; Maximum file Upload size
- protected $canUpload; Whether you can upload
- protected $doUpFile; File name of the upload
- protected $SM _file; Thumbnail name
- Private function __construct ($_formname= ' file ', $_directory= './images/uploads/', $_maxsize=1048576) {//1024*1024=1m
- Initialize parameters
- $this->formname = $_formname;
- $this->directory = $_directory;
- $this->maxsize = $_maxsize;
- $this->canupload = true;
- $this->doupfile = ";
- $this->sm_file = ";
- }
- Determine if the picture is in the allowed format
- static public Function Type ($_formname= ' file ') {
- $_type = $_files[$_formname][' type '];
- Switch ($_type) {
- Case ' image/gif ':
- if (self:: $a ==null)
- Self:: $a = new Imgupload ($_formname);
- Break
- Case ' Image/pjpeg ':
- if (self:: $a ==null)
- Self:: $a = new Imgupload ($_formname);
- Break
- Case ' image/x-png ':
- if (self:: $a ==null)
- Self:: $a = new Imgupload ($_formname);
- Break
- Default
- Self:: $a = false;
- }
- Return self:: $a;
- }
- Get File size
- Public Function GetSize ($_format= ' K ') {
- if ($this->canupload) {
- if (0 = = $_files[$this->formname][' size ') {
- $this->canupload = false;
- return $this->canupload;
- Break
- }
- Switch ($_format) {
- Case ' B ':
- return $_files[$this->formname][' size ');
- Break
- Case ' K ':
- Return round ($_files[$this->formname][' size ')/1024);
- Break
- Case ' M ':
- Return round ($_files[$this->formname][' size ')/(1024*1024), 2);
- Break
- }
- }
- }
- Get file type
- Public Function Getext () {
- if ($this->canupload) {
- $_name = $_files[$this->formname][' name '];
- $_namearr = Explode ('. ', $_name);
- $_count = count ($_namearr)-1;
- }
- return $_namearr[$_count];
- }
- Get file name
- Public Function GetName () {
- if ($this->canupload) {
- return $_files[$this->formname][' name '];
- }
- }
- New file name
- Public Function NewName () {
- Return date (' Ymdhis '). Rand (0,9);
- }
- Uploading files
- Public function upload () {
- if ($this->canupload)
- {
- $_getsize = $this->getsize (' B ');
- if (!$_getsize)
- {
- return $_getsize;
- Break
- }
- Else
- {
- $_newname = $this->newname ();
- $_ext = $this->getext ();
- $_doupload = Move_uploaded_file ($_files[$this->formname][' tmp_name '), $this->directory.$_newname. ".". $_ext);
- if ($_doupload)
- {
- $this->doupfile = $_newname;
- }
- return $_doupload;
- }
- }
- }
- Create a thumbnail image
- Public function thumb ($_dstchar= ' _m ', $_max_len=320) {//$_dstchar:_m, _s
- if ($this->canupload && $this->doupfile! = "") {
- $_ext = $this->getext ();
- $_srcimage = $this->directory. $this->doupfile. ".". $_ext;
- Get an array of picture information
- $_date = getimagesize ($_srcimage, & $info);
- $SRC _w = $_date[0]; Source Picture Width
- $SRC _h = $_date[1]; SOURCE Picture High
- $SRC _max_len = max ($src _w, $src _h); Long Edge
- $src _min_len = min ($src _w, $src _h); Short Edge
- $DST _w = "; Target Image width
- $DST _h = "; Target Picture High
- Wide-height scaling, longest side not greater than $_max_len
- if ($src _max_len>$_max_len)
- {
- $percent = $src _min_len/$src _max_len;
- if ($src _w = = $src _max_len)
- {
- $DST _w = $_max_len;
- $DST _h = $percent * $DST _w;
- }
- Else
- {
- $DST _h = $_max_len;
- $DST _w = $percent * $DST _h;
- }
- }
- Else
- {
- $DST _w = $src _w;
- $DST _h = $src _h;
- }
- The location of the source picture when you create a thumbnail image
- $SRC _x = ";
- $src _y = ";
- Determine if the thumbnail is used for the logo, it will be cut
- if (' s_ ' = = $_dstchar) {
- $src _x = $src _w * 0.10;
- $src _y = $src _h * 0.10;
- $SRC _w *= 0.8;
- $SRC _h *= 0.8;
- }
- Determine the type of picture and create a new picture
- Switch ($_date[2]) {
- Case 1:
- $src _im = imagecreatefromgif ($_srcimage);
- Break
- Case 2:
- $src _im = Imagecreatefromjpeg ($_srcimage);
- Break
- Case 3:
- $src _im = imagecreatefrompng ($_srcimage);
- Break
- Case 8:
- $src _im = imagecreatefromwbmp ($_srcimage);
- Break
- }
- Create a new image
- if ($_date[2]==1) {//gif cannot be applied Imagecreatetruecolor
- $DST _im = imagecreate ($dst _w, $dst _h);
- }else {
- $DST _im = Imagecreatetruecolor ($dst _w, $dst _h);
- }
- Thumbnail copy of this image
- $BG = Imagecolorallocate ($dst _im,255,255,0);
- Imagecopyresized ($dst _im, $src _im, 0, 0, $src _x, $src _y, $dst _w, $dst _h, $src _w, $src _h);
- antialiasing a picture
- Imageantialias ($dst _im, true);
- Switch ($_date[2]) {
- Case 1:
- $CR = Imagegif ($dst _im, $this->directory. $this->doupfile.$_dstchar. ".". $_ext, 100);
- Break
- Case 2:
- $CR = imagejpeg ($dst _im, $this->directory. $this->doupfile.$_dstchar. ".". $_ext, 100);
- Break
- Case 3://imagepng has a problem, so use imagejpg instead
- $CR = imagejpeg ($dst _im, $this->directory. $this->doupfile.$_dstchar. ".". $_ext, 100);
- Break
- }
- $CR = imagejpeg ($dst _im, $this->directory.$_dstchar. $this->doupfile, 90);
- if ($CR) {
- $this->sm_file = $this->directory. $this->doupfile.$_dstchar. ".". $_ext;
- return $this->sm_file;
- }else {
- return false;
- }
- }
- Imagedestroy ($dst _im);
- Imagedestroy ($CR);
- }
- Get the file name after uploading
- Public Function Getupfile () {
- if ($this->doupfile!= ") {
- $_ext = $this->getext ();
- return $this->doupfile. ".". $_ext;
- }else {
- return false;
- }
- }
- Get the file full path after uploading
- Public Function Getfilepatch () {
- if ($this->doupfile!= ") {
- $_ext = $this->getext ();
- Return $this->directory. $this->doupfile. ".". $_ext;
- }else {
- return false;
- }
- }
- Get thumbnail file full path
- Public Function Getthumb () {
- if ($this->sm_file!= ") {
- return $this->sm_file;
- }else {
- return false;
- }
- }
- Get the path to the uploaded file
- Public Function getdirectory () {
- if ($this->directory!= ") {
- return $this->directory;
- }else {
- return false;
- }
- }
- }
- ?>
Copy Code
|