/** * Uploadify上传文件处理 */ public function hd_uploadify() { $uploadModel = M('upload'); //上传文件类型 if (isset($_POST['type'])) { $type = str_replace('*.', '', $_POST['type']); $type = explode(';', $type); } else { $type = array(); } $size = Q('size') ? Q('size') : C('allow_size'); $upload = new Upload(Q('upload_dir'), $type, $size); $file = $upload->upload(); if (!empty($file)) { $file = $file[0]; $file['uid'] = $_SESSION['user']['uid']; //图片加水印 if ($file['image'] && Q('water')) { $img = new Image(); $img->water($file['path']); } //写入upload表 $uploadModel->add($file); $data = $file; $data['status'] = 1; } else { $data['status'] = 0; $data['message'] = $upload->error; } echo json_encode($data); exit; }
/** * Uploadify上传文件处理 */ public function hd_uploadify() { $uploadModel = M('upload'); $size = Q('size') ? Q('size') : C('allow_size'); $upload = new Upload(Q('post.upload_dir'), array(), $size); $file = $upload->upload(); if (!empty($file)) { $file = $file[0]; $file['uid'] = session('uid'); //图片加水印 if ($file['image'] && Q('water')) { $img = new Image(); $img->water($file['path']); } //写入upload表 $uploadModel->add($file); $data = $file; $data['status'] = 1; $data['isimage'] = $file['image'] ? 1 : 0; } else { $data['status'] = 0; $data['message'] = $upload->error; } echo json_encode($data); exit; }
function do_it($image, $ImgWaterPath, $ImgWaterPos, $ImageWaterAlpha, $i) { echo "正在处理第" . $i . "张图片:" . $image . "<br/>"; flush(); ob_flush(); Image::water($image, $ImgWaterPath, $ImgWaterPos, '', $ImageWaterAlpha); }
public function down_img($url, $mid = 'video') { $chr = strrchr($url, '.'); $imgUrl = uniqid(); $imgPath = $mid . '/' . date(C('upload_style'), time()) . '/'; $imgPath_s = './' . C('upload_path') . '-s/' . $imgPath; $filename = './' . C('upload_path') . '/' . $imgPath . $imgUrl . $chr; $get_file = get_collect_file($url); if ($get_file) { write_file($filename, $get_file); //是否添加水印 if (C('upload_water')) { import('ORG.Util.Image'); Image::water($filename, C('upload_water_img'), '', C('upload_water_pct'), C('upload_water_pos')); } //是否生成缩略图 if (C('upload_thumb')) { mkdirss($imgPath_s); import('ORG.Util.Image'); Image::thumb($filename, $imgPath_s . $imgUrl . $chr, '', C('upload_thumb_w'), C('upload_thumb_h'), true); } //是否上传远程 if (C('upload_ftp')) { $this->ftp_upload($imgPath . $imgUrl . $chr); } return $imgPath . $imgUrl . $chr; } else { return $url; } }
public function upload() { echo '<div style="font-size:12px; height:30px; line-height:30px">'; $uppath = './' . C('upload_path') . '/'; $uppath_s = './' . C('upload_path') . '-s/'; $mid = trim($_POST['mid']); $fileback = !empty($_POST['fileback']) ? trim($_POST['fileback']) : 'picurl'; if ($mid) { $uppath .= $mid . '/'; $uppath_s .= $mid . '/'; $backpath = $mid . '/'; } import("ORG.Net.UploadFile"); $up = new UploadFile(); //$up->maxSize = 3292200; $up->savePath = $uppath; $up->saveRule = uniqid; $up->uploadReplace = true; $up->allowExts = explode(',', C('cms_exts')); $up->autoSub = true; $up->subType = date; $up->dateFormat = C('upload_style'); if (!$up->upload()) { $error = $up->getErrorMsg(); if ($error == '上传文件类型不允许') { $error .= ',可上传<font color=red>' . C('cms_exts') . '</font>'; } exit($error . ' [<a href="?s=Admin/Upload/Show/mid/' . $mid . '/fileback/' . $fileback . '">重新上传</a>]'); //dump($up->getErrorMsg()); } $uploadList = $up->getUploadFileInfo(); //是否添加水印 if (C('upload_water')) { import("ORG.Util.Image"); Image::water($uppath . $uploadList[0]['savename'], C('upload_water_img'), '', C('upload_water_pct'), C('upload_water_pos')); } //是否生成缩略图 if (C('upload_thumb')) { $thumbdir = substr($uploadList[0]['savename'], 0, strrpos($uploadList[0]['savename'], '/')); mkdirss($uppath_s . $thumbdir); import("ORG.Util.Image"); Image::thumb($uppath . $uploadList[0]['savename'], $uppath_s . $uploadList[0]['savename'], '', C('upload_thumb_w'), C('upload_thumb_h'), true); } //是否远程图片 if (C('upload_ftp')) { $img = D('Down'); $img->ftp_upload($backpath . $uploadList[0]['savename']); } echo "<script type='text/javascript'>parent.document.getElementById('" . $fileback . "').value='" . $backpath . $uploadList[0]['savename'] . "';</script>"; echo '文件<a href="' . $uppath . $uploadList[0]['savename'] . '" target="_blank"><font color=red>' . $uploadList[0]['savename'] . '</font></a>上传成功 [<a href="?s=Admin/Upload/Show/mid/' . $mid . '/fileback/' . $fileback . '">重新上传</a>]'; echo '</div>'; }
public function upload() { $upload = new \Think\Upload(); // 实例化上传类 $upload->savePath = './Uploads/'; $info = $upload->upload($_FILES); if ($info) { import('Common/Org/Util/Image'); $img = new \Image(); $img->water($info['upfile']['savepath'] . $info['upfile']['savename']); echo json_encode(array('url' => ltrim($info['upfile']['savepath'] . $info['upfile']['savename'], '.'), 'title' => htmlspecialchars($_POST['pictitle'], ENT_QUOTES), 'original' => $info['upfile']['name'], 'state' => 'SUCCESS')); } else { echo json_encode(array('state' => $upload->getError())); } }
protected function _upload() { import('@.ORG.UploadFile'); //导入上传类 $upload = new UploadFile(); //设置上传文件大小 $upload->maxSize = 3292200; //设置上传文件类型 $upload->allowExts = explode(',', 'jpg,gif,png,jpeg'); //设置附件上传目录 $upload->savePath = './Uploads/'; //设置需要生成缩略图,仅对图像文件有效 $upload->thumb = true; // 设置引用图片类库包路径 $upload->imageClassPath = '@.ORG.Image'; //设置需要生成缩略图的文件后缀 $upload->thumbPrefix = 'm_,s_'; //生产2张缩略图 //设置缩略图最大宽度 $upload->thumbMaxWidth = '400,100'; //设置缩略图最大高度 $upload->thumbMaxHeight = '400,100'; //设置上传文件规则 $upload->saveRule = 'uniqid'; //删除原图 $upload->thumbRemoveOrigin = true; if (!$upload->upload()) { //捕获上传异常 $this->error($upload->getErrorMsg()); } else { //取得成功上传的文件信息 $uploadList = $upload->getUploadFileInfo(); import('@.ORG.Image'); //给m_缩略图添加水印, Image::water('原文件名','水印图片地址') Image::water($uploadList[0]['savepath'] . 'm_' . $uploadList[0]['savename'], APP_PATH . 'Tpl/Public/Images/logo.png'); $_POST['image'] = $uploadList[0]['savename']; } $model = M('Photo'); //保存当前数据对象 $data['image'] = $_POST['image']; $data['create_time'] = NOW_TIME; $list = $model->add($data); if ($list !== false) { $this->success('上传图片成功!'); } else { $this->error('上传图片失败!'); } }
public function upload() { import('ORG.Net.UploadFile'); $upload = new UploadFile(); $upload->autoSub = true; $upload->subType = 'date'; $upload->dateFormat = 'Ym'; if ($upload->upload('./Uploads/')) { $info = $upload->getUploadFileInfo(); // import('ORG.Util.Image'); // Image::water('./Uploads/' . $info[0]['savename'], './Data/fff.png'); import('Class.Image', APP_PATH); Image::water('./Uploads/' . $info[0]['savename'], './Data/fff.png'); echo json_encode(array('url' => $info[0]['savename'], 'title' => htmlspecialchars($POST['pictitle'], ENT_QUOTES), 'original' => $info[0]['name'], 'state' => 'SUCCESS')); } else { echo json_encode(array('state' => $upload->getErrorMsg())); } }
public function index() { import('ORG.Net.UploadFile'); $upload = new UploadFile(); $upload->autoSub = true; $upload->subType = 'date'; $upload->dateFormat = 'Ym'; $CONFIG = json_decode(preg_replace("/\\/\\*[\\s\\S]+?\\*\\//", "", file_get_contents(CONF_PATH . "config.json")), true); $action = htmlspecialchars($_GET['action']); switch ($action) { case 'config': $result = json_encode($CONFIG); break; /* 上传图片 */ /* 上传图片 */ case 'uploadimage': if ($upload->upload('./Uploads/')) { $info = $upload->getUploadFileInfo(); //引入外部的图像类处理水印 import('Class.Image', APP_PATH); Image::water('./Uploads/' . $info[0]['savename']); echo json_encode(array('url' => __ROOT__ . '/Uploads/' . $info[0]['savename'], 'original' => $info[0]['name'], 'state' => 'SUCCESS')); } else { echo json_encode(array('state' => $upload->getErrorMsg())); } break; default: $result = json_encode(array('state' => '请求地址出错')); break; } /* 输出结果 */ if (isset($_GET["callback"])) { if (preg_match("/^[\\w_]+\$/", $_GET["callback"])) { echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')'; } else { echo json_encode(array('state' => 'callback参数不合法')); } } else { echo $result; } }
/** * 多文件上传 * * @param boolean $thumb [description] * @return [type] [description] */ public static function uploads($params = false) { $conf = Config::get('', 'base'); $params['thumbSize'] = array(400, 400); Yii::import('application.vendors.*'); require_once 'Tp/UploadFile.class.php'; $upload = new UploadFile(); // 设置上传文件大小 $maxSize = isset($params['maxSize']) ? $params['maxSize'] : $conf['upload_max_size']; $upload->maxSize = $maxSize * 1024; // 设置上传文件类型 $upload->allowExts = isset($params['allowExts']) ? explode(',', $params['allowExts']) : explode(',', $conf['upload_allow_ext']); // 设置附件上传目录 $upload->savePath = self::_saveRule($params); // 设置需要生成缩略图,仅对图像文件有效 $upload->thumb = isset($params['thumb']) ? $params['thumb'] : $conf['thumb']; // 设置需要生成缩略图的文件后缀 $upload->thumbPrefix = 'thumb_'; // 生产2张缩略图 // 设置缩略图最大宽度 $upload->thumbMaxWidth = $params['thumbSize'][0]; // 设置缩略图最大高度 $upload->thumbMaxHeight = $params['thumbSize'][1]; // 设置上传文件规则 $upload->saveRule = uniqid; // 删除原图 $upload->thumbRemoveOrigin = false; if (!$upload->upload()) { return $upload->getErrorMsg(); } else { $fileinfo = $upload->getUploadFileInfo(); require_once 'Tp/Image.class.php'; Image::water($fileget['pathname'], './' . $conf['upload_water_file'], null, $conf['upload_water_trans']); foreach ($fileinfo as $key => $row) { if (true == $upload->thumb) { $fileinfo[$key]['thumb'] = $upload->thumbPrefix . $fileinfo[$key]['savename']; } $fileinfo[$key]['pathname'] = $upload->savePath . $fileinfo[$key]['savename']; $fileinfo[$key]['paththumbname'] = $upload->savePath . $upload->thumbPrefix . $fileinfo[$key]['savename']; if ($conf['upload_water_status'] == 'open') { Image::water($fileinfo[$key]['pathname'], './' . $conf['upload_water_file'], null, $conf['upload_water_trans']); } } return $fileinfo; } }
protected function _upload() { import("@.ORG.UploadFile"); $upload = new UploadFile(); //设置上传文件大小 $upload->maxSize = 3292200; //设置上传文件类型 $upload->allowExts = explode(',', 'jpg,gif,png,jpeg'); //设置附件上传目录 $upload->savePath = 'Tpl/default/Public/Uploads/'; //设置需要生成缩略图,仅对图像文件有效 $upload->thumb = true; // 设置引用图片类库包路径 $upload->imageClassPath = '@.ORG.Image'; //设置需要生成缩略图的文件后缀 $upload->thumbPrefix = 'm_,s_'; //生产2张缩略图 //设置缩略图最大宽度 $upload->thumbMaxWidth = '400,100'; //设置缩略图最大高度 $upload->thumbMaxHeight = '400,100'; //设置上传文件规则 $upload->saveRule = uniqid; //删除原图 $upload->thumbRemoveOrigin = true; if (!$upload->upload()) { //捕获上传异常 $this->error($upload->getErrorMsg()); } else { //取得成功上传的文件信息 $uploadList = $upload->getUploadFileInfo(); import("@.ORG.Image"); //给m_缩略图添加水印, Image::water('原文件名','水印图片地址') Image::water($uploadList[0]['savepath'] . 'm_' . $uploadList[0]['savename'], '../Public/Images/logo2.png'); $_POST['image'] = $uploadList[0]['savename']; } if ($list !== false) { //$this->success('上传图片成功!'); return $uploadList; } else { return false; //$this->error('上传图片失败!'); } }
function upload() { import("ORG.Net.UploadFile"); $upload = new UploadFile(); //检查客户端上传文件参数设置 if (isset($_POST['_uploadFileSize']) && is_numeric($_POST['_uploadFileSize'])) { //设置上传文件大小 $upload->maxSize = $_POST['_uploadFileSize']; } else { $upload->maxSize = C('FILE_UPLOAD_MAXSIZE'); } /*if (! empty ( $_POST ['_uploadFileType'] )) { //设置上传文件类型 $upload->allowExts = explode ( ',', strtolower ( $_POST ['_uploadFileType'] ) ); } else { $upload->allowExts = explode ( ',', C ( 'FILE_UPLOAD_ALLOWEXTS' ) ); }*/ $upload->allowExts = explode(',', C('FILE_UPLOAD_ALLOWEXTS')); if (!empty($_POST['_uploadSavePath'])) { //设置附件上传目录 $upload->savePath = $_POST['_uploadSavePath']; } else { $upload->savePath = "./Public/Uploads/" . MODULE_NAME . "/"; } //是否自动命名 if (GetSettValue('uploadsaveRule') == 1) { if (isset($_POST['_uploadSaveRule'])) { //设置附件命名规则 $upload->saveRule = $_POST['_uploadSaveRule']; } else { $upload->saveRule = 'uniqid'; } } if (MODULE_NAME == 'Products') { //创建目录 $upload->savePath = $upload->savePath . toDate(time(), 'Ymd') . "/"; if (!file_exists("{$upload->savePath}")) { mk_dir($upload->savePath); } //设置需要生成缩略图,仅对图像文件有效 $upload->thumb = true; //设置需要生成缩略图的文件后缀 //$upload->thumbSuffix = "_thumb"; //设置缩略图最大宽度 $upload->thumbMaxWidth = GetSettValue('ImgThumbW'); //设置缩略图最大高度 $upload->thumbMaxHeight = GetSettValue('ImgThumbH'); } $upload->uploadReplace = true; if (!$upload->upload()) { $error = $upload->getErrorMsg(); $this->ajaxReturn('', $error, 0); } else { $uploadSuccess = true; $uploadList = $upload->getUploadFileInfo(); //是否打上水印 $imgwater = GetSettValue('ImgWater'); $ImageWaterAlpha = GetSettValue('ImageWaterAlpha'); if (empty($ImageWaterAlpha)) { $ImageWaterAlpha = 80; } import("ORG.Util.Image"); foreach ($uploadList as $key => $file) { $savename['savename'] = $upload->savePath . $file['savename']; if ($imgwater == 1) { if (GetSettValue('is_only_proimg_water') == 1 && MODULE_NAME == 'Products') { Image::water($savename['savename'], GetSettValue('ImgWaterPath'), GetSettValue('ImgWaterPos'), '', $ImageWaterAlpha); } elseif (GetSettValue('is_only_proimg_water') == 0) { Image::water($savename['savename'], GetSettValue('ImgWaterPath'), GetSettValue('ImgWaterPos'), '', $ImageWaterAlpha); } } $savename['name'] = $file['name']; } $this->ajaxReturn($savename, '上传成功!', 1); } }
/** * 保存图片 * @param array $key $_FILES 中的键名 为空则保存 $_FILES 中的所有图片 * @param string $dir 保存的目录 为空则保存到临时目录 * @param bool $is_thumb 是否缩略图片 * @param array $whs 缩略图大小信息 为空则取后台设置,并返回 大图键名big 小图键名small 可生成多个缩略图 数组 参数1 为宽度, 参数2为高度, 参数3为处理方式:0(缩放,默认),1(剪裁), 参数4为是否水印 默认为 0(不生成水印) array( 'thumb1'=>array(300,300,0,0), 'thumb2'=>array(100,100,0,0), ... ), * @param bool $is_delete_origin 是否删除原图(当有缩略图时,此设置才生效) * @param bool $is_water 是否水印 * @return array 如果只有一个图片,则返回 array( 'name'=>图片名称, 'url'=>原图web路径, 'path'=>原图物理路径, 有略图时 'thumb'=>array( 'thumb1'=>array('url'=>web路径,'path'=>物理路径), 'thumb2'=>array('url'=>web路径,'path'=>物理路径), ... ) ) 如果有多个图片,则返回(key 为 $_FILES 中的键名) array( 'key'=>array( 'name'=>图片名称, 'url'=>原图web路径, 'path'=>原图物理路径, 有略图时 'thumb'=>array( 'thumb1'=>array('url'=>web路径,'path'=>物理路径), 'thumb2'=>array('url'=>web路径,'path'=>物理路径), ... ) ) .... ) */ public function save($key = '', $dir = 'temp', $is_thumb = false, $whs = array(), $is_delete_origin = false, $is_water = false) { global $_FANWE; include_once fimport('class/image'); $image = new Image(); if (intval($_FANWE['setting']['max_upload']) > 0) { $image->max_size = intval($_FANWE['setting']['max_upload']); } $list = array(); if (empty($key)) { foreach ($_FILES as $fkey => $file) { $list[$fkey] = false; $image->init($file, $dir); if ($image->save()) { $list[$fkey] = array(); $list[$fkey]['url'] = $image->file['target']; $list[$fkey]['path'] = $image->file['local_target']; $list[$fkey]['name'] = $image->file['prefix']; } } } else { $list[$key] = false; $image->init($_FILES[$key], $dir); if ($image->save()) { $list[$key] = array(); $list[$key]['url'] = $image->file['target']; $list[$key]['path'] = $image->file['local_target']; $list[$key]['name'] = $image->file['prefix']; } } $water_image = FANWE_ROOT . $_FANWE['setting']['water_image']; $water_mark = intval($_FANWE['setting']['water_mark']); $alpha = intval($_FANWE['setting']['water_alpha']); $place = intval($_FANWE['setting']['water_position']); if ($is_thumb) { if (empty($whs)) { $big_width = intval($_FANWE['setting']['big_width']); $big_height = intval($_FANWE['setting']['big_height']); $small_width = intval($_FANWE['setting']['small_width']); $small_height = intval($_FANWE['setting']['small_height']); $thumb_type = intval($_FANWE['setting']['auto_gen_image']); $whs = array('big' => array($big_width, $big_height, $thumb_type, $water_mark), 'small' => array($small_width, $small_height, 1, 0)); } } foreach ($list as $lkey => $item) { if ($is_thumb) { foreach ($whs as $tkey => $wh) { $list[$lkey]['thumb'][$tkey]['url'] = false; $list[$lkey]['thumb'][$tkey]['path'] = false; if ($wh[0] > 0 || $wh[1] > 0) { $thumb_bln = false; $thumb_type = isset($wh[2]) ? intval($wh[2]) : 0; if ($thumb = $image->thumb($item['path'], $wh[0], $wh[1], $thumb_type)) { $thumb_bln = true; $list[$lkey]['thumb'][$tkey]['url'] = $thumb['url']; $list[$lkey]['thumb'][$tkey]['path'] = $thumb['path']; if (isset($wh[3]) && intval($wh[3]) > 0) { $image->water($list[$lkey]['thumb'][$tkey]['path'], $water_image, $alpha, $place); } } } } if ($is_delete_origin && $thumb_bln) { @unlink($item['path']); $list[$lkey]['url'] = false; $list[$lkey]['path'] = false; } } if ($is_water) { $image->water($item['path'], $water_image, $alpha, $place); } } if ($key != '') { return $list[$key]; } else { return $list; } }
/** * 图片上传处理 * @param * @return */ public function upload() { import('ORG.Net.UploadFile'); $config = array('autoSub' => true, 'subType' => 'date', 'dateFormat' => 'Ymd', 'savePath' => './uploads/'); $upload = new UploadFile($config); if ($upload->upload()) { $info = $upload->getUploadFileInfo(); //引入自己写的Imaage类 import('Class.Image', APP_PATH); //实例化水印(water)方法 Image::water('./uploads/' . $info[0]['savename']); echo json_encode(array('url' => $info[0]['savename'], 'title' => htmlspecialchars($_POST['pictitle'], ENT_QUOTES), 'original' => $info[0]['name'], 'state' => 'SUCCESS')); } else { echo json_encode(array('state' => $upload->getErrorMsg())); } /** * 向浏览器返回数据json数据 * { * 'url' :'a.jpg', //保存后的文件路径 * 'title' :'hello', //文件描述,对图片来说在前端会添加到title属性上 * 'original' :'b.jpg', //原始文件名 * 'state' :'SUCCESS' //上传状态,成功时返回SUCCESS,其他任何值将原样返回至图片上传框中 * } */ }
case 'uploadimage': /* 上传涂鸦 */ /* 上传涂鸦 */ case 'uploadscrawl': /* 上传视频 */ /* 上传视频 */ case 'uploadvideo': /* 上传文件 */ /* 上传文件 */ case 'uploadfile': $upload = new Upload($CONFIG['imagePathFormat']); if ($info = $upload->upload()) { //加水印 if ($_GET['water'] == 1) { $image = new Image(); $image->water($info[0]['path']); } $info = $info[0]; $result = json_encode(array("state" => "SUCCESS", "url" => $info['url'], "title" => $info['filename'], "original" => $info['name'], "type" => $info['ext'], "size" => $info['size'])); } else { $result = json_encode(array('state' => $upload->error)); } break; /* 列出图片 */ /* 列出图片 */ case 'listimage': $result = (include "action_list.php"); break; /* 列出文件 */ /* 列出文件 */ case 'listfile':
* array( * "state" => "", //上传状态,上传成功时必须返回"SUCCESS" * "url" => "", //返回的地址 * "title" => "", //新文件名 * "original" => "", //原始文件名 * "type" => "" //文件类型 * "size" => "", //文件大小 * ) */ $config = (include $rootpath . '/App/Admin/Conf/Water.php'); if ($config['WATER_FUNTION'] == 1) { $info = $up->getFileInfo(); $filePath = $rootpath . $info['url']; $imgObj = new Image(); $imgObj->open($filePath); $waterpos = $config['WATER_POS']; $watercom = $config['WATER_COMPRESSION']; switch ($config['WATER_FILE_TYPE']) { case 1: $waterimg = $rootpath . $config['WATER_IMAGE']; $imgObj->water($waterimg, $waterpos, $watercom); break; case 2: $waterfont = $rootpath . $config['WATER_FONTFILE']; $imgObj->text($config['WATER_TEXT'], $waterfont, $config['WATER_FONTSIZE'], $config['WATER_FONTCOLOR'], $waterpos, 0, $config['WATER_ANGLE']); break; } $imgObj->save($filePath); } /* 返回数据 */ return json_encode($up->getFileInfo());
/** * * @param $origin_path 原始物理图片地址 * @param array $sizes 缩略图大小信息 为空则取后台设置 可生成多个缩略图 数组 参数1 为宽度, 参数2为高度, 参数3为处理方式:0(缩放,默认),1(剪裁), 参数4为是否水印 默认为 0(不生成水印) array( array(300,300,0,0), array(100,100,0,0), ... ), * @param $path 存储的路径 * @param $file_name 保存的文件名 * @param $del_temp 是否删除临时文件 * @param $id 关联编号,将根据编号生成目录 * * 返回 复制成功的信息,如为false则复制失败 * array( * 'path' => xxx //物理路径 * 'url' => xxx //相对路径 * ); * * by fzmatthew */ function copyImage($origin_path, $sizes = array(), $path = 'share', $del_temp = true, $id = 0) { global $_FANWE; static $size_setting = NULL, $image = NULL; if ($image === NULL) { include_once fimport('class/image'); $image = new Image(); } if ($path == 'temp') { $dir = './public/upload/temp/' . fToDate(NULL, 'Y/m/d/H'); } else { if ($id > 0) { $dir = './public/upload/' . $path . '/' . getDirsById($id); } else { $dir = './public/upload/' . $path . '/' . fToDate(NULL, 'Y/m/d'); } } makeDir(FANWE_ROOT . $dir); $file_name = md5(microtime(true)) . random('6') . '.jpg'; $file_path = FANWE_ROOT . $dir . "/" . $file_name; $bln = false; if (file_exists($origin_path) && @copy($origin_path, $file_path)) { $bln = true; } else { $data = getUrlContent($origin_path); if (!empty($data) && @file_put_contents($file_path, $data) > 0) { $bln = true; } } if ($bln) { $info = $image->getImageInfo($file_path); if ($info['type'] != 'jpg' && $info['type'] != 'jpeg') { if (!$image->convertType($file_path, $file_path)) { return false; } } $water_image = $_FANWE['setting']['water_image']; if (!empty($water_image)) { $water_image = FANWE_ROOT . $water_image; } $water_mark = intval($_FANWE['setting']['water_mark']); $alpha = intval($_FANWE['setting']['water_alpha']); $place = intval($_FANWE['setting']['water_position']); if ($sizes !== false && empty($sizes)) { if ($size_setting === NULL) { if (!isset($_FANWE['cache']['image_sizes'])) { FanweService::instance()->cache->loadCache('image_sizes'); } $size_setting = $_FANWE['cache']['image_sizes']; } $sizes = $size_setting; } foreach ($sizes as $size) { if ($size[0] > 0 || $size[1] > 0) { $thumb_bln = false; $thumb_type = isset($size[2]) ? intval($size[2]) : 0; if ($thumb = $image->thumb($file_path, $size[0], $size[1], $thumb_type)) { if (isset($size[3]) && intval($size[3]) > 0) { $image->water($thumb['path'], $water_image, $alpha, $place); } } } } if ($del_temp) { @unlink($origin_path); } return array('path' => $file_path, 'url' => $dir . "/" . $file_name, 'width' => $info[0], 'height' => $info[1]); } else { return false; } }
private function upthumb() { $upload = new UploadFile(); $upload->maxSize = '20480000'; $upload->savePath = './Public/Uploads/'; $upload->saveRule = time; $upload->uploadReplace = true; $upload->allowExts = array('jpg', 'jpeg', 'png', 'gif'); //准许上传的文件后缀 $upload->allowTypes = array('image/jpeg', 'image/pjpeg', 'image/png', 'image/gif', 'image/x-png'); //准许上传的文件类型 $upload->imageClassPath = 'ORG.Util.Image'; $upload->thumb = true; //是否开启图片文件缩略,true表示开启 $upload->thumbMaxWidth = '500'; //以字串格式来传,如果你希望有多个,那就在此处,用,分格,写上多个最大宽 $upload->thumbMaxHeight = '400'; $upload->thumbPrefix = 'thumb_'; //缩略图文件前缀 $upload->thumbPath = './Public/Uploads/thumb/'; $upload->thumbRemoveOrigin = 1; if ($upload->upload()) { $info = $upload->getUploadFileInfo(); $config = F('basic', '', './Web/Conf/'); if ($config['watermark'] == 1) { Image::water($info[0]['savepath'] . '/thumb/thumb_' . $info[0]['savename'], $info[0]['savepath'] . '/logo/' . $config['watermarkimg']); } return $info; } else { $this->error($upload->getErrorMsg()); } }
public function index() { $files = $_FILES; $ban_ext = array('php', 'asp', 'asp', 'html', 'htm', 'js', 'shtml', 'txt', 'aspx'); if (!empty($files)) { foreach ($files as $file) { $name = $file['name']; $ext = explode('.', $file['name']); $ext = end($ext); if (in_array($ext, $ban_ext)) { $this->error_msg('非法文件上传!'); return; } } } else { $this->error_msg('上传文件不能为空!'); return; } //文件路径 $file_path = __ROOTDIR__ . '/upload/'; //文件URL路径 $file_url = __ROOTURL__ . '/upload/'; //文件目录时间 $filetime = date('Y-m') . '/' . date('d'); //重命名 function filename() { foreach ($_FILES as $file) { $name = explode('.', $file['name']); $ext = end($name); $name = $name[0]; } $pinyin = new Pinyin(); $pattern = '/[^\\x{4e00}-\\x{9fa5}\\d\\w]+/u'; $name = preg_replace($pattern, '', $name); $name = substr($pinyin->output($name, true), 0, 80); if (file_exists(__ROOTDIR__ . '/upload/' . date('Y-m') . '/' . date('d') . '/' . $name . '.' . $ext)) { $rand = '-' . substr(cp_uniqid(), -5); } return $name . $rand; } //上传 $upload = new UploadFile(); $upload->maxSize = 1024 * 1024 * $this->config['ACCESSPRY_SIZE']; //大小 $upload->allowExts = explode(',', $this->config['ACCESSPRY_TYPE']); //格式 $upload->savePath = $file_path . $filetime . '/'; //保存路径 $upload->saveRule = 'filename'; //重命名 if (!$upload->upload()) { $this->error_msg($upload->getErrorMsg()); //输出错误消息 return; } else { $info = $upload->getUploadFileInfo(); $info = $info[0]; //返回信息 Array ( [0] => Array ( [name] => 未命名.jpg [type] => image/pjpeg [size] => 53241 [key] => Filedata [extension] => jpg [savepath] => ../../../upload/2011-12-17/ [savename] => 1112170727041127335395.jpg ) ) $ext = $info['extension']; if ($_POST['wateradd']) { $waterfile = __ROOTDIR__ . '/public/watermark/' . $this->config['WATERMARK_IMAGE']; if (!isset($_POST['waterpor'])) { $por = $this->config['WATERMARK_PLACE']; } else { $por = $_POST['waterpor']; } if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'gif') { Image::water($file_path . $filetime . '/' . $info['savename'], $waterfile, $por); } } if ($_POST['thumb']) { //设置高度和宽度 $thumbwidth = intval($_POST['thumbwidth']); $thumbheight = intval($_POST['thumbheight']); if (empty($thumbwidth) || $_POST['thumbsys'] == 1) { $thumbwidth = $this->config['THUMBNAIL_MAXWIDTH']; } if (empty($thumbheight) || $_POST['thumbsys'] == 1) { $thumbheight = $this->config['THUMBNAIL_MAXHIGHT']; } if (isset($_POST['thumb_cutout'])) { $thumb_cutout = intval($_POST['thumb_cutout']); } else { $thumb_cutout = $this->config['WATERMARK_CUTOUT']; } //过滤不支持格式进行缩图 if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'gif') { $thumb = Image::thumb($file_path . $filetime . '/' . $info['savename'], $file_path . $filetime . '/thumb_' . $info['savename'], '', $thumbwidth, $thumbheight, '', $thumb_cutout); } } //根据缩图返回数据 if ($thumb) { $file = $file_url . $filetime . '/thumb_' . $info['savename']; } else { $file = $file_url . $filetime . '/' . $info['savename']; } $title = str_replace('.' . $info['extension'], '', $info['name']); $json = array('error' => 0, 'url' => $file, 'original' => $file_url . $filetime . '/' . $info['savename'], 'file' => $file, 'title' => $title, 'ext' => $ext, 'msg' => '成功'); /*hook*/ $json = $this->plus_hook_replace('upload', 'index', $json); /*hook end*/ //录入数据库 $data['file'] = $json['file']; $data['title'] = $json['title']; $data['folder'] = date('Y-m-d'); $data['ext'] = $ext; $data['size'] = $info['size']; $data['time'] = time(); $id = model('upload')->add($data); $json['id'] = $id; @header("Content-type:text/html"); echo json_encode($json); return; } }
/** * 上传图片的通公基础方法 * * @param integer $water 0:不加水印 1:打印水印 * @param string $dir 上传的文件夹 * @param bool $is_thumb 是否保存为缩略图 * @return array */ protected function uploadImages($water = 0, $dir = 'images', $is_thumb = false, $whs = array(), $is_swf = false) { $water_mark = FANWE_ROOT . fanweC("WATER_IMAGE"); //配置于config $alpha = fanweC("WATER_ALPHA"); $place = fanweC("WATER_POSITION"); $upload = new UploadFile(); //设置上传文件大小 $max_upload = intval(fanweC('MAX_UPLOAD')); if ($max_upload > 0) { $upload->maxSize = $max_upload * 1024; } /* 配置于config */ //设置上传文件类型 $upload_exts = fanweC('ALLOW_UPLOAD_EXTS'); if (!empty($upload_exts)) { $upload->allowExts = explode(',', fanweC('ALLOW_UPLOAD_EXTS')); } /* 配置于config */ if ($is_swf) { $upload->allowExts[] = 'swf'; } if ($is_thumb) { $upload->thumb = true; if ($width > 0) { $upload->thumbMaxWidth = $width; } else { $upload->thumbMaxWidth = $width; } } if ($is_thumb) { $save_rec_Path = "./Www/Uploads/" . $dir . "/" . toDate(gmtTime(), 'Ym/d') . "/origin/"; } else { $save_rec_Path = "./Www/Uploads/" . $dir . "/" . toDate(gmtTime(), 'Ym/d') . "/"; } //上传至服务器的相对路径 $save_path = ROOT_PATH . '/' . $save_rec_Path; //绝对路径 if (!is_dir($save_path)) { mk_dir($save_path); } $upload->saveRule = "uniqid"; //唯一 $upload->savePath = $save_path; if ($upload->upload()) { $upload_list = $upload->getUploadFileInfo(); foreach ($upload_list as $k => $file_item) { if ($is_thumb) { $file_name = $file_item['savepath'] . $file_item['savename']; //上图原图的地址 //开始缩放处理产品大图 if (isset($whs['big_width'])) { $big_width = $whs['big_width']; } else { $big_width = fanweC("BIG_WIDTH"); } if (isset($whs['big_height'])) { $big_height = $whs['big_height']; } else { $big_height = fanweC("BIG_HEIGHT"); } $big_save_path = str_replace("origin", "big", $save_path); //大图存放图径 if (!is_dir($big_save_path)) { mk_dir($big_save_path); } $big_file_name = str_replace("origin", "big", $file_name); $big_save_path = str_replace("origin", "big", $savePath); //大图存放图径 if (!is_dir($big_save_path)) { mk_dir($big_save_path); } $big_file_name = str_replace("origin", "big", $file_name); if (fanweC("AUTO_GEN_IMAGE") == 1) { Image::thumb($file_name, $big_file_name, '', $big_width, $big_height); } else { @copy($file_name, $big_file_name); } if ($water && file_exists($water_mark)) { Image::water($big_file_name, $water_mark, $big_file_name, $alpha, $place); } //开始缩放处理产品小图 if (isset($whs['small_width'])) { $small_width = $whs['small_width']; } else { $small_width = fanweC("SMALL_WIDTH"); } if (isset($whs['small_height'])) { $small_height = $whs['small_height']; } else { $small_height = fanweC("SMALL_HEIGHT"); } $small_save_path = str_replace("origin", "small", $save_path); //小图存放图径 if (!is_dir($small_save_path)) { mk_dir($small_save_path); } $small_file_name = str_replace("origin", "small", $file_name); Image::thumb($file_name, $small_file_name, '', $small_width, $small_height); $big_save_rec_Path = str_replace("origin", "big", $save_rec_Path); //大图存放的相对路径 $small_save_rec_Path = str_replace("origin", "small", $save_rec_Path); //大图存放的相对路径 $upload_list[$k]['recpath'] = $save_rec_Path; $upload_list[$k]['big_recpath'] = $big_save_rec_Path; $upload_list[$k]['small_recpath'] = $small_save_rec_Path; } else { $upload_list[$k]['recpath'] = $save_rec_Path; $file_name = $file_item['savepath'] . $file_item['savename']; if ($water && file_exists($water_mark)) { Image::water($file_name, $water_mark, $file_name, $alpha, $place); } } } return $upload_list; } else { return false; } }
/** * 上传图片的通公基础方法 * * @return array */ protected function uploadImage() { if (conf("WATER_MARK") != "") { $water_mark = get_real_path() . conf("WATER_MARK"); } else { $water_mark = ""; } $alpha = conf("WATER_ALPHA"); //水印透明 $place = conf("WATER_POSITION"); //水印位置 $upload = new UploadFile(); //设置上传文件大小 $upload->maxSize = conf('MAX_IMAGE_SIZE'); /* 配置于config */ //设置上传文件类型 $upload->allowExts = explode(',', conf('ALLOW_IMAGE_EXT')); /* 配置于config */ $dir_name = to_date(NOW_TIME, "Ym"); if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name)) { @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name); @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name, 0777); } $dir_name = $dir_name . "/" . to_date(NOW_TIME, "d"); if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name)) { @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name); @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name, 0777); } $dir_name = $dir_name . "/" . to_date(NOW_TIME, "H"); if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name)) { @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name); @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name, 0777); } $save_rec_Path = "/public/attachment/" . $dir_name . "/origin/"; //上传时先存放原图 $savePath = APP_ROOT_PATH . "public/attachment/" . $dir_name . "/origin/"; //绝对路径 if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name . "/origin/")) { @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name . "/origin/"); @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name . "/origin/", 0777); } $domain_path = SITE_DOMAIN . APP_ROOT . $save_rec_Path; $upload->saveRule = "uniqid"; //唯一 $upload->savePath = $savePath; if ($upload->upload()) { $uploadList = $upload->getUploadFileInfo(); foreach ($uploadList as $k => $fileItem) { $file_name = $fileItem['savepath'] . $fileItem['savename']; //上图原图的地址 //水印图 $big_save_path = str_replace("origin/", "", $savePath); //大图存放图径 $big_file_name = str_replace("origin/", "", $file_name); // Image::thumb($file_name,$big_file_name,'',$big_width,$big_height); @file_put_contents($big_file_name, @file_get_contents($file_name)); if (file_exists($water_mark)) { Image::water($big_file_name, $water_mark, $big_file_name, $alpha, $place); } $big_save_rec_Path = str_replace("origin/", "", $save_rec_Path); //上传的图存放的相对路径 $uploadList[$k]['recpath'] = $save_rec_Path; $uploadList[$k]['bigrecpath'] = $big_save_rec_Path; if ($GLOBALS['distribution_cfg']['OSS_TYPE'] && $GLOBALS['distribution_cfg']['OSS_TYPE'] != 'NONE') { syn_to_remote_image_server("./public/attachment/" . $dir_name . "/origin/" . $fileItem['savename']); syn_to_remote_image_server("./public/attachment/" . $dir_name . "/" . $fileItem['savename']); } } return array("status" => 1, 'data' => $uploadList, 'info' => L("UPLOAD_SUCCESS")); } else { return array("status" => 0, 'data' => null, 'info' => $upload->getErrorMsg()); } }
private function upattach() { $upload = new UploadFile(); $upload->maxSize = '2048000'; $upload->savePath = './Public/Uploads/attach/'; $upload->saveRule = time; $upload->uploadReplace = true; $upload->allowExts = array('zip', 'rar', 'txt', 'ppt', 'pptx', 'cls', 'clsx', 'doc', 'docx', 'swf', 'jpg', 'png', 'gif', 'tar.gz', '.7z'); //准许上传的文件后缀 if ($upload->upload()) { $info = $upload->getUploadFileInfo(); if ($info[0]['extension'] == 'jpg' || $info[0]['extension'] == 'png' || $info[0]['extension'] == 'gif') { $config = F('basic', '', './Web/Conf/'); if ($config['watermark'] == 1) { import('@.ORG.Image'); Image::water($info[0]['savepath'] . $info[0]['savename'], './Public/Uploads/logo/' . $config['watermarkimg']); } } return $info; } else { $this->error($upload->getErrorMsg()); } }
/** * 多文件上传 * * @param boolean $thumb [description] * @return [type] [description] */ public static function uploads($thumb = true, $params = array('thumb' => false, 'thumbSize' => array(400, 400), 'allowExts' => 'jpg,gif,png,jpeg', 'maxSize' => 3292200)) { Yii::import('application.vendors.*'); require_once 'Tp/UploadFile.class.php'; // 设置上传文件大小 $upload->maxSize = isset($params['maxSize']) ? $params['maxSize'] : Config::get('maxSize'); // 设置上传文件类型 $upload->allowExts = isset($params['allowExts']) ? explode(',', $params['allowExts']) : explode(',', Config::get('upload_allow_ext')); // 设置附件上传目录 $upload->savePath = self::_saveRule($params); // 设置需要生成缩略图,仅对图像文件有效 $upload->thumb = isset($params['thumb']) ? $params['thumb'] : Config::get('thumb'); // 设置需要生成缩略图的文件后缀 $upload->thumbPrefix = 'thumb_'; // 生产2张缩略图 // 设置缩略图最大宽度 $upload->thumbMaxWidth = $params['thumbSize'][0]; // 设置缩略图最大高度 $upload->thumbMaxHeight = $params['thumbSize'][1]; // 设置上传文件规则 $upload->saveRule = uniqid; // 删除原图 $upload->thumbRemoveOrigin = false; if (!$upload->upload()) { return $upload->getErrorMsg(); } else { $fileinfo = $upload->getUploadFileInfo(); require_once 'Tp/Image.class.php'; Image::water($fileget['pathname'], './' . Config::get('upload_water_file'), null, Config::get('upload_water_trans')); foreach ($fileinfo as $key => $row) { if (true == $upload->thumb) { $fileinfo[$key]['thumb'] = $upload->thumbPrefix . $fileinfo[$key]['savename']; } $fileinfo[$key]['pathname'] = '/' . $upload->savePath . $fileinfo[$key]['savename']; $fileinfo[$key]['paththumbname'] = '/' . $upload->savePath . $upload->thumbPrefix . $fileinfo[$key]['savename']; self::uploadToQiniu($fileinfo[$key]['savepath'] . $fileinfo[$key]['savename'], $upload->savePath . $fileinfo[$key]['savename']); if (Config::get('upload_water_status') == 'open') { Image::water($fileinfo[$key]['pathname'], './' . Config::get('upload_water_file'), null, Config::get('upload_water_trans')); } } return $fileinfo; } }
/** * 上传图片的通公基础方法 * * @return array */ protected function uploadImage() { if (conf("WATER_MARK") != "") { $water_mark = get_real_path() . conf("WATER_MARK"); } else { $water_mark = ""; } $alpha = conf("WATER_ALPHA"); //水印透明 $place = conf("WATER_POSITION"); //水印位置 $upload = new UploadFile(); //设置上传文件大小 $upload->maxSize = conf('MAX_IMAGE_SIZE'); /* 配置于config */ //设置上传文件类型 $upload->allowExts = explode(',', conf('ALLOW_IMAGE_EXT')); /* 配置于config */ $dir_name = to_date(get_gmtime(), "Ym"); if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name)) { @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name); @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name, 0777); } $dir_name = $dir_name . "/" . to_date(get_gmtime(), "d"); if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name)) { @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name); @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name, 0777); } $dir_name = $dir_name . "/" . to_date(get_gmtime(), "H"); if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name)) { @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name); @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name, 0777); } $save_rec_Path = "/public/attachment/" . $dir_name . "/origin/"; //上传时先存放原图 $savePath = APP_ROOT_PATH . "public/attachment/" . $dir_name . "/origin/"; //绝对路径 if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name . "/origin/")) { @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name . "/origin/"); @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name . "/origin/", 0777); } $domain_path = get_domain() . APP_ROOT . $save_rec_Path; $upload->saveRule = "uniqid"; //唯一 $upload->savePath = $savePath; if ($upload->upload()) { $uploadList = $upload->getUploadFileInfo(); foreach ($uploadList as $k => $fileItem) { $file_name = $fileItem['savepath'] . $fileItem['savename']; //上图原图的地址 //水印图 $big_save_path = str_replace("origin/", "", $savePath); //大图存放图径 $big_file_name = str_replace("origin/", "", $file_name); // Image::thumb($file_name,$big_file_name,'',$big_width,$big_height); @file_put_contents($big_file_name, @file_get_contents($file_name)); if (file_exists($water_mark)) { Image::water($big_file_name, $water_mark, $big_file_name, $alpha, $place); } $big_save_rec_Path = str_replace("origin/", "", $save_rec_Path); //上传的图存放的相对路径 $uploadList[$k]['recpath'] = $save_rec_Path; $uploadList[$k]['bigrecpath'] = $big_save_rec_Path; // if(app_conf("PUBLIC_DOMAIN_ROOT")!='') // { // $origin_syn_url = app_conf("PUBLIC_DOMAIN_ROOT")."/es_file.php?username="******"IMAGE_USERNAME")."&password="******"IMAGE_PASSWORD")."&file=".get_domain().APP_ROOT."/public/attachment/".$dir_name."/origin/".$fileItem['savename']."&path=attachment/".$dir_name."/origin/&name=".$fileItem['savename']."&act=0"; // $big_syn_url = app_conf("PUBLIC_DOMAIN_ROOT")."/es_file.php?username="******"IMAGE_USERNAME")."&password="******"IMAGE_PASSWORD")."&file=".get_domain().APP_ROOT."/public/attachment/".$dir_name."/".$fileItem['savename']."&path=attachment/".$dir_name."/&name=".$fileItem['savename']."&act=0"; // @file_get_contents($origin_syn_url); // @file_get_contents($big_syn_url); // } } return array("status" => 1, 'data' => $uploadList, 'info' => L("UPLOAD_SUCCESS")); } else { return array("status" => 0, 'data' => null, 'info' => $upload->getErrorMsg()); } }
/** * 上传一个文件 * @access public * @param mixed $name 数据 * @param string $value 数据表名 * @return string */ private function save($file) { $filename = $file['savepath'] . $file['savename']; //[cluster] 去掉不覆盖判断 // 如果是图像文件 检测文件格式 if (in_array(strtolower($file['extension']), array('gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'))) { $info = getimagesize($file['tmp_name']); if (false === $info || 'gif' == strtolower($file['extension']) && empty($info['bits'])) { $this->error = '非法图像文件'; return false; } } $uploadfile = $file['tmp_name']; //[cluster] 生成水印图片 if (!empty($this->water)) { import($this->imageClassPath); $tmp_water = tempnam(sys_get_temp_dir(), 'tp_'); Image::water($file['tmp_name'], $this->water, $tmp_water, $this->water_alpha, false); if ($this->water_savename) { file_upload($tmp_water, $this->water_savename); } else { $uploadfile = $tmp_water; } } //[cluster] 上传文件 if (!$this->thumbRemoveOrigin && !file_upload($uploadfile, $this->autoCharset($filename, 'utf-8', 'gbk'))) { $this->error = '文件上传保存错误!'; return false; } //[cluster] 删除水印临时文件 if (isset($tmp_water) && file_exists($tmp_water)) { unlink($tmp_water); } if ($this->thumb && in_array(strtolower($file['extension']), array('gif', 'jpg', 'jpeg', 'bmp', 'png'))) { $image = getimagesize($file['tmp_name']); if (false !== $image) { //是图像文件生成缩略图 $thumbWidth = explode(',', $this->thumbMaxWidth); $thumbHeight = explode(',', $this->thumbMaxHeight); $thumbPrefix = explode(',', $this->thumbPrefix); $thumbSuffix = explode(',', $this->thumbSuffix); $thumbFile = explode(',', $this->thumbFile); $thumbPath = $this->thumbPath ? $this->thumbPath : dirname($filename) . '/'; $thumbExt = $this->thumbExt ? $this->thumbExt : $file['extension']; //自定义缩略图扩展名 // 生成图像缩略图 import($this->imageClassPath); for ($i = 0, $len = count($thumbWidth); $i < $len; $i++) { if (!empty($thumbFile[$i])) { $thumbname = $thumbFile[$i]; } else { $prefix = isset($thumbPrefix[$i]) ? $thumbPrefix[$i] : $thumbPrefix[0]; $suffix = isset($thumbSuffix[$i]) ? $thumbSuffix[$i] : $thumbSuffix[0]; $thumbname = $prefix . basename($filename, '.' . $file['extension']) . $suffix; } //[cluster] 缩略图用临时文件实现 $tmp_file = sys_get_temp_dir() . '/' . $thumbname . '.' . $thumbExt; $ret = Image::thumb($file['tmp_name'], $tmp_file, '', $thumbWidth[$i], $thumbHeight[$i], true); if (false !== $ret) { file_upload($tmp_file, $thumbPath . $thumbname . '.' . $thumbExt); } if (file_exists($tmp_file)) { unlink($tmp_file); } } } } if ($this->zipImags) { // TODO 对图片压缩包在线解压 } return true; }
/** * 用于图片附件上传加水印回调方法 * @param type $_this * @param type $fileInfo * @param type $params */ public static function water($_this, $fileInfo, $params) { //网站拍照 $config = cache('Config'); //是否开启水印 if (empty($config['watermarkenable'])) { return false; } //水印文件 $water = SITE_PATH . $config['watermarkimg']; //水印位置 $waterPos = (int) $config['watermarkpos']; //水印透明度 $alpha = (int) $config['watermarkpct']; //jpg图片质量 $quality = (int) $config['watermarkquality']; foreach ($fileInfo as $file) { //原图文件 $source = $file['savepath'] . $file['savename']; //图像信息 $sInfo = \Image::getImageInfo($source); //如果图片小于系统设置,不进行水印添加 if ($sInfo["width"] < (int) $config['watermarkminwidth'] || $sInfo['height'] < (int) $config['watermarkminheight']) { continue; } \Image::water($source, $water, $source, $alpha, $waterPos, $quality); } }
/** * 图片加水印 * @param type $source 原图文件名。 * @param type $water 水印图片文件名 * @param type $savename 要保存的图片名,如果留空则用source * @param type $alpha 水印图片的alpha值,默认为80,范围为0~100 * @param type $waterPos 水印位置。 * @param type $quality jpg图片质量 */ public function water($source, $water = null, $savename = null, $alpha = null, $waterPos = null, $quality = null) { //设置默认水印 if ($water == '') { $water = SITE_PATH . $this->config['watermarkimg']; } //图像信息 $sInfo = \Image::getImageInfo($source); //如果图片小于系统设置,不进行水印添加 if ($sInfo["width"] < (int) $this->config['watermarkminwidth'] || $sInfo['height'] < (int) $this->config['watermarkminheight']) { return false; } //水印位置 if (empty($waterPos)) { $waterPos = (int) $this->config['watermarkpos']; } //水印透明度 if (empty($alpha)) { $alpha = (int) $this->config['watermarkpct']; } //jpg图片质量 if (empty($quality)) { $quality = (int) $this->config['watermarkquality']; } return \Image::water($source, $water, $savename, $alpha, $waterPos, $quality); }
/** * 上传文件 */ public function upload() { $upload = new Upload('Upload/Content/' . date('y/m')); $file = $upload->upload(); if (empty($file)) { $this->ajax('上传失败'); } else { $data = $file[0]; if ($data['image'] && C('WATER_ON')) { $imageObj = new Image(); $imageObj->water($data['path'], $data['path']); } $data['uid'] = $_SESSION['user']['uid']; $data['mid'] = $this->mid; M('upload')->add($data); $this->ajax($data); } }
/** * 上传一个文件 * * @access public * @param mixed $name 数据 * @param string $value 数据表名 * @return string */ private function save($file) { //file_put_contents('log.txt',var_export($file,true)); $filename = $file['savepath'] . $file['savename']; if (!$this->uploadReplace && is_file($filename)) { // 不覆盖同名文件 $this->error = '文件已经存在!' . $filename; return false; } $source_file_name = $this->autoCharset($filename, 'utf-8', 'gbk'); if (!move_uploaded_file($file['tmp_name'], $source_file_name)) { $this->error = '文件上传保存错误!'; return false; } // 如果是图像文件 检测文件格式,兼容某些任何图片都无法识别上传问题 if (in_array(strtolower($file['extension']), array('gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'))) { $info = getimagesize($source_file_name); if (false === $info || 'gif' == strtolower($file['extension']) && empty($info['bits'])) { $this->error = '非法图像文件1'; @unlink($source_file_name); return false; } } //判断文件后缀 if (stripos($source_file_name, '.php') !== false) { $this->error = '非法图像文件2'; @unlink($source_file_name); return false; } //判断文件内容 $tmp_con = @file_get_contents($source_file_name); if (strstr($tmp_con, 'php')) { $this->error = '非法图像文件2'; @unlink($source_file_name); return false; } if ($this->thumb && in_array(strtolower($file['extension']), array('gif', 'jpg', 'jpeg', 'bmp', 'png'))) { $image = getimagesize($filename); if (false !== $image) { // 是图像文件生成缩略图 $thumbWidth = explode(',', $this->thumbMaxWidth); $thumbHeight = explode(',', $this->thumbMaxHeight); $thumbPrefix = explode(',', $this->thumbPrefix); $thumbSuffix = explode(',', $this->thumbSuffix); $thumbFile = explode(',', $this->thumbFile); $thumbPath = $this->thumbPath ? $this->thumbPath : dirname($filename) . '/'; $thumbExt = $this->thumbExt ? $this->thumbExt : $file['extension']; //自定义缩略图扩展名 // 生成图像缩略图 for ($i = 0, $len = count($thumbWidth); $i < $len; $i++) { if (!empty($thumbFile[$i])) { $thumbname = $thumbFile[$i]; } else { $prefix = isset($thumbPrefix[$i]) ? $thumbPrefix[$i] : $thumbPrefix[0]; $suffix = isset($thumbSuffix[$i]) ? $thumbSuffix[$i] : $thumbSuffix[0]; $thumbname = $prefix . basename($filename, '.' . $file['extension']) . $suffix; } Image::thumb($filename, $thumbPath . $thumbname . '.' . $thumbExt, '', $thumbWidth[$i], $thumbHeight[$i], true); } if ($this->thumbRemoveOrigin) { // 生成缩略图之后删除原图 unlink($filename); } } } //开始打水印 if ($this->config['water']) { //将图片路径保存到water中 $this->config['water']['source'] = $this->autoCharset($filename, 'utf-8', 'gbk'); //file_put_contents('log.txt',var_export($this->config['water'],true)); Image::water($this->config['water']); //将图片缩略图路径保存到water中 $this->config['water']['source'] = $thumbPath . $thumbname . '.' . $thumbExt; Image::water($this->config['water']); } if ($this->zipImags) { // TODO 对图片压缩包在线解压 } return true; }
/** * 用于图片附件上传加水印回调方法 * @param type $_this * @param type $fileInfo * @param type $params */ public static function water($_this, $fileInfo, $params) { import("Image"); //水印文件 $water = SITE_PATH . CONFIG_WATERMARKIMG; //水印位置 $waterPos = (int) CONFIG_WATERMARKPOS; //水印透明度 $alpha = (int) CONFIG_WATERMARKPCT; //jpg图片质量 $quality = (int) CONFIG_WATERMARKQUALITY; foreach ($fileInfo as $file) { //原图文件 $source = $file['savepath'] . $file['savename']; //图像信息 $sInfo = Image::getImageInfo($source); //如果图片小于系统设置,不进行水印添加 if ($sInfo["width"] < (int) CONFIG_WATERMARKMINWIDTH || $sInfo['height'] < (int) CONFIG_WATERMARKMINHEIGHT) { continue; } Image::water($source, $water, $source, $alpha, $waterPos, $quality); } }