- //-------------------------------------
- File Description: File Upload processing class
- File Author: Jesse Lee
- //-------------------------------------
- Class Upload {
- var $dir; Attachment Storage Physical Directory
- var $time; Custom File Upload Time
- var $allow _types; Allow uploading of attachment types
- var $field; Upload Control Name
- var $maxsize; Maximum allowable file size, in kilobytes per kb
- var $thumb _width; Thumbnail width
- var $thumb _height; Thumbnail height
- var $watermark _file; Watermark Image Address
- var $watermark _pos; Watermark Location
- var $watermark _trans;//Watermark Transparency
- constructor function
- $types: Allowed file types to upload, $maxsize: Allowed size, $field: Upload control name, $time: Custom upload Time
- function upload ($types = ' jpg|png ', $maxsize = 1024x768, $field = ' attach ', $time = ') {
- $this->allow_types = explode (' | ', $types);
- $this->maxsize = $maxsize * 1024;
- $this->field = $field;
- $this->time = $time? $time: Time ();
- }
- Set up and create a directory where files are specifically stored
- $basedir: Base directory, must be a physical path
- $filedir: Custom subdirectory, available with parameters {Y}, {m}, {d}
- function Set_dir ($basedir, $filedir = ") {
- $dir = $basedir;
- !is_dir ($dir) && @mkdir ($dir, 0777);
- if (!empty ($filedir)) {
- $filedir = str_replace (Array (' {y} ', ' {m} ', ' {y} '), Array (date (' Y ', $this->time), date (' m ', $this->time), date (' d ' , $this->time)), Strtolower ($filedir));
- $dirs = explode ('/', $filedir);
- foreach ($dirs as $d) {
- !empty ($d) && $dir. = $d. '/';
- !is_dir ($dir) && @mkdir ($dir, 0777);
- }
- }
- $this->dir = $dir;
- }
- Picture thumbnail settings, not set if no thumbnail is generated
- $width: Thumbnail width, $height: thumbnail height
- function Set_thumb ($width = 0, $height = 0) {
- $this->thumb_width = $width;
- $this->thumb_height = $height;
- }
- Picture watermark settings, if not generated add watermark do not set
- $file: Watermark image, $pos: Watermark location, $trans: Watermark Transparency
- function Set_watermark ($file, $pos = 6, $trans = 80) {
- $this->watermark_file = $file;
- $this->watermark_pos = $pos;
- $this->watermark_trans = $trans;
- }
- /*----------------------------------------------------------------
- Performs a file upload, and returns an array of file information containing the upload success or failure.
- Where: Name is the file name, upload success is uploaded to the server filename, upload failure is the local file name
- Dir is the physical path where the attachment is stored on the server and the upload failure does not exist for this value
- Size is an attachment, upload failure does not exist for this value
- Flag is the status ID, 1 indicates success, 1 indicates that the file type is not allowed, and 2 indicates that the file size exceeds
- ----------------------------------------------------------------- */
- function Execute () {
- $files = Array (); Successfully uploaded file information
- $field = $this->field;
- $keys = Array_keys ($_files[$field [' name ']);
- foreach ($keys as $key) {
- if (!$_files[$field [' name '] [$key]) continue;
- $fileext = $this->fileext ($_files[$field] [' name '] [$key]); Get file name extension
- $filename = $this->time.mt_rand (100,999). '. '. $fileext; Generate file name
- $filedir = $this->dir; Attachment Actual Storage Directory
- $filesize = $_files[$field [' Size '] [$key]; File size
- File type does not allow
- if (!in_array ($fileext, $this->allow_types)) {
- $files [$key] [' name '] = $_files[$field] [' name '] [$key];
- $files [$key] [' flag '] =-1;
- Continue
- }
- File size exceeded
- if ($filesize > $this->maxsize) {
- $files [$key] [' name '] = $_files[$field] [' name '] [$key];
- $files [$key] [' flag '] =-2;
- Continue
- }
- $files [$key] [' name '] = $filename;
- $files [$key] [' dir '] = $filedir;
- $files [$key] [' size '] = $filesize;
- Save the upload file and delete the temporary file
- if (Is_uploaded_file ($_files[$field [' tmp_name '] [$key])) {
- Move_uploaded_file ($_files[$field] [' tmp_name '] [$key], $filedir. $filename);
- @unlink ($_files[$field [' tmp_name '] [$key]);
- $files [$key] [' flag '] = 1;
- Watermark and generate thumbnails for pictures
- if (In_array ($fileext, array (' jpg ', ' png ', ' gif '))) {
- if ($this->thumb_width) {
- if ($this->create_thumb ($filedir. $filename, $filedir. ' Thumb_ '. $filename)) {
- $files [$key] [' thumb '] = ' thumb_ '. $filename; Thumbnail file name
- }
- }
- $this->create_watermark ($filedir. $filename);
- }
- }
- }
- return $files;
- }
- Create thumbnails to generate thumbnails with the same extension
- Php.aspx_file: Source image path, $thumb _file: Thumbnail path
- function Create_thumb (php.aspx_file, $thumb _file) {
- $t _width = $this->thumb_width;
- $t _height = $this->thumb_height;
- if (!file_exists (Php.aspx_file)) return false;
- Php.aspx_info = getimagesize (php.aspx_file);
- Copy source image as thumbnail if source image is less than or equal to thumbnail
- if (php.aspx_info[0] <= $t _width && php.aspx_info[1] <= $t _height) {
- if (!copy (Php.aspx_file, $thumb _file)) {
- return false;
- }
- return true;
- }
- Calculate thumbnail size proportionally
- if (php.aspx_info[0]-$t _width > Php.aspx_info[1]-$t _height) {
- $t _height = ($t _width/php.aspx_info[0]) * php.aspx_info[1];
- } else {
- $t _width = ($t _height/php.aspx_info[1]) * php.aspx_info[0];
- }
- Get file name extension
- $fileext = $this->fileext (php.aspx_file);
- Switch ($fileext) {
- Case ' jpg ':
- php.aspx_img = Imagecreatefromjpeg (php.aspx_file); Break
- Case ' PNG ':
- php.aspx_img = Imagecreatefrompng (php.aspx_file); Break
- Case ' gif ':
- php.aspx_img = Imagecreatefromgif (php.aspx_file); Break
- }
- Create a true-color thumbnail image
- $thumb _img = @ImageCreateTrueColor ($t _width, $t _height);
- The image smoothness of the imagecopyresampled function copy is better, and the first consideration
- if (function_exists (' imagecopyresampled ')) {
- @ImageCopyResampled ($thumb _img,php.aspx_img,0,0,0,0, $t _width, $t _height,php.aspx_info[0],php.aspx_info[1]);
- } else {
- @ImageCopyResized ($thumb _img,php.aspx_img,0,0,0,0, $t _width, $t _height,php.aspx_info[0],php.aspx_info[1]);
- }
- Generate thumbnail images
- Switch ($fileext) {
- Case ' jpg ':
- Imagejpeg ($thumb _img, $thumb _file); Break
- Case ' gif ':
- Imagegif ($thumb _img, $thumb _file); Break
- Case ' PNG ':
- Imagepng ($thumb _img, $thumb _file); Break
- }
- Destroying temporary images
- @ImageDestroy (PHP.ASPX_IMG);
- @ImageDestroy ($thumb _img);
- return true;
- }
- Add a watermark to a picture
- $file: The file to add the watermark to
- function Create_watermark ($file) {
- The file does not exist and returns
- if (!file_exists ($this->watermark_file) | | |!file_exists ($file)) return;
- if (!function_exists (' getimagesize ')) return;
- Check the file types supported by GD
- $GD _allow_types = Array ();
- if (function_exists (' imagecreatefromgif ')) $gd _allow_types[' image/gif '] = ' imagecreatefromgif ';
- if (function_exists (' imagecreatefrompng ')) $gd _allow_types[' image/png '] = ' imagecreatefrompng ';
- if (function_exists (' imagecreatefromjpeg ')) $gd _allow_types[' image/jpeg '] = ' imagecreatefromjpeg ';
- Get file information
- $fileinfo = getimagesize ($file);
- $wminfo = getimagesize ($this->watermark_file);
- if ($fileinfo [0] < $wminfo [0] | | $fileinfo [1] < $wminfo [1]) return;
- if (array_key_exists ($fileinfo [' MIME '], $GD _allow_types)) {
- if (array_key_exists ($wminfo [' MIME '], $GD _allow_types)) {
- Create an image from a file
- $temp = $gd _allow_types[$fileinfo [' MIME ']] ($file);
- $temp _wm = $gd _allow_types[$wminfo [' MIME ']] ($this->watermark_file);
- Watermark Location
- Switch ($this->watermark_pos) {
- Case 1://Top Left
- $DST _x = 0; $DST _y = 0; Break
- Case 2://Top Center
- $DST _x = ($fileinfo [0]-$wminfo [0])/2; $DST _y = 0; Break
- Case 3://Top Right
- $DST _x = $fileinfo [0]; $DST _y = 0; Break
- Case 4://Bottom Left
- $DST _x = 0; $DST _y = $fileinfo [1]; Break
- Case 5://Bottom Center
- $DST _x = ($fileinfo [0]-$wminfo [0])/2; $DST _y = $fileinfo [1]; Break
- Case 6://Bottom Right
- $DST _x = $fileinfo [0]-$wminfo [0]; $DST _y = $fileinfo [1]-$wminfo [1]; Break
- Default://Random
- $DST _x = Mt_rand (0, $fileinfo [0]-$wminfo [0]); $DST _y = Mt_rand (0, $fileinfo [1]-$wminfo [1]);
- }
- if (function_exists (' imagealphablending ')) imagealphablending ($temp _wm,true); To set the color blending mode of an image
- if (function_exists (' Imagesavealpha ')) Imagesavealpha ($temp _wm,true); To save the full alpha channel information
- Add a watermark to an image
- if (function_exists (' Imagecopymerge ')) {
- Imagecopymerge ($temp, $temp _wm, $dst _x, $dst _y,0,0, $wminfo [0], $wminfo [1], $this->watermark_trans);
- } else {
- Imagecopymerge ($temp, $temp _wm, $dst _x, $dst _y,0,0, $wminfo [0], $wminfo [1]);
- }
- Save picture
- Switch ($fileinfo [' MIME ']) {
- Case ' Image/jpeg ':
- @imageJPEG ($temp, $file);
- Break
- Case ' image/png ':
- @imagePNG ($temp, $file);
- Break
- Case ' image/gif ':
- @imageGIF ($temp, $file);
- Break
- }
- Destroy Zero image
- @imageDestroy ($temp);
- @imageDestroy ($temp _wm);
- }
- }
- }
- Get file name extension
- function Fileext ($filename) {
- Return Strtolower (substr (STRRCHR ($filename, '. '), 1,10));
- }
- }
- ?>
Copy CodeInvocation Example:
- if ($_get[' action '] = = ' Save ') {
- $up = new Upload ();
- $up->set_dir (DirName (__file__). ' /upload/', ' {y}/{m} ');
- $up->set_thumb (100,80);
- $up->set_watermark (DirName (__file__). ' /jblog/images/watermark.png ', 6,90);
- $fs = $up->execute ();
- Var_dump ($FS);
- }
- ?>
- Test
Copy Code |