//引入
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上传图片生成缩略图的更多相关文章

  1. asp.net——上传图片生成缩略图

    上传图片生成缩略图,原图和缩略图地址一样的时候缩略图会把原图覆盖掉 /// <summary> /// 生成缩略图 /// </summary> /// <param n ...

  2. 使用Uploadify实现上传图片生成缩略图例子,实时显示进度条

    不了解Uploadify的,先看看前一篇详细说明 http://www.cnblogs.com/XuebinDing/archive/2012/04/26/2470995.html Uploadify ...

  3. 上传图片+生成缩略图 ashx代码

    html页面 <form action="Handlers/UploadImageHandler.ashx" method="post" enctype= ...

  4. C#上传图片同时生成缩略图,控制图片上传大小。

    #region 上传图片生成缩略图 /// <summary> /// 上传图片 /// </summary> /// <param name="sender& ...

  5. js无刷新上传图片,服务端有生成缩略图,剪切图片,iphone图片旋转判断功能

    html: <form action="<{:AppLink('circle/uploadimg')}>" id="imageform" me ...

  6. C#上传图片和生成缩略图以及图片预览

    因工作需要,上传图片要增加MIME类型验证和生成较小尺寸的图片用于浏览.根据网上代码加以修改做出如下效果图: 前台代码如下: <html xmlns="http://www.w3.or ...

  7. PHP.24-TP框架商城应用实例-后台1-添加商品功能、钩子函数、在线编辑器、过滤XSS、上传图片并生成缩略图

    添加商品功能 1.创建商品控制器[C] /www.test.com/shop/Admin/Controller/GoodsController.class.php <?php namespace ...

  8. MVC4 上传图片并生成缩略图

    Views @using (Html.BeginForm("Create","img",FormMethod.Post, new { enctype = &qu ...

  9. thinkphp上传图片,生成缩略图

    Image.php <?php /** * 实现图片上传,图片缩小, 增加水印 * 需要定义以下常量 * define('ERR_INVALID_IMAGE', 1); * define('ER ...

随机推荐

  1. 关于 DELPHI DATASNAP 的文章集

    关于 DELPHI  DATASNAP 的文章集: 1.墨者工作室  DataSnap基础 https://wenku.baidu.com/view/78715605cc1755270722088b. ...

  2. python性能分析之line_profiler模块

    line_profiler使用装饰器(@profile)标记需要调试的函数.用kernprof.py脚本运行代码,被选函数每一行花费的cpu时间以及其他信息就会被记录下来. 安装 pip3 insta ...

  3. VC中的字符串转换宏

              ATL7.0引入了几个新的转换类和宏,现有的宏提供了显着的改善.新的字符串转换类和宏的名称,采取的形式如下: C SourceType [C]DestinationType[EX] ...

  4. /etc/shadow中密码段的生成方式

    why? 为什么要手动生成? 通常情况下,使用passwd命令即可以为用户设置密码,但是在某些情况下是需要先获得加密后的密码然后去操作的,比如,kickstart文件中的rootpw指令,ansibl ...

  5. PostgreSQL安装和创建用户和创建数据库

    一.安装 可以参考postgresql官网安装教程:https://www.postgresql.org/download/linux/redhat/ Centos 6 安装postgresql 10 ...

  6. T-SQL删除存储过程

    使用T-SQL脚本删除存储过程 语法: --声明数据库引用use 数据库名称;go --判断是否存在存储过程,如果存在则删除if exists(select * from sys.procedures ...

  7. Goland could not launch process: decoding dwarf section info at offset 0x0: too short 解决方案

    1 前言 Goland版本:2018.1.5 Go:1.11.2 此版本调试不了bug,而且有时会显示could not launch process: decoding dwarf section ...

  8. elasticsearch中的java.io.IOException: 远程主机强迫关闭了一个现有的连接

    [2018-07-31T14:29:41,289][WARN ][o.e.x.s.t.n.SecurityNetty4HttpServerTransport] [9rTGh-y] caught exc ...

  9. java 中int与integer的区别

    int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型 ...

  10. ADO.NET连接字符串大全---各种数据库的连接字符串

    ADO.NET连接字符串大全 ADO.NET连接字符串 名称 ADO.NET连接字符串 说明 ADO.NET连接字符串:SQL Server,SQL Server 2005,ACCESS,Oracle ...