-
- //----------------------------------------
- // File description: File upload processing class
- // File author: Jesse Lee
- // Author homepage: http://bbs.it-home.org
- // Last updated: 2011-5-14
- //-- ----------------------------------
- class upload {
- var $dir; //The physical directory where attachments are stored
- var $time; //Customized file upload time
- var $allow_types; //Allow attachment types to upload
- var $field; //Upload control name
- var $maxsize; //Maximum allowed file size in 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
- //Construction Function
- //$types: file types allowed to be uploaded, $maxsize: allowed size, $field: upload control name, $time: customized 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 the specific directory where files are stored
- //$basedir: base directory, must be 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}','{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, 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, no need to set if no watermark is generated
- //$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.
- Among them: name is the 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. The 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. This value does not exist if the upload fails.
- 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 = $this- >time.mt_rand(100,999).'.'.$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]['flag'] = -2;
- continue;
- }
- $files[$key]['name'] = $filename;
- $files[$key]['dir'] = $filedir;
- $files[$key]['size'] = $filesize;
- / /Save the uploaded 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;
- //Add pictures Watermark and generate thumbnails
- 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 thumbnail, generate thumbnail 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);
- //If the source image is less than or equal to the thumbnail, copy the source image as the 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 thumbnails proportionally Size
- 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 the file 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);
- //ImageCopyResampled function The copied image has better smoothness, give priority to
- 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 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 the temporary image
- @ImageDestroy(Php.aspx_img);
- @ImageDestroy($thumb_img);
- return true;
- }
- //Add watermark to the picture
- //$file: The file to be watermarked
- function create_watermark ($file) {
- //Return if the file does not exist
- 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 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 : //bottom centered
- $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 image blending mode
- if (function_exists('ImageSaveAlpha')) ImageSaveAlpha($temp_wm,True); //Save the complete alpha channel information
- //Add watermark to the 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 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 Zero hour image
- @imageDestroy($temp);
- @imageDestroy($temp_wm);
- }
- }
- }
- //Get the file extension
- function fileext($filename) {
- return strtolower(substr(strrchr($filename ,'.'),1,10));
- }
- }
- ?>
Copy code
使用示例:
|