ampedweb/laravel-glide-in-a-box

为Laravel的Glide PHP提供的开箱即用解决方案

v0.7.0 2024-04-27 10:00 UTC

This package is auto-updated.

Last update: 2024-08-27 10:55:53 UTC


README

Latest Stable Version License

为Laravel提供开箱即用的Glide PHP解决方案,提供Glide API的流畅接口。

完整文档和示例在此: https://ampedweb.github.io/laravel-glide-in-a-box/

如果您只想在项目中使用流畅的URL构建器,可以在此找到: https://github.com/ampedweb/glide-url-helper。当前那里的文档仍在进行中...!

要求

  • PHP >= 7.3,以下扩展
    • Exif
    • GD ImageMagick
  • league/glide-laravel": "^1.6",

特性

  • 使用glide_url()辅助函数时,会自动生成签名URL。
  • 所有Glide图像API的流畅接口,请参阅此处: https://glide.thephpleague.com

安装

composer require ampedweb/laravel-glide-in-a-box

发布配置文件

php artisan vendor:publish --tag=glideinabox  

基本用法

所有Glide图像URL请求的“开箱即用”基本URL是"/img/"。一旦发布,您可以在glideinabox.php配置文件中调整此设置。

使用glide_url()辅助函数应该可以使构建图像URL变得简单。

使用预设作为基础并使用流畅方法进行一些修改的示例

glide_url($pathToYourImageFile)->preset('medium')->filter('sepia')->url();

// You can also cast the FluentUrlBuilder to a string:

$url = (string)glide_url($pathToYourImageFile)->preset('medium')->filter('sepia'); 

// Or print it:

print glide_url($pathToYourImageFile)->preset('medium')->filter('sepia');

// Or use it directly in your blade templates:

{{ glide_url($pathToYourImageFile)->preset('medium')->filter('sepia') }}

如果更喜欢使用预定义常量而不是字符串,则也存在预定义的常量,例如

use AmpedWeb\GlideUrl\Interfaces\Filter;

glide_url($pathToYourImageFile)->preset('medium')->filter(Filter::SEPIA)->url();

您也可以构建一个完全没有预设的完全自定义的图像。以下是一个200x100像素的裁剪webp图像,质量为50%

use AmpedWeb\GlideUrl\Interfaces\Fit;

glide_url($pathToYourImageFile)->size(200,100)->fit(Fit::CROP)->webp(50)->url();

始终记得在配置完图像后调用->url()方法。

扩展

如果您想使用自己的图像控制器但仍然想使用该包的基本功能,则需要扩展

AmpedWeb\GlideInABox\Controller\GlideImageController;

运行测试

php vendor/bin/phpunit