php文件上传类可生成缩略图代码 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
- <?php
- 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);
- }
-
-
- ?>
- <html>
- <head><title>test</title></head>
- <body style="margin:0;padding:0">
- <form name="upload" method="post" action="?action=save" enctype="multipart/form-data" style="margin:0">
- <input type="file" name="attach[]" />
- <input type="file" name="attach[]" />
- <input type="submit" name="submit" value="上 传" />
- </form>
- </body>
- </html>
- class upload {
-
- var $dir;
- var $time;
- var $allow_types;
- var $field;
- var $maxsize;
-
- var $thumb_width;
- var $thumb_height;
-
- var $watermark_file;
- var $watermark_pos;
- var $watermark_trans;
-
-
-
-
- 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();
- }
-
-
-
-
- function set_dir($basedir,$filedir = '') {
- $dir = $basedir;
- !is_dir($dir) && @mkdir($dir,0777);
- if (!emptyempty($filedir)) {
- $filedir = str_replace(array('{y}','{m}','{d}'),array(date('Y',$this->time),date('m',$this->time),date('d',$this->time)),strtolower($filedir));
- $dirs = explode('/',$filedir);
- foreach ($dirs as $d) {
- !emptyempty($d) && $dir .= $d.'/';
- !is_dir($dir) && @mkdir($dir,0777);
- }
- }
- $this->dir = $dir;
- }
-
-
-
- function set_thumb ($width = 0, $height = 0) {
- $this->thumb_width = $width;
- $this->thumb_height = $height;
- }
-
-
-
- function set_watermark ($file, $pos = 6, $trans = 80) {
- $this->watermark_file = $file;
- $this->watermark_pos = $pos;
- $this->watermark_trans = $trans;
- }
-
-
-
-
-
-
-
-
- function execute() {
- $files = array();
- $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]);
- $filename = date('Ymdhis',$this->time).mt_rand(10,99).'.'.$fileext;
- $filedir = $this->dir;
- $filesize = $_FILES[$field]['size'][$key];
-
-
- if (!in_array($fileext,$this->allow_types)) {
- $files[$key]['name'] = $_FILES[$field]['name'][$key];
- $files[$key]['flag'] = -1;
- continue;
- }
-
-
- 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;
-
-
- 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;
-
-
- 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;
- }
- }
- $this->create_watermark($filedir.$filename);
- }
- }
- }
-
- return $files;
- }
-
-
-
- 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 ($src_info[0] <= $t_width && $src_info[1] <= $t_height) {
- if (!copy($src_file,$thumb_file)) {
- return false;
- }
- return true;
- }
-
-
- 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];
- }
-
-
- $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;
- }
-
-
- $thumb_img = @ImageCreateTrueColor($t_width,$t_height);
-
-
- 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]);
- }
-
-
- 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;
- }
-
-
- @ImageDestroy($src_img);
- @ImageDestroy($thumb_img);
-
- return true;
-
- }
-
-
-
- function create_watermark ($file) {
-
-
- if (!file_exists($this->watermark_file) || !file_exists($file)) return;
- if (!function_exists('getImageSize')) return;
-
-
- $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';
-
-
- $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)) {
-
-
- $temp = $gd_allow_types[$fileinfo['mime']]($file);
- $temp_wm = $gd_allow_types[$wminfo['mime']]($this->watermark_file);
-
-
- switch ($this->watermark_pos) {
- case 1 :
- $dst_x = 0; $dst_y = 0; break;
- case 2 :
- $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = 0; break;
- case 3 :
- $dst_x = $fileinfo[0]; $dst_y = 0; break;
- case 4 :
- $dst_x = 0; $dst_y = $fileinfo[1]; break;
- case 5 :
- $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = $fileinfo[1]; break;
- case 6 :
- $dst_x = $fileinfo[0]-$wminfo[0]; $dst_y = $fileinfo[1]-$wminfo[1]; break;
- default :
- $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);
- if (function_exists('ImageSaveAlpha')) ImageSaveAlpha($temp_wm,True);
-
-
- 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]);
- }
-
-
- switch ($fileinfo['mime']) {
- case 'image/jpeg' :
- @imageJPEG($temp,$file);
- break;
- case 'image/png' :
- @imagePNG($temp,$file);
- break;
- case 'image/gif' :
- @imageGIF($temp,$file);
- break;
- }
-
- @imageDestroy($temp);
- @imageDestroy($temp_wm);
- }
- }
- }
-
-
- function fileext($filename) {
- return strtolower(substr(strrchr($filename,'.'),1,10));
- }
- }
|
|
|
|