indent/imgcache

自动缓存您的 Laravel 应用中来自任何源的照片

dev-master 2022-05-19 07:07 UTC

This package is auto-updated.

Last update: 2024-09-19 12:14:35 UTC


README

在您的 Laravel 应用中本地缓存来自任何源的照片

安装

  1. 使用 Composer 安装
composer require indent/imgcache --prefer-dist
  1. public/ 目录和 storage/imgcache/ 之间创建符号链接
php artisan imgcache:link
  1. config/services.php 中添加您的 Cloudinary 云名称。您可以在 Cloudinary 控制台中找到您的云名称。
return [
    'imgcache' => [
        'cloudinary' => [
            'cloud_name' => env('CLOUDINARY_CLOUD_NAME', '<Your cloud name>'),
        ],
    ],
];

使用方法

通过调用 facade 或使用全局助手来使用 Imgcache。

Facade

class ProductController
{
    public function show()
    {
        $img = Imgcache::make('https://picsum.photos/id/1/100/100')->get();

        return view('product.show', [
            'img' => $img,
        ]);
    }
}

助手

全局助手在渲染 HTML 或 Blade 视图中的图像时很有用

{{-- When called in a Blade view Imgcache will be stringified --}}
<img src="{{ imgcache($imageUrl) }} alt="...">

{{-- If you want to be explicit, you can call `get()` --}}
<img src="{{ imgcache($imageUrl)->get() }} alt="...">

内联散列

您可以为 Blade 模板中的内联加载生成非常小的图像散列,然后在稍后进行懒加载。

<img src="{{ imgcache($imageUrl)->width(150)->blur(2000)->format('webp')->base64() }} alt="...">

API

base64(): string;

返回 base64 编码的图像字符串

blur(int {1, 2000} $blur): self;

将模糊效果应用于图像

brightness(int {-99, 100} $brightness): self;

将亮度/暗度应用于图像

crop(int {50+} $width, int|null {null|50+} $height = null): self;

将图像裁剪到指定大小

get(): string;

返回图像的相对 URL

height(int {50+} $height): self;

将图像调整到指定高度,并自动调整宽度

make(string {url} $source): self;

创建 Imgcache 的新实例

pixelate(int {1, 200} $pixelate): self;

将马赛克效果应用于图像

width(int {50+} $width): self;

将图像调整到指定宽度,并自动调整高度

驱动程序

当前仅通过他们的获取 API 支持通过 Cloudinary。