Commit 90a9b17e327d02223cddc2841530a33fe657d207

Authored by u123231312
1 parent 22ed4ac5

菜品库上传文件时 自动压缩图片大小。

@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 namespace app\admin\controller; 2 namespace app\admin\controller;
3 require '../vendor/Qiniu/autoload.php'; 3 require '../vendor/Qiniu/autoload.php';
4 use think\Db; 4 use think\Db;
  5 +use think\Compression;
5 use think\Request; 6 use think\Request;
6 use think\Controller; 7 use think\Controller;
7 use think\facade\Env; 8 use think\facade\Env;
@@ -39,12 +40,6 @@ class UpFiles extends Common @@ -39,12 +40,6 @@ class UpFiles extends Common
39 // 获取表单上传文件 40 // 获取表单上传文件
40 $file = request()->file($fileKey['0']); 41 $file = request()->file($fileKey['0']);
41 $obj = $file->getInfo(); 42 $obj = $file->getInfo();
42 - if($obj['size']> 122880){  
43 - $result['code'] =0;  
44 - $result['info'] = "图片超出125KB";  
45 - $result['url'] = '';  
46 - return $result;  
47 - }  
48 $rs = array('code' => 0, 'url' => ''); 43 $rs = array('code' => 0, 'url' => '');
49 $tmpName = $obj['tmp_name']; 44 $tmpName = $obj['tmp_name'];
50 $name = md5($obj['name'] . $_SERVER['REQUEST_TIME']); 45 $name = md5($obj['name'] . $_SERVER['REQUEST_TIME']);
@@ -57,6 +52,16 @@ class UpFiles extends Common @@ -57,6 +52,16 @@ class UpFiles extends Common
57 $imgPath = $uploadFolder . $name . $ext; 52 $imgPath = $uploadFolder . $name . $ext;
58 //移动到临时目录 53 //移动到临时目录
59 if (move_uploaded_file($tmpName, $imgPath)) { 54 if (move_uploaded_file($tmpName, $imgPath)) {
  55 + if($obj['size']> 122880){
  56 + $image = new Compression($imgPath);
  57 + $imgPaths = $image->getThumb();
  58 + }
  59 + /*if(filesize($imgPath)> 122880){
  60 + $result['code'] =0;
  61 + $result['info'] = "图片超出125KB";
  62 + $result['url'] = '';
  63 + return $result;
  64 + }*/
60 $accessKey = 'uva-aaonM8dukeR6phc9yuPBrOXk9brrNW_PxLD7'; 65 $accessKey = 'uva-aaonM8dukeR6phc9yuPBrOXk9brrNW_PxLD7';
61 $secretKey = 'oZMMVN6phk49lG0J1xwmi1Ju0ETT15EnAEWcU9Cs'; 66 $secretKey = 'oZMMVN6phk49lG0J1xwmi1Ju0ETT15EnAEWcU9Cs';
62 $auth = new Auth($accessKey, $secretKey); 67 $auth = new Auth($accessKey, $secretKey);
@@ -67,7 +72,7 @@ class UpFiles extends Common @@ -67,7 +72,7 @@ class UpFiles extends Common
67 // 构建 UploadManager 对象 72 // 构建 UploadManager 对象
68 $uploadMgr = new UploadManager(); 73 $uploadMgr = new UploadManager();
69 $backUrl = "pe9jzv1q9.bkt.clouddn.com"; //返回图片在七牛的路径 74 $backUrl = "pe9jzv1q9.bkt.clouddn.com"; //返回图片在七牛的路径
70 - list($ret, $err) = $uploadMgr->putFile($token,$key,$imgPath); 75 + list($ret, $err) = $uploadMgr->putFile($token,$key,$imgPaths);
71 // 获取视频的时长 76 // 获取视频的时长
72 // 第一步先获取到到的是关于视频所有信息的json字符串 77 // 第一步先获取到到的是关于视频所有信息的json字符串
73 //$shichang = file_get_contents('http://'.WAILIAN.$key.'?avinfo'); 78 //$shichang = file_get_contents('http://'.WAILIAN.$key.'?avinfo');
@@ -94,6 +99,7 @@ class UpFiles extends Common @@ -94,6 +99,7 @@ class UpFiles extends Common
94 if($rs['url']){ 99 if($rs['url']){
95 //上传成功之后删除服务器文件 100 //上传成功之后删除服务器文件
96 unlink($imgPath); 101 unlink($imgPath);
  102 + unlink($imgPaths);
97 $result['code'] = 1; 103 $result['code'] = 1;
98 $result['info'] = '图片上传成功!'; 104 $result['info'] = '图片上传成功!';
99 $path=str_replace('\\','/',$rs['url']); 105 $path=str_replace('\\','/',$rs['url']);
@@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
15 </a> 15 </a>
16 </blockquote> 16 </blockquote>
17 <form class="layui-form layui-form-pane"> 17 <form class="layui-form layui-form-pane">
  18 + <!--<input title="点击选择文件夹" id="h5Input2" multiple="" webkitdirectory="" accept="*/*" type="file" name="html5uploader" style="position:absolute;opacity:0;top:0;left:0;width:100%;height:100%;cursor:pointer;">上传文件夹-->
18 <div class="layui-form-item" id="container"> 19 <div class="layui-form-item" id="container">
19 <label class="layui-form-label">菜品名称</label> 20 <label class="layui-form-label">菜品名称</label>
20 <div class="layui-input-1"> 21 <div class="layui-input-1">
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8 +// +----------------------------------------------------------------------
  9 +// | Author: liu21st <liu21st@gmail.com>
  10 +// +----------------------------------------------------------------------
  11 +
  12 +namespace think;
  13 +/**
  14 + 图片压缩操作类
  15 + v1.0
  16 +*/
  17 +class Compression
  18 +{
  19 + private $src;
  20 + public $percent = 0.1;
  21 + public function __construct($src){
  22 +
  23 + $this->src = $src;
  24 +
  25 + }
  26 + /**
  27 + * 图片压缩处理
  28 + * @param string $sFile 图片路径
  29 + * @param int $iWidth 自定义图片宽度
  30 + * @param int $iHeight 自定义图片高度
  31 + */
  32 + function getThumb(){
  33 + $iWidth = 375;
  34 + $iHeight = 240;
  35 + $sFile = $this->src;
  36 + //判断该图片是否存在
  37 + if(!file_exists($sFile)) return $sFile;
  38 + //判断图片格式
  39 + $attach_fileext = $this->get_filetype($sFile);
  40 + if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
  41 + return $sFile;
  42 + }
  43 + //压缩图片
  44 + $sFileNameS = str_replace(".".$attach_fileext, "_".$iWidth.'_'.$iHeight.'.'.$attach_fileext, $sFile);
  45 + //判断是否已压缩图片,若是则返回压缩图片路径
  46 + if(file_exists($sFileNameS)){
  47 + return $sFileNameS;
  48 + }
  49 + //解决手机端上传图片被旋转问题
  50 + /*if (in_array($attach_fileext, array('jpeg')) ){
  51 + $this->adjustPicOrientation($sFile);
  52 + }*/
  53 + //生成压缩图片,并存储到原图同路径下
  54 + $this->resizeImage($sFile, $sFileNameS, $iWidth, $iHeight);
  55 + if(!file_exists($sFileNameS)){
  56 + return $sFile;
  57 + }
  58 + return $sFileNameS;
  59 + }
  60 + /**
  61 + *获取文件后缀名
  62 + */
  63 + function get_filetype($filename) {
  64 + $extend = explode("." , $filename);
  65 + return strtolower($extend[count($extend) - 1]);
  66 + }
  67 + /**
  68 + * 生成图片
  69 + * @param string $im 源图片路径
  70 + * @param string $dest 目标图片路径
  71 + * @param int $maxwidth 生成图片宽
  72 + * @param int $maxheight 生成图片高
  73 + */
  74 + function resizeImage($im, $dest, $maxwidth, $maxheight) {
  75 + $img = getimagesize($im);
  76 + switch ($img[2]) {
  77 + case 1:
  78 + $im = @imagecreatefromgif($im);
  79 + break;
  80 + case 2:
  81 + $im = @imagecreatefromjpeg($im);
  82 + break;
  83 + case 3:
  84 + $im = @imagecreatefrompng($im);
  85 + break;
  86 + }
  87 +
  88 + $pic_width = imagesx($im);
  89 + $pic_height = imagesy($im);
  90 + $resizewidth_tag = false;
  91 + $resizeheight_tag = false;
  92 + if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
  93 + if ($maxwidth && $pic_width > $maxwidth) {
  94 + $widthratio = $maxwidth / $pic_width;
  95 + $resizewidth_tag = true;
  96 + }
  97 +
  98 + if ($maxheight && $pic_height > $maxheight) {
  99 + $heightratio = $maxheight / $pic_height;
  100 + $resizeheight_tag = true;
  101 + }
  102 +
  103 + if ($resizewidth_tag && $resizeheight_tag) {
  104 + if ($widthratio < $heightratio)
  105 + $ratio = $widthratio;
  106 + else
  107 + $ratio = $heightratio;
  108 + }
  109 +
  110 +
  111 + if ($resizewidth_tag && !$resizeheight_tag)
  112 + $ratio = $widthratio;
  113 + if ($resizeheight_tag && !$resizewidth_tag)
  114 + $ratio = $heightratio;
  115 + $newwidth = $pic_width * $ratio;
  116 + $newheight = $pic_height * $ratio;
  117 +
  118 + if (function_exists("imagecopyresampled")) {
  119 + $newim = imagecreatetruecolor($newwidth, $newheight);
  120 + imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  121 + } else {
  122 + $newim = imagecreate($newwidth, $newheight);
  123 + imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  124 + }
  125 +
  126 + imagejpeg($newim, $dest);
  127 + imagedestroy($newim);
  128 + } else {
  129 + imagejpeg($im, $dest);
  130 + }
  131 + }
  132 + /**
  133 + * 解决手机上传图片被旋转问题
  134 + * @param string $full_filename 文件路径
  135 + */
  136 + function adjustPicOrientation($full_filename){
  137 + $exif = exif_read_data($full_filename);
  138 + if($exif && isset($exif['Orientation'])) {
  139 + $orientation = $exif['Orientation'];
  140 + if($orientation != 1){
  141 + $img = imagecreatefromjpeg($full_filename);
  142 +
  143 + $mirror = false;
  144 + $deg = 0;
  145 +
  146 + switch ($orientation) {
  147 + case 2:
  148 + $mirror = true;
  149 + break;
  150 + case 3:
  151 + $deg = 180;
  152 + break;
  153 + case 4:
  154 + $deg = 180;
  155 + $mirror = true;
  156 + break;
  157 + case 5:
  158 + $deg = 270;
  159 + $mirror = true;
  160 + break;
  161 + case 6:
  162 + $deg = 270;
  163 + break;
  164 + case 7:
  165 + $deg = 90;
  166 + $mirror = true;
  167 + break;
  168 + case 8:
  169 + $deg = 90;
  170 + break;
  171 + }
  172 + if ($deg) $img = imagerotate($img, $deg, 0);
  173 + if ($mirror) $img = _mirrorImage($img);
  174 + //$full_filename = str_replace('.jpg', "-O$orientation.jpg", $full_filename);新文件名
  175 + imagejpeg($img, $full_filename, 95);
  176 + }
  177 + }
  178 + return $full_filename;
  179 + }
  180 +}