salahmyn/laravel-image-resize

使用Intervention和Storage动态调整图片大小并返回URL

v2.1.1 2020-06-27 08:07 UTC

README

MaximumAdvertising/laravel-image-resize 分支

使用Intervention和Storage动态调整图片大小并返回URL

Latest Stable Version Latest Unstable Version

要求

  • Laravel 5+
  • Intervention Image ^2.4

支持的文件系统驱动器

  • 本地
  • S3
  • Oss (阿里云存储)

安装

此包可以通过Composer安装。

composer require salahmyn/laravel-image-resize

对于Laravel 5.4及以下版本,您需要手动注册服务提供者和别名。

添加到服务提供者

Mxmm\ImageResize\ImageResizeServiceProvider::class,

和别名

'ImageResize' => Mxmm\ImageResize\Facade::class,	

发布配置和资产(可选)

php artisan vendor:publish --provider="Mxmm\ImageResize\ImageResizeServiceProvider"

用法

接受的参数

/**
 * @param string|null $path
 * @param int|null $width
 * @param int|null $height
 * @param string $action
 * @return string
 */
public static function url(string $path = null, int $width = null, int $height = null, string $action = 'fit' , $disk = null): string

在HTML中

<img src="{{ ImageResize::url('originalDir/filename.jpg', width, height, [action]) }}" />

将图片调整到200x200px

<img src="{{ ImageResize::url('originalDir/filename.jpg', 200, 200, 'fit') }}" />

调整图片到200x200px

<img src="{{ ImageResize::url('originalDir/filename.jpg', 200, 200, 'resize') }}" />

将图片宽度调整为200px,高度自动

<img src="{{ ImageResize::url('originalDir/filename.jpg', 200, null, 'fit') }}" />

将图片宽度调整为200px,高度自动

<img src="{{ ImageResize::url('originalDir/filename.jpg', 200, null, 'fit') }}" />

示例输出

<img src="https:///thumbs/originalDir/fit/200x200/filename.jpg" />

别名 [资产]

使用 asset 方法处理 public 目录中的文件,需要配置 filesystems 中的 assets 驱动器

<?php

return [

    /*
    |-----------------------------------------------
    | Default Filesystem Disk
    |-----------------------------------------------
    |
    */
    // ...
    
    'disks' => [
        // ...

        'assets' => [
            'driver' => 'local',
            'root' => public_path('assets'),
        ],
    ];

在HTML中

<img src="{{ ImageResize::asset('originalDir/filename.jpg', width, height, [action]) }}" />

测试

使用以下命令运行测试

vendor/bin/phpunit