fixed image
|
|
@ -7,12 +7,18 @@ use Illuminate\Http\Request;
|
|||
|
||||
class FileManagerController extends Controller
|
||||
{
|
||||
/**
|
||||
* 获取文件夹和文件列表
|
||||
* @param Request $request
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$baseFolder = $request->get('base_folder');
|
||||
$page = (int)$request->get('page');
|
||||
$data = (new FileManagerService)->getFiles($baseFolder, $page);
|
||||
|
||||
dd($data);
|
||||
if ($request->expectsJson()) {
|
||||
return $data;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ class FileManagerService
|
|||
* @param $baseFolder
|
||||
* @param int $page
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getFiles($baseFolder, int $page = 1): array
|
||||
{
|
||||
|
|
@ -91,6 +92,7 @@ class FileManagerService
|
|||
* @param $filePath
|
||||
* @param $baseName
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function handleImage($filePath, $baseName): array
|
||||
{
|
||||
|
|
|
|||
|
|
@ -172,13 +172,14 @@ function currency_format($price): string
|
|||
* @param int $width
|
||||
* @param int $height
|
||||
* @return mixed|void
|
||||
* @throws Exception
|
||||
*/
|
||||
function image_resize($image, int $width = 100, int $height = 100)
|
||||
{
|
||||
if (Str::startsWith($image, 'http')) {
|
||||
return $image;
|
||||
}
|
||||
return asset($image);
|
||||
return (new \Beike\Services\ImageService($image))->resize($width, $height);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* ImageService.php
|
||||
*
|
||||
|
|
@ -9,23 +8,67 @@
|
|||
* @created 2022-07-12 17:30:20
|
||||
* @modified 2022-07-12 17:30:20
|
||||
*/
|
||||
|
||||
namespace Beike\Services;
|
||||
|
||||
use Intervention\Image\Facades\Image;
|
||||
|
||||
class ImageService
|
||||
{
|
||||
private $image;
|
||||
private $imagePath;
|
||||
|
||||
public function resize($image, $width = 100, $height = 100)
|
||||
/**
|
||||
* @param string $image
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct(string $image)
|
||||
{
|
||||
$this->image = $image;
|
||||
$imagePath = public_path($image);
|
||||
$img = \Intervention\Image\Facades\Image::make($imagePath);
|
||||
$img->resize($width, $height);
|
||||
// $img->insert('public/watermark.png');
|
||||
if (!file_exists($imagePath)) {
|
||||
throw new \Exception("图片不存在");
|
||||
}
|
||||
$this->imagePath = $imagePath;
|
||||
}
|
||||
|
||||
$baseName = basename($imagePath);
|
||||
$imageInfo = explode('.', $image);
|
||||
$imageName = $imageInfo[0];
|
||||
$imageExt = $imageInfo[1];
|
||||
$newImagePath = public_path("cache/{$imageName}-{$width}x{$height}.{$imageExt}");
|
||||
\Illuminate\Support\Facades\File::ensureDirectoryExists($newImagePath, 0755, true);
|
||||
$img->save($newImagePath);
|
||||
dd($newImagePath);
|
||||
|
||||
/**
|
||||
* 生成并获取缩略图
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
* @return string
|
||||
*/
|
||||
public function resize(int $width = 100, int $height = 100): string
|
||||
{
|
||||
$extension = pathinfo($this->imagePath, PATHINFO_EXTENSION);
|
||||
$newImage = 'cache/' . mb_substr($this->image, 0, mb_strrpos($this->image, '.')) . '-' . (int)$width . 'x' . (int)$height . '.' . $extension;
|
||||
|
||||
$newImagePath = public_path($newImage);
|
||||
if (!is_file($newImagePath) || (filemtime($this->imagePath) > filemtime($newImagePath))) {
|
||||
$this->createDirectories($newImage);
|
||||
$img = Image::make($this->imagePath);
|
||||
$img->resize($width, $height);
|
||||
$img->save($newImagePath);
|
||||
}
|
||||
return asset($newImage);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 递归创建缓存文件夹
|
||||
*
|
||||
* @param $imagePath
|
||||
*/
|
||||
private function createDirectories($imagePath)
|
||||
{
|
||||
$path = '';
|
||||
$directories = explode('/', dirname($imagePath));
|
||||
foreach ($directories as $directory) {
|
||||
$path = $path . '/' . $directory;
|
||||
if (!is_dir(public_path($path))) {
|
||||
@mkdir(public_path($path), 0755);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
After Width: | Height: | Size: 6.9 KiB |
|
After Width: | Height: | Size: 6.5 KiB |
|
After Width: | Height: | Size: 8.6 KiB |
|
After Width: | Height: | Size: 5.1 KiB |
|
After Width: | Height: | Size: 6.4 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 336 B |