首先下载一下扩展:
找到根目录文件夹输入cmd命令:(composer 安装)
composer require topthink/think-image
版本一
存在问题:会生成三张图片占用内存,下一个版本已经解决
html代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="{:url('index/test/testa')}" method="post" enctype="multipart/form-data">
<input type="file" name="image" required="required"></div>
<input type="submit" value="提交">
</form>
</body>
</html>
控制代码:
<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Log;
use think\Db;
class Test extends Controller
{
public function index(){
return $this->fetch( 'index' );
}
public function testa(){
$file = $this->request->file('image');
$imagesize = getimagesize($file->getInfo()['tmp_name']);
$upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads');
$save_path = '/uploads/';
$info = $file->move($upload_path); //保存原图
// $info = $file->validate($config)->move($upload_path); //保存原图
if ($info) {
$url = str_replace('\\', '/', $save_path . $info->getSaveName());
// dump($url);exit;
$image = \think\Image::open(str_replace('\/', '/',ROOT_PATH.'public'.$url));
$date_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads').'/'.date('Ymd');
if(!file_exists($date_path)){
mkdir($date_path,0777,true);
}
$thumb_path = $date_path.'/'.$info->getFilename();
$path_parts = pathinfo($thumb_path);
$path = $path_parts['dirname'].'/'.$path_parts['filename'].'_thumb.'.$path_parts['extension']; //缩略图文件名
$file_path = $result = substr($path,strripos($path,"public")+6);
// $water_path = str_replace('\/', '/',ROOT_PATH).'public/static/images/water.png';
//->thumb()等比例缩放
//->water()加水印
// $image->thumb(1200, 750)->water($water_path,5)->water($water_path)->save($path); //大缩略图(加两个不同位置的水印)
$image->thumb(1200, 750)->save($path); //不加水印
$path = $path_parts['dirname'].'/'.$path_parts['filename'].'_thumb_m.'.$path_parts['extension']; //缩略图文件名
$file_path = $result = substr($path,strripos($path,"public")+6);
$image->thumb(715, 500)->save($path); //小缩略图
$result = [
'error' => 0,
'url' => $file_path,
'size'=>$imagesize[0].'*'.$imagesize[1],
];
} else {
$result = [
'error' => 1,
'message' => $file->getError()
];
}
return json($result);
}
}
?>
版本二
解决三张图片问题
html代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="{:url('index/test/testa')}" method="post" enctype="multipart/form-data">
<input type="file" name="image" required="required"></div>
<input type="submit" value="提交">
</form>
</body>
</html>
控制器代码:
<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Log;
use think\Db;
class Test extends Controller
{
public function index(){
return $this->fetch( 'index' );
}
public function testa(){
$file = $this->request->file('image');
$imagesize = getimagesize($file->getInfo()['tmp_name']);
$upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads');
$save_path = '/uploads/';
$info = $file->move($upload_path); //保存原图
// $info = $file->validate($config)->move($upload_path); //保存原图
if ($info) {
$url = str_replace('\\', '/', $save_path . $info->getSaveName());
// dump($url);exit;
$image = \think\Image::open(str_replace('\/', '/',ROOT_PATH.'public'.$url));
$date_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads').'/'.date('Ymd');
if(!file_exists($date_path)){
mkdir($date_path,0777,true);
}
$thumb_path = $date_path.'/'.$info->getFilename();
$path_parts = pathinfo($thumb_path);
$path = $path_parts['dirname'].'/'.$path_parts['filename'].'_thumb.'.$path_parts['extension']; //缩略图文件名
$file_path = $result = substr($path,strripos($path,"public")+6);
$image->thumb(715, 500)->save($path); //小缩略图
$result = [
'error' => 0,
'url' => $file_path,
'size'=>$imagesize[0].'*'.$imagesize[1],
];
} else {
$result = [
'error' => 1,
'message' => $file->getError()
];
}
unlink( ROOT_PATH . 'public' . DS . $url);//删除原本图片
return json($result);
}
}
?>