You can use {Y}{m}{n} to change the current date
- set_dir(dirname(__FILE__).'/upload/','{y}/{m}'); //Save path, Supports {y}{m}{d} options
- $up->set_thumb(100,80); //Thumbnail size setting. The unit is pixels
- $up->set_watermark(dirname(__FILE__). '/jblog/images/watermark.png',6,90); //Watermark setting
- $fs = $up->execute(); //Start execution
-
- var_dump($fs); //View for testing Class situation
- }
- ?>
- /////View form---------
-
- test
-
- //Support multiple image uploads
-
-
-
-
-
- */
- class upload {
-
- var $dir; //Physical directory where attachments are stored
- var $time; //Customized file upload time
- var $allow_types; //Allow upload attachment types
- var $field; //Upload control name
- var $maxsize; //Maximum allowed file size, unit is KB
-
- var $thumb_width; //Thumbnail width
- var $thumb_height; //Thumbnail height
-
- var $watermark_file; //Watermark image address
- var $watermark_pos; //Watermark Position
- var $watermark_trans;//Watermark transparency
-
- //Constructor
- //$types: File types allowed to be uploaded, $maxsize: Allowed size, $field: Upload control name, $time: Custom upload time
- function upload($types = 'jpg|png', $maxsize = 1024, $field = 'attach', $time = '') {
- $this->allow_types = explode('|',$types);
- $ this->maxsize = $maxsize * 1024;
- $this->field = $field;
- $this->time = $time ? $time : time();
- }
-
- //Set and create file Specific storage directory
- //$basedir: base directory, must be a physical path
- //$filedir: custom subdirectory, available 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));//Use string_replace to replace {y} {m} {d} tags
- $dirs = explode('/',$filedir );
- foreach ($dirs as $d) {
- !empty($d) && $dir .= $d.'/';
- !is_dir($dir) && @mkdir($dir,0777);
- }
- }
- $this->dir = $dir;
- }
-
- //Picture thumbnail settings, no need to set if thumbnails are not 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 There is no need to set it
- //$file: watermark image, $pos: watermark position, $trans: watermark transparency
- function set_watermark ($file, $pos = 6, $trans = 80) {
- $this->watermark_file = $ file;
- $this->watermark_pos = $pos;
- $this->watermark_trans = $trans;
- }
-
- /*——————————————————————-
- Execute file upload, and return an array of file information containing upload success or failure after processing,
- Where: name is File name. When the upload is successful, it is the file name uploaded to the server. If the upload fails, it is the local file name. dir is the physical path to store the attachment on the server. This value does not exist if the upload fails. Size is the size of the attachment. If the upload fails, it does not exist. The existence of this value
- flag is the status identifier, 1 means success, -1 means the file type is not allowed, -2 means 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 the file extension
- $filename = date('Ymdhis',$this->time).mt_rand(10,99).'.'.$ fileext; //Generate file name
- $filedir = $this->dir; //The actual directory where attachments are stored
- $filesize = $_FILES[$field]['size'][$key]; //File size
-
- //File type not allowed
- 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 uploaded files and delete temporary files
- 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;
-
- //Add watermark to the picture and generate thumbnails. The demonstration here only supports jpg and png (if the gif is generated, the frame will be lost)
- 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, 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);
-
- / /If the source image is less than or equal to the thumbnail, copy the source image as the thumbnail, eliminating the need for operation
- 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 the file 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 copied by the ImageCopyResampled function has better smoothness, priority is given
- 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 thumbnails
- 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;
- }
-
- //Destroy temporary image
- @ImageDestroy
-
- return true //If the file does not exist, return
- if (!file_exists($this->watermark_file) || !file_exists($file)) return;
- if (!function_exists('getImageSize')) return;
-
- //Check GD Supported file types
- $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 image from file
- $temp = $gd_allow_types[ $fileinfo['mime']]($file);
- $temp_wm = $gd_allow_types[$wminfo['mime']]($this->watermark_file);
-
- //Watermark position
- 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: //Centered at the bottom
- $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); //Set the color blending mode of the image
- if (function_exists('ImageSaveAlpha')) ImageSaveAlpha($temp_wm,True); //Save the complete alpha channel information
-
- //For the image Add watermark
- 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 the image
- 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 the zero-time image
- @imageDestroy($temp);
- @imageDestroy($temp_wm);
- }
- }
- }
-
- //Get the file extension
- function fileext($filename) {
- return strtolower(substr(strrchr($filename,'.'),1,10));
- }
- }
- ?>
Copy code
|