Share a PHP image upload class, and can automatically generate thumbnails, the need for friends, can make a reference, hope can be useful to you. PHP implementation of the picture file upload class, code:
SaveFile (); # Save the method and return the file name accompanying the save path; @ echo makebuild ($BuildFile, $newFile, $File _width); Generates a thumbnail of the specified file; $myupload->makebuild ("images/ A.jpg "," news/b.jpg "," */class "), Upfileclass{var $upfile, $upfile _name, $upfile _size;var $new _upfile_name; # The file name after uploading; var $fleth, $fileExtent; # file extension (type); var $f 1, $f 2, $f 3; # File save path (multilevel) Upfiles/2008-01/08/;var $filename; # file (with path); var $maxSize, $File _type; # Allow upload file size allows uploading of file types; var $BuildFile, $newFile, $File _width, $File _height, $rate; function Upfileclass ($upfile, $upfile _ Name, $upfile _size) {$this->upfile = $upfile; $this->upfile_name = $upfile _name; $this->upfile_size = $upfile _size; $this->new_upfile_name = $this->createnewfilename ($this->upfile_name); $this->f1 = "Upfiles"; $this->f2 = $this->f1. " /". Date (' Y ')." -". Date (' m '); $this->f3 = $this->f2." /". Date (' d '); $this->filename = $this->f3. "/" . $this->new_upfile_name; $this->maxsize = 500*1024; # File Size 500KB $this->file_type = "gif/jPg/jpeg/png "; # Allow uploaded file type}# Create new file name (original filename) function createnewfilename ($file _name) {$this->fleth = explode (".", $file _name); $this->fileextent = $this->fleth[(int) count ($this->fleth)-1]; # get file suffix; $tmpstr = Date (' Ymdhis '). ".". $this->fileextent; # Create a new file name; return $TMPSTR;} # detects if the file type is correct function chk_fileextent () {$iwTrue = 0; $fle = Explode ("/", $this->file_type); for ($i =0; $i < count ($fle), $i + +) {if ($this->fileextent = = $fle [$i]) {$iwTrue = (int) $iwTrue + 1; }} if ($iwTrue = = 0) {$this->msg ("file does not match". $this->file_type. "Format!"); }}# prompts for error message and terminates Operation function msg ($ERROR) {echo "/n"; Die ();} # Save File Function SaveFile () {$this->chk_fileextent (); $this->chk_filesize (); $this->createfolder (". /". $this->f1); $this->createfolder (". /". $this->f2); $this->createfolder (". /". $this->f3); return $this->chk_savefile ();} # Check if the upload result is successful function chk_savefile () {$copymsg = copy ($this->upfile, ": /". $this->filename); if ($copymsg) {return $this->filename; } else{$this->msg ("File upload failed! /n/n please re-upload! "); }}# Create folder function CreateFolder ($foldername) {if (!is_dir ($foldername)) {mkdir ($foldername, 0777); }}# detect file Size function Chk_filesize () {if ($this->upfile_size > $this->maxsize) {$this->msg ("Destination file cannot be greater than". $thi s->maxsize/1024. " KB "); }}# Delete file ($filePath file relative path) function DeleteFile ($filePath) {if (!is_file ($filePath)) {return false; } else{$ending = @unlink ($filePath); return $ending; }}/* function: Generate thumbnail image makebuild ("Images/a.jpg", "news/b.jpg", "100"); Parameters: Echo $BuildFile; The original with the path echo $newFile; Generated thumbnail with path echo $File _width; Thumbnail width value echo $File _height; Thumbnail height value (default is the width of the scale value) echo $rate; thumbnail image quality; */function makebuild ($BuildFile, $newFile, $File _width, $File _height=0, $rate =100) {if (!is_file ($BuildFile)) { $this->msg ("file". $BuildFile. "is not a valid graphics file! /n/n system cannot generate thumbnails for this file! "); return false; } $data = GetimagesiZe ($BuildFile); Switch ($data [2]) {Case 1: $im = @ImageCreateFromGIF ($BuildFile); Break Case 2: $im = @ImageCreateFromJPEG ($BuildFile); Break Case 3: $im = @ImageCreateFromPNG ($BuildFile); Break } if (! $im) {return false; } else{$srcW =imagesx ($im); # get the original width; $srcH =imagesy ($im); # get the original height; $dstX = 0; $dstY = 0; if ($File _height==0) {$File _height = $File _width/$srcW * $srcH; if ($srcW * $File _height> $srcH * $File _width) {$fFile _height = round ($srcH * $File _width/$srcW); $dstY = Floor (($File _height-$fFile _height)/2); $fFile _width = $File _width; } else {$fFile _width = round ($srcW * $File _height/$srcH); $dstX = Floor (($File _width-$fFile _width)/2); $fFile _height = $File _height; } $ni = Imagecreatetruecolor ($File _width, $File _height); $dstX = ($dstX <0)? 0: $dstX; $dstY = ($dstX <0)? 0: $dstY; $dstX = ($dstX > ($File _width/2)) floor ($File _WIDTH/2): $dstX; $dstY= ($dstY > ($File _height/2)) floor ($File _height/s): $dstY; Imagecopyresized ($ni, $im, $dstX, $dstY, 0,0, $fFile _width, $fFile _height, $srcW, $srcH); Imagejpeg ($ni, $newFile, $rate); # Generate thumbnails; Imagedestroy ($im); # Imagedestroy (Resource) release image Associated Memory}}}?> |