diff --git a/beike/Admin/Http/Controllers/FilemanagerController.php b/beike/Admin/Http/Controllers/FilemanagerController.php index e2c4545b..b70a30e7 100644 --- a/beike/Admin/Http/Controllers/FilemanagerController.php +++ b/beike/Admin/Http/Controllers/FilemanagerController.php @@ -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; } diff --git a/beike/Admin/Services/FileManagerService.php b/beike/Admin/Services/FileManagerService.php index 2ead0db4..1eb1b810 100644 --- a/beike/Admin/Services/FileManagerService.php +++ b/beike/Admin/Services/FileManagerService.php @@ -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 { diff --git a/beike/Helpers.php b/beike/Helpers.php index 8ac2d2bf..664fe99d 100644 --- a/beike/Helpers.php +++ b/beike/Helpers.php @@ -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); } /** diff --git a/beike/Services/ImageService.php b/beike/Services/ImageService.php index 697fb9b1..88ae720c 100644 --- a/beike/Services/ImageService.php +++ b/beike/Services/ImageService.php @@ -1,5 +1,4 @@ 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); + } + } } } diff --git a/public/cache/catalog/demo/banner-1-100x100.png b/public/cache/catalog/demo/banner-1-100x100.png new file mode 100644 index 00000000..5025c5cb Binary files /dev/null and b/public/cache/catalog/demo/banner-1-100x100.png differ diff --git a/public/cache/catalog/demo/banner-100x100.png b/public/cache/catalog/demo/banner-100x100.png new file mode 100644 index 00000000..6c47faa0 Binary files /dev/null and b/public/cache/catalog/demo/banner-100x100.png differ diff --git a/public/cache/catalog/demo/banner-2-100x100.png b/public/cache/catalog/demo/banner-2-100x100.png new file mode 100644 index 00000000..4d43b54b Binary files /dev/null and b/public/cache/catalog/demo/banner-2-100x100.png differ diff --git a/public/cache/catalog/demo/footer-icon-1-100x100.png b/public/cache/catalog/demo/footer-icon-1-100x100.png new file mode 100644 index 00000000..2796a7e4 Binary files /dev/null and b/public/cache/catalog/demo/footer-icon-1-100x100.png differ diff --git a/public/cache/catalog/demo/footer-payment-100x100.png b/public/cache/catalog/demo/footer-payment-100x100.png new file mode 100644 index 00000000..ea0b6101 Binary files /dev/null and b/public/cache/catalog/demo/footer-payment-100x100.png differ diff --git a/public/cache/catalog/demo/image_plus_1-100x100.png b/public/cache/catalog/demo/image_plus_1-100x100.png new file mode 100644 index 00000000..8bcb065c Binary files /dev/null and b/public/cache/catalog/demo/image_plus_1-100x100.png differ diff --git a/public/cache/catalog/demo/image_plus_2-100x100.png b/public/cache/catalog/demo/image_plus_2-100x100.png new file mode 100644 index 00000000..92ea0f71 Binary files /dev/null and b/public/cache/catalog/demo/image_plus_2-100x100.png differ diff --git a/public/cache/catalog/demo/image_plus_3-100x100.png b/public/cache/catalog/demo/image_plus_3-100x100.png new file mode 100644 index 00000000..00f72d7f Binary files /dev/null and b/public/cache/catalog/demo/image_plus_3-100x100.png differ diff --git a/public/cache/catalog/demo/image_plus_4-100x100.png b/public/cache/catalog/demo/image_plus_4-100x100.png new file mode 100644 index 00000000..009c3e6c Binary files /dev/null and b/public/cache/catalog/demo/image_plus_4-100x100.png differ diff --git a/public/cache/catalog/demo/zhanweitu-100x100.png b/public/cache/catalog/demo/zhanweitu-100x100.png new file mode 100644 index 00000000..b9a9f868 Binary files /dev/null and b/public/cache/catalog/demo/zhanweitu-100x100.png differ