Commit 90a9b17e327d02223cddc2841530a33fe657d207

Authored by u123231312
1 parent 22ed4ac5

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

... ... @@ -2,6 +2,7 @@
namespace app\admin\controller;
require '../vendor/Qiniu/autoload.php';
use think\Db;
use think\Compression;
use think\Request;
use think\Controller;
use think\facade\Env;
... ... @@ -39,12 +40,6 @@ class UpFiles extends Common
// 获取表单上传文件
$file = request()->file($fileKey['0']);
$obj = $file->getInfo();
if($obj['size']> 122880){
$result['code'] =0;
$result['info'] = "图片超出125KB";
$result['url'] = '';
return $result;
}
$rs = array('code' => 0, 'url' => '');
$tmpName = $obj['tmp_name'];
$name = md5($obj['name'] . $_SERVER['REQUEST_TIME']);
... ... @@ -57,6 +52,16 @@ class UpFiles extends Common
$imgPath = $uploadFolder . $name . $ext;
//移动到临时目录
if (move_uploaded_file($tmpName, $imgPath)) {
if($obj['size']> 122880){
$image = new Compression($imgPath);
$imgPaths = $image->getThumb();
}
/*if(filesize($imgPath)> 122880){
$result['code'] =0;
$result['info'] = "图片超出125KB";
$result['url'] = '';
return $result;
}*/
$accessKey = 'uva-aaonM8dukeR6phc9yuPBrOXk9brrNW_PxLD7';
$secretKey = 'oZMMVN6phk49lG0J1xwmi1Ju0ETT15EnAEWcU9Cs';
$auth = new Auth($accessKey, $secretKey);
... ... @@ -67,7 +72,7 @@ class UpFiles extends Common
// 构建 UploadManager 对象
$uploadMgr = new UploadManager();
$backUrl = "pe9jzv1q9.bkt.clouddn.com"; //返回图片在七牛的路径
list($ret, $err) = $uploadMgr->putFile($token,$key,$imgPath);
list($ret, $err) = $uploadMgr->putFile($token,$key,$imgPaths);
// 获取视频的时长
// 第一步先获取到到的是关于视频所有信息的json字符串
//$shichang = file_get_contents('http://'.WAILIAN.$key.'?avinfo');
... ... @@ -94,6 +99,7 @@ class UpFiles extends Common
if($rs['url']){
//上传成功之后删除服务器文件
unlink($imgPath);
unlink($imgPaths);
$result['code'] = 1;
$result['info'] = '图片上传成功!';
$path=str_replace('\\','/',$rs['url']);
... ...
... ... @@ -15,6 +15,7 @@
</a>
</blockquote>
<form class="layui-form layui-form-pane">
<!--<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;">上传文件夹-->
<div class="layui-form-item" id="container">
<label class="layui-form-label">菜品名称</label>
<div class="layui-input-1">
... ...
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think;
/**
图片压缩操作类
v1.0
*/
class Compression
{
private $src;
public $percent = 0.1;
public function __construct($src){
$this->src = $src;
}
/**
* 图片压缩处理
* @param string $sFile 图片路径
* @param int $iWidth 自定义图片宽度
* @param int $iHeight 自定义图片高度
*/
function getThumb(){
$iWidth = 375;
$iHeight = 240;
$sFile = $this->src;
//判断该图片是否存在
if(!file_exists($sFile)) return $sFile;
//判断图片格式
$attach_fileext = $this->get_filetype($sFile);
if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
return $sFile;
}
//压缩图片
$sFileNameS = str_replace(".".$attach_fileext, "_".$iWidth.'_'.$iHeight.'.'.$attach_fileext, $sFile);
//判断是否已压缩图片,若是则返回压缩图片路径
if(file_exists($sFileNameS)){
return $sFileNameS;
}
//解决手机端上传图片被旋转问题
/*if (in_array($attach_fileext, array('jpeg')) ){
$this->adjustPicOrientation($sFile);
}*/
//生成压缩图片,并存储到原图同路径下
$this->resizeImage($sFile, $sFileNameS, $iWidth, $iHeight);
if(!file_exists($sFileNameS)){
return $sFile;
}
return $sFileNameS;
}
/**
*获取文件后缀名
*/
function get_filetype($filename) {
$extend = explode("." , $filename);
return strtolower($extend[count($extend) - 1]);
}
/**
* 生成图片
* @param string $im 源图片路径
* @param string $dest 目标图片路径
* @param int $maxwidth 生成图片宽
* @param int $maxheight 生成图片高
*/
function resizeImage($im, $dest, $maxwidth, $maxheight) {
$img = getimagesize($im);
switch ($img[2]) {
case 1:
$im = @imagecreatefromgif($im);
break;
case 2:
$im = @imagecreatefromjpeg($im);
break;
case 3:
$im = @imagecreatefrompng($im);
break;
}
$pic_width = imagesx($im);
$pic_height = imagesy($im);
$resizewidth_tag = false;
$resizeheight_tag = false;
if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
if ($maxwidth && $pic_width > $maxwidth) {
$widthratio = $maxwidth / $pic_width;
$resizewidth_tag = true;
}
if ($maxheight && $pic_height > $maxheight) {
$heightratio = $maxheight / $pic_height;
$resizeheight_tag = true;
}
if ($resizewidth_tag && $resizeheight_tag) {
if ($widthratio < $heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}
if ($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;
if ($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;
if (function_exists("imagecopyresampled")) {
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
} else {
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
}
imagejpeg($newim, $dest);
imagedestroy($newim);
} else {
imagejpeg($im, $dest);
}
}
/**
* 解决手机上传图片被旋转问题
* @param string $full_filename 文件路径
*/
function adjustPicOrientation($full_filename){
$exif = exif_read_data($full_filename);
if($exif && isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
if($orientation != 1){
$img = imagecreatefromjpeg($full_filename);
$mirror = false;
$deg = 0;
switch ($orientation) {
case 2:
$mirror = true;
break;
case 3:
$deg = 180;
break;
case 4:
$deg = 180;
$mirror = true;
break;
case 5:
$deg = 270;
$mirror = true;
break;
case 6:
$deg = 270;
break;
case 7:
$deg = 90;
$mirror = true;
break;
case 8:
$deg = 90;
break;
}
if ($deg) $img = imagerotate($img, $deg, 0);
if ($mirror) $img = _mirrorImage($img);
//$full_filename = str_replace('.jpg', "-O$orientation.jpg", $full_filename);新文件名
imagejpeg($img, $full_filename, 95);
}
}
return $full_filename;
}
}
\ No newline at end of file
... ...