thinkphp3.2.3实现后台自定义图片上传格式、大小;添加水印的条件(宽度大于多少高度大于多少才会添加水印)、水印类型(文字和图片)、水印文字的颜色和大小,水印图片上传,水印的位置以及透明度的配置 演示地址请访问http://www.gouguoyin.cn/php/32.html
thinkphp3.2.3实现后台自定义图片上传格式、大小;添加水印的条件(宽度大于多少高度大于多少才会添加水印)、水印类型(文字和图片)、水印文字的颜色和大小,水印图片上传,水印的位置以及透明度的配置
演示地址请访问http://www.gouguoyin.cn/php/32.html
thinkphp 下面开始教程一、数据库首页要在数据库创建一个网站配置表,各个字段说明如下create table if not exists `go_config` ( `id` int(10) not null auto_increment, `field` varchar(255) not null comment '属性字段', `title` varchar(255) not null comment '字段标题', `mark` varchar(255) not null comment '备注信息,主要用于placeholder显示', `field_type` varchar(255) not null default 'string' comment '字段类型,string:字符串 longstring:长字符串 phone:手机号 email:邮箱 url:链接地址 bool:布尔 text:多行文本 editor:编辑器 pic:图片 file:文件,默认string', `config_type` varchar(255) not null default 'site' comment '配置类型', `value` varchar(255) not null comment '字段值', `is_system` tinyint(1) not null default '0' comment '是否是系统字段,0:否 1:是 ,系统字段无法删除', `is_required` tinyint(1) not null default '0' comment '是否必填,1:必填 0:非必填,默认0', `sort` int(10) not null default '1' comment '排序', `status` tinyint(1) not null default '1' comment '状态,1:正常,0:回收站,默认1', `add_time` datetime not null comment '添加时间', `update_time` timestamp not null default current_timestamp on update current_timestamp comment '更新时间', primary key (`id`), unique key `field` (`field`)) engine=innodb default charset=utf8 comment='系统配置表' auto_increment=1 ;二、控制器在项目的application/admin/controller/目录下建立configcontroller.class.php控制器,代码如下display('config_upload'); } //保存上传配置 public function upload_save() { $data = i('post.'); foreach ($data as $k => $v) { $result = m('config')->where(array('field' => $k))->save(array('value' => $v)); } $this->success('上传设置保存成功', u('admin/config/upload')); }}三、视图模板在项目的application/admin/view/config目录下建立config_upload.html的模板,代码如下{include file=public/header title=上传设置/}{include file=public/sidebar/} 编辑上传配置信息
允许上传图片格式:(请用|分割格式,例如jpg|png|gif)
允许上传多媒体格式:(请用|分割格式,例如swf|flv|mpg)
允许上传文件格式:(请用|分割格式,例如zip|rar|doc|xls)
允许上传文件大小限制:(单位:m)
生成缩略图条件:(单位:px) 上传图片宽度大于: 上传图片高度大于:
缩略图默认大小:(单位:px) ×
添加水印条件:(单位:px) 上传图片宽度大于: 上传图片高度大于:
水印类型: {$upload_watermark_type = getconfigvalue('upload_watermark_type')} 文字 图片
水印文字:
水印字体颜色:
水印字体大小:(单位:px)
上传水印图片:(点击图片更换水印)
水印图片默认大小:(单位:px) ×
水印位置: {$upload_watermark_position = getconfigvalue('upload_watermark_position')} 顶部居左 顶部居中 顶部居右 中间居左 居中 中间居右 底部居左 底部居中 底部居右
水印透明度:(单位:%,数值越小越透明)
{include file=public/footer/}四、使用方法在项目的application/common/helper目录(没有helper目录请自行创建)下建立upload.class.php助手函数库,代码如下 0); //获取上传类型image、flash、media、file //从数据库获取上传配置信息 $max_size = getconfigvalue('upload_file_size')*1024*1024; //允许上传的图片后缀 $upload_pic_suffix = getconfigvalue('upload_pic_suffix'); $pic_ext_arr = explode('|', $upload_pic_suffix); //允许上传的多媒体后缀 $upload_media_suffix = getconfigvalue('upload_media_suffix'); $media_ext_arr = explode('|', $upload_media_suffix); //允许上传的文件后缀 $upload_file_suffix = getconfigvalue('upload_file_suffix'); $file_ext_arr = explode('|', $upload_file_suffix); //生成缩略图默认大小 $upload_thumb_width = getconfigvalue('upload_thumb_width'); $upload_thumb_height = getconfigvalue('upload_thumb_height'); //水印类型 $upload_watermark_type = getconfigvalue('upload_watermark_type'); //文字水印文本 $upload_watermark_text = getconfigvalue('upload_watermark_text'); //文字水印颜色 $upload_watermark_color = getconfigvalue('upload_watermark_color'); //文字水印大小 $upload_watermark_size = getconfigvalue('upload_watermark_size'); $upload_watermark_pic_width = getconfigvalue('upload_watermark_pic_width'); $upload_watermark_pic_height = getconfigvalue('upload_watermark_pic_height'); //图片水印路径 $upload_watermark = '.'.getconfigvalue('upload_watermark'); if(!$upload_watermark){ $upload_watermark = './public/images/water.png'; } //水印宽度 $upload_watermark_width = getconfigvalue('upload_watermark_width'); //水印高度 $upload_watermark_height = getconfigvalue('upload_watermark_height'); //水印位置 $upload_watermark_position = getconfigvalue('upload_watermark_position'); //水印透明度 $upload_watermark_opacity = getconfigvalue('upload_watermark_opacity'); //上传配置 $ext_arr = array( 'image' => $pic_ext_arr, 'media' => $media_ext_arr, 'file' => $file_ext_arr ); $upload = new \think\upload(); // 实例化上传类 $upload->maxsize = $max_size; // 设置附件上传大小 $upload->exts = $ext_arr[$type]; // 设置附件上传类型 $upload->savepath = './'.$type. '/'; $file_info = $upload->upload(); if ($file_info) { foreach($file_info as $info){ $file_path = './uploads'.$info['savepath']. $info['savename']; } //引入图片处理类 $image = new \think\image(); $image->open($file_path); $width = $image->width(); $height = $image->height(); //生成缩略图 //$image->crop($upload_thumb_width, $upload_thumb_height, 1)->save('./crop.jpg'); //添加水印 if($is_water && $width >= $upload_watermark_pic_width && $height>= $upload_watermark_pic_height){ if($upload_watermark_type == 2){ //添加图片水印 $image->water($upload_watermark, $upload_watermark_position, $upload_watermark_opacity)->save($file_path); }else{ //添加文字水印 $image->text($upload_watermark_text, './public/font/vista.ttf', $upload_watermark_size, $upload_watermark_color, $upload_watermark_position)->save($file_path); } } $base_url = './uploads/'; $return['url'] = substr($base_url, 1).$info['savepath']. $info['savename']; } else { $return['error'] = 1; $return['message'] = $upload->geterror(); } exit(json_encode($return)); }}在控制器里使用方法:引入助手函数库use common\helper\upload;调用上传函数//$is_thumb 是否启用缩略图 ,$is_water是否启用水印$pic_data = upload::index($type='image', $is_thumb=0, $is_water=0);$pic_data数组里返回有上传图片的路径url,如果上传失败,则会返回错误代码和错误信息。有了上传图片的路径就可以对上传图片进行入库等操作了。注:为方便大家使用,特将核心代码打包上传,大家请点击“下载源码”按钮下载打包文件。
源码下载地址http://www.gouguoyin.cn/php/32.html