Skip to content

Динамическое создание изображений #719

Description

@tabuna

Текущее состояние

На текущий момент изображения для разных разрешений создаются автоматически при загрузке и хранятся отдельно как отдельная запись в базе данных. Типы и разрешения определяются в файле config/patform:

    /*
    |--------------------------------------------------------------------------
    | Attachment
    |--------------------------------------------------------------------------
    |
    | ....
    |
    */
    'attachment' => [
        'small'  => Orchid\Attachment\Templates\Small::class,
        'medium' => Orchid\Attachment\Templates\Medium::class,
        'large'  => Orchid\Attachment\Templates\Large::class,
    ],

И обрабатываются с помощью слушателя событий, благодаря этому есть возможность выбирать необходимый тип:

//Возвращает общий адрес изображения с требуемым разрешением изображения
$attachment->url('standart');

Это предполагает, что все изображения хранятся на диске постоянно, что явно не требуется

Предлагаемое изменение

Активно использовать intervention/imagecache, который предоставляет готовый функционал.

Настройка осуществляется в config/imagecache.php:

return [

    /*
    |--------------------------------------------------------------------------
    | Name of route
    |--------------------------------------------------------------------------
    |
    | Enter the routes name to enable dynamic imagecache manipulation.
    | This handle will define the first part of the URI:
    | 
    | {route}/{template}/{filename}
    | 
    | Examples: "images", "img/cache"
    |
    */

    'route' => 'img/cache',

    /*
    |--------------------------------------------------------------------------
    | Storage paths
    |--------------------------------------------------------------------------
    |
    | The following paths will be searched for the image filename, submited 
    | by URI. 
    | 
    | Define as many directories as you like.
    |
    */

    'paths' => [
        public_path('storage'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Manipulation templates
    |--------------------------------------------------------------------------
    |
    | Here you may specify your own manipulation filter templates.
    | The keys of this array will define which templates 
    | are available in the URI:
    |
    | {route}/{template}/{filename}
    |
    | The values of this array will define which filter class
    | will be applied, by its fully qualified name.
    |
    */

    'templates' => [
        'small'  => Intervention\Image\Templates\Small::class,
        'medium' => Intervention\Image\Templates\Medium::class,
        'large'  => Intervention\Image\Templates\Large::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Image Cache Lifetime
    |--------------------------------------------------------------------------
    |
    | Lifetime in minutes of the images handled by the imagecache route.
    |
    */

    'lifetime' => 43200,

];

После определения маршрута и путей (делать это придётся в ручную) будет доступна динамическое создание изображений с необходимыми параметрами:

Например:

http://localhost:8000/img/cache/small/images/4999c4acbde2553a25fff98b089a13caec7946f0.png

Заметки

Плюсы:

  • Хранение только необходимой информации

Минусы:

Голосование


Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions