intervention/image 服务器图片裁剪
1.拉取组件
composer require intervention/image
2.门面和服务提供者 config/app.php
// 服务提供者
'providers' => [
...
...
Intervention\Image\ImageServiceProvider::class,
],
// 门面
'aliases' => [
...
...
'Image' => Intervention\Image\Facades\Image::class,
],
上传类实例(兄弟会大牛郭鹏超)
<?php
namespace App\Services;
use Intervention\Image\Facades\Image;
/**
* 图片处理业务层
*
* Class UploadService
* @package App\Services
* @author 郭鹏超
*/
class UploadService
{
protected static $qiNiuService = null;
/**
* 单例引入
*
* UploadService constructor.
* @param QiNiuService $qiNiuService
* @author 郭鹏超
*/
public function __construct(QiNiuService $qiNiuService)
{
self::$qiNiuService = $qiNiuService;
}
/**
* 图片上传接口
*
* @param $file
* @return array
* @author 郭鹏超
*/
public function uploadFile($file)
{
// 检查文件是否上传
$check = $this->checkFile($file);
// 验证
if ($check['ServerNo'] != 200) return $check;
// 文件存储路径
$path = $this->path();
// 重命名
$fileName = $this->reName($file->getClientOriginalExtension());
// 移动
if (!$file->move($path, $fileName)){
return ['ServerNo' => 500, 'ResultData' => '文件保存失败'];
}
return ['ServerNo' => 200, 'ResultData' => $fileName];
}
/**
* 判断上传文件是否可以上传
*
* @param $file
* @return array
* @author 郭鹏超
*/
private function checkFile($file)
{
// 检查是否文件上传成功
if (!$file->isValid()) {
return ['ServerNo' => 404, 'ResultData' => '文件上传失败'];
}
// 检查是否超过文件大小
if ($file->getClientSize() > $file->getMaxFilesize()) {
return ['ServerNo' => 404, 'ResultData' => '上传文件过大'];
}
return ['ServerNo' => 200, 'ResultData' => '文件上传成功'];
}
/**
* 文件重命名
*
* @param $endname
* @return string
* @author 郭鹏超
*/
private function reName($endname)
{
return md5($_SERVER['REQUEST_TIME'] . mt_rand(0,9999999)) . '.' . $endname;
}
/**文件存储位置
*
* @param $type
* @return string
* @author 郭鹏超
*/
private function path()
{
return public_path('uploads');
}
/**
* 处理头像上传
*
* @param $request
* @return array
* @author 郭鹏超
*/
public function uploadPhoto($request)
{
// 文件名
$name = 'avatar_file';
// 检查坐标和文件是否存在
$data = $request->only('avatar_data');
$file = $request->file($name);
if (empty($data) || empty($file)) return ['ServerNo' => 404, 'ResultData' => '获取数据失 败'];
// 上传图片操作,成功返回图片上传本地的路径
$result = $this->uploadFile($file, $name);
if ($result['ServerNo'] != 200) return $result;
// 图片路径
$path = config('config.common.upload_path') . DIRECTORY_SEPARATOR;
$file = $result['ResultData']; // 旧图片路径
$newFile = config('config.common.photo_prefix') . $file; // 新图片路径
// 判断图片是否存在
if (!file_exists($path . $file)) return ['ServerNo' => 404, 'ResultData' => '图片不存在'];
// 图片裁剪
$cutRes = $this->cutPhoto($data, $path . $file, $path . $newFile);
if ($cutRes['ServerNo'] != 200) return $cutRes;
// 上传裁剪图片到7牛
$qiNiu = self::$qiNiuService->qiNiuUpload($newFile);
// 检查文件是否上传成功
if ($qiNiu['ServerNo'] != 200) return $qiNiu;
return ['ServerNo' => 200, 'ResultData' => $qiNiu['ResultData']];
}
/**
* 进行图片裁剪操作
*
* @param $data
* @param $oldPath
* @param $newPath
* @return array
* @author 郭鹏超
*/
public function cutPhoto($data, $oldPath, $newPath)
{
// 获取用户对文件进行处理的数据
$avatarInfo = json_decode($data['avatar_data']);
$cropX = floor($avatarInfo->x);
$cropY = floor($avatarInfo->y);
$cropW = floor($avatarInfo->width);
$cropH = floor($avatarInfo->height);
$rotate = $avatarInfo->rotate;
// 使用图片处理类进行裁剪操作
$img = Image::make($oldPath)
->rotate(-$rotate)
->crop($cropW, $cropH, $cropX, $cropY)
->save($newPath);
// 生成裁剪图片失败
if(!$img) return ['ServerNo' => 500, 'ResultData' => '图片保存失败'];
// 删除旧图片
@unlink($oldPath);
return ['ServerNo' => 200, 'ResultData' => $newPath];
}
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: