You can use {Y}{m}{n} to become the current date
- Set_dir (DirName (__file__). ' /upload/', ' {y}/{m} '); Save path, support {y}{m}{d} these several options
- $up->set_thumb (100,80); Thumbnail size setting. Units in pixels
- $up->set_watermark (DirName (__file__). ' /jblog/images/watermark.png ', 6,90); Watermark Settings
- $fs = $up->execute (); Start execution
- Var_dump ($FS); Scenarios for testing the view class
- }
- ?>
- View Sheet---------
- Test
- Support Multiple image uploads
- */
- 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} ', ' {d} '), Array (date (' Y ', $this->time), date (' m ', $this->time), date (' d ' , $this->time)), Strtolower ($filedir));//replace {y} {m} {D} with String_replace
- $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 = Date (' Ymdhis ', $this->time). Mt_rand (10,99). '. '. $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] [' name '] = $filesize;
- $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 images and generate thumbnails, where the demo only supports JPG and PNG (GIF generated words will be out of frame)
- if (In_array ($fileext, array (' jpg ', ' png '))) {
- 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
- $SRC _file: Source image path, $thumb _file: Thumbnail path
- function Create_thumb ($src _file, $thumb _file) {
- $t _width = $this->thumb_width;
- $t _height = $this->thumb_height;
- if (!file_exists ($src _file)) return false;
- $src _info = getimagesize ($src _file);
- Copy the source image as a thumbnail if the source image is less than or equal to the thumbnail, eliminating the action
- if ($src _info[0] <= $t _width && $src _info[1] <= $t _height) {
- if (!copy ($src _file, $thumb _file)) {
- return false;
- }
- return true;
- }
- Calculate thumbnail size proportionally
- if (($src _info[0]-$t _width) > ($src _info[1]-$t _height)) {
- $t _height = ($t _width/$src _info[0]) * $SRC _info[1];
- } else {
- $t _width = ($t _height/$src _info[1]) * $SRC _info[0];
- }
- Get file name extension
- $fileext = $this->fileext ($src _file);
- Switch ($fileext) {
- Case ' jpg ':
- $src _img = imagecreatefromjpeg ($src _file); Break
- Case ' PNG ':
- $src _img = imagecreatefrompng ($src _file); Break
- Case ' gif ':
- $src _img = imagecreatefromgif ($src _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, $src _img,0,0,0,0, $t _width, $t _height, $src _info[0], $src _info[1]);
- } else {
- @ImageCopyResized ($thumb _img, $src _img,0,0,0,0, $t _width, $t _height, $src _info[0], $src _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 ($src _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 Code
|