tp3.2上传图片生成缩略图
//引入
use think\Image; /*
* $name为表单上传的name值
* $filePath为为保存在入口文件夹public下面uploads/下面的文件夹名称,没有的话会自动创建
* $width指定缩略宽度
* $height指定缩略高度
* 自动生成的缩略图保存在$filePath文件夹下面的thumb文件夹里,自动创建
* @return array 一个是图片路径,一个是缩略图路径,如下:
* array(2) {
["filepath"] => string(57) "uploads/img/20171211\3d4ca4098a8fb0f90e5f53fd7cd71535.jpg"
["thumbimgpath"] => string(63) "uploads/img/thumb/20171211/3d4ca4098a8fb0f90e5f53fd7cd71535.jpg"
}
*/
protected function uploadFile($name,$filePath,$width = 0,$height = 0)
{
$file = request()->file($name);
if($file){
$filePaths = ROOT_PATH . 'public' . DS . 'uploads' . DS . $filePath;
if (!file_exists($filePaths)) {
mkdir($filePaths, 0777, true);
}
$info = $file->move($filePaths);
if ($info) {
$imgpath = $filePaths . '/' . $info->getSaveName();
$url_path = '/uploads/'. $filePath . '/' . $info->getSaveName();
$image = \think\Image::open($imgpath);
$date_path = $filePaths . '/thumb/' . date('Ymd');
$thumb_url = '/uploads/'. $filePath .'/thumb/' . date('Ymd');
if (!file_exists($date_path)) {
mkdir($date_path, 0777, true);
}
$thumb_path = $date_path . '/' . $info->getFilename();
$thumb_url_path = $thumb_url . '/' . $info->getFilename();
$image->thumb($width, $height)->save($thumb_path);
$uploadinfo = ['code' => 0, 'data' => ['filepath' => $url_path,'thumbimgpath'=>$thumb_url_path],'msg'=>''];
return json_encode($uploadinfo);
}else{
// 上传失败获取错误信息
return json_encode(['code'=>-1,'data'=>'','msg'=>$file->getError()]);
}
}
}
//调用
www.xxx.com/api/goods/uploadFile
$res = uploadFile('image','goods',200,200);
var_dump($res);
tp3.2上传图片生成缩略图的更多相关文章
- asp.net——上传图片生成缩略图
上传图片生成缩略图,原图和缩略图地址一样的时候缩略图会把原图覆盖掉 /// <summary> /// 生成缩略图 /// </summary> /// <param n ...
- 使用Uploadify实现上传图片生成缩略图例子,实时显示进度条
不了解Uploadify的,先看看前一篇详细说明 http://www.cnblogs.com/XuebinDing/archive/2012/04/26/2470995.html Uploadify ...
- 上传图片+生成缩略图 ashx代码
html页面 <form action="Handlers/UploadImageHandler.ashx" method="post" enctype= ...
- C#上传图片同时生成缩略图,控制图片上传大小。
#region 上传图片生成缩略图 /// <summary> /// 上传图片 /// </summary> /// <param name="sender& ...
- js无刷新上传图片,服务端有生成缩略图,剪切图片,iphone图片旋转判断功能
html: <form action="<{:AppLink('circle/uploadimg')}>" id="imageform" me ...
- C#上传图片和生成缩略图以及图片预览
因工作需要,上传图片要增加MIME类型验证和生成较小尺寸的图片用于浏览.根据网上代码加以修改做出如下效果图: 前台代码如下: <html xmlns="http://www.w3.or ...
- PHP.24-TP框架商城应用实例-后台1-添加商品功能、钩子函数、在线编辑器、过滤XSS、上传图片并生成缩略图
添加商品功能 1.创建商品控制器[C] /www.test.com/shop/Admin/Controller/GoodsController.class.php <?php namespace ...
- MVC4 上传图片并生成缩略图
Views @using (Html.BeginForm("Create","img",FormMethod.Post, new { enctype = &qu ...
- thinkphp上传图片,生成缩略图
Image.php <?php /** * 实现图片上传,图片缩小, 增加水印 * 需要定义以下常量 * define('ERR_INVALID_IMAGE', 1); * define('ER ...
随机推荐
- nginx 配置域名转发
自己测试环境,配置下载目录和一个jenkins的地址: 域名跳转,反向代理 # cat ../nginx.conf user www www; worker_processes ; error_log ...
- C#编写COM组件
1.新建一个类库项目 2.将Class1.cs改为我们想要的名字 问是否同时给类改名,确定 3.修改Properties目录下面的AssemblyInfo.cs ComVisible属性设置为True ...
- MFCWinInet学习
http://blog.csdn.net/segen_jaa/article/details/6278167 背景: 功能:服务端下载文件 服务端:用Java写Sevlet进行有效性验证 客户端:用C ...
- Food HDU - 4292 网络流 拆点建图
http://acm.hdu.edu.cn/showproblem.php?pid=4292 给一些人想要的食物和饮料,和你拥有的数量,问最多多少人可以同时获得一份食物和一份饮料 写的时候一共用了2种 ...
- $Django 多表操作(增删改查,基于双下划线,对象的查询) 在Python脚本中调用Django环境
在Python脚本中调用Django环境. import osif __name__ == '__main__': os.environ.setdefault("DJANGO_SETTING ...
- centos7 docker使用https_proxy 代理配置
centos7 docker使用https_proxy 代理配置 背景: 内网的centos主机不能上网,通过同网段的windows设置代理上网,yum.conf配置http代理是可以的,但是dock ...
- [PHP]curl上传多文件
码一下curl上传多文件的行 5.5之前版本的写法 $file = array( 'pic[0]'=>"@E:\\wwwroot\\10003\\temp_56.ini;type=te ...
- LuoGu P1004 方格取数
题目传送门 一开始这个题我是不会的(沙华弱DP啊QwQ),后来考完试我一想,这东西怎么和数字三角形那题这么像啊? 都是自上而下,只能向下或者向右,求一个max 那么...这不就是个走两遍的数字矩阵嘛 ...
- 细说Django的中间件
分析Django的生命周期,我们知道所有的http请求都要经过Django的中间件. 假如现在有一个需求,所有到达服务端的url请求都在系统中记录一条日志,该怎么做呢? Django的中间件的简介 D ...
- Confluence 6 数据收集隐私策略
为什么 Confluence 收集使用数据? 针对 Confluence 我们很自豪 Confluence 是这个星球上最高效和强大的协作工具,我们也计划继续保持这个特性,尽我们最大的努力提供更新的 ...