novius/laravel-media-toolbox

此包帮助您即时优化图片

2.1.0 2020-10-15 12:28 UTC

This package is auto-updated.

Last update: 2024-09-26 11:21:59 UTC


README

Travis Packagist Release Licence

即时优化您的图片!🛫

要求

Laravel >= 8.0

注意:这些说明适用于 Laravel >= 8.0。如果您使用的是早期版本,请参阅旧版本的文档

安装

composer require novius/laravel-media-toolbox

然后,您必须发布并运行迁移以创建 language_lines 表

php artisan vendor:publish --provider="Novius\MediaToolbox\MediaToolboxServiceProvider" --tag="migrations"
php artisan migrate

然后添加以下内容到 config/app.php

// in 'aliases' => [ ... ]
'Medt' => Novius\MediaToolbox\Support\MediaToolbox::class,

使用

在视图中

<!-- This will change the height proportionaly, in order to keep the aspect -->
<img src="{{ Medt::asset('images/hello.png')->width(150) }}">

<!-- It works as well with external pictures. Setting the ratio will force
     previously defined dimensions and change the remaining ones, here the height -->
<img src="{{ Medt::asset('https://example.com/images/logo.png')->width(500)->ratio(16/9) }}">

<!-- fit('cover') is the default behavior. You can just omit it -->
<img src="{{ Medt::asset($user->getPicture())->size(140, 200)->fit('cover') }}">
<img src="{{ Medt::asset($user->getPicture())->size([140, 200])->fit('stretch') }}">

<!-- This will output a jpg with 75% quality.
     Lower number makes smaller files. Minimum is 1, max is 100 -->
<img src="{{ Medt::asset('images/something.gif')->quality(75) }}">

SEO 友好

您可以指定文件名(值将被转换为缩写名)

在视图中的使用

<img src="{{ Medt::asset('images/hello.png')->quality(90)->name('my image') }}">

配置

发布配置文件

php artisan vendor:publish --provider="Novius\MediaToolbox\MediaToolboxServiceProvider"

编辑 config/mediatoolbox.php

手动清理缓存

php artisan cache:clear

清除过期的媒体

在您的 app/Console/Kernel.php 文件中,您应该注册一个每日任务来清除过期的媒体

protected function schedule(Schedule $schedule)
{
    $schedule->command('media-toolbox:purge-expired')
        ->daily();
}

默认情况下,媒体在1周后被认为是过期的。您可以在配置文件中通过 `expire key` 覆盖此值。

代码风格检查

使用 php-cs 运行

composer run-script lint

贡献

欢迎贡献!在 Github 上留下一个问题,或创建一个 Pull Request。