helilabs / laravel-mediable-manipulation
Requires
- intervention/image: ^2.4
- plank/laravel-mediable: ^2.5
Requires (Dev)
- mockery/mockery: ^1.0
- orchestra/testbench: ^3.5
- phpunit/phpunit: ^6.4
This package is not auto-updated.
Last update: 2024-01-05 07:57:51 UTC
README
此包是知名 Laravel 包 Laravel Mediable 的扩展。
此包的主要目的是提供兼容且快速的媒体操作功能。目前它仅支持图像操作。
图像操作
图像操作是通过 Intervention\Image 包实现的,您可以使用其方法来操作图像
使用 manipulateImage 函数来操作图像,它接受方法名作为第一个参数,方法参数数组作为第二个参数,例如,如果您想调整图像大小
$media->manipulateImage('resize',['w' => 250, 'h' => 250] ) ->applyImageManipulation()
这将生成一个新文件,其中包含指定的操作属性,请注意,您可以应用多个操作属性
$media->manipulateImage('resize',['w' => 250, 'h' => 250] ) ->manipulateImage('colorize', ['Red' => 10, 'Green' => 20, 'Blue' => 30 ] ) ->applyImageManipulation()
如果您想连续进行两次操作,请确保在第二次操作之前清除图像操作属性。
//first manipulation resize then colorize then generate $media->manipulateImage('resize',['w' => 250, 'h' => 250] ) ->manipulateImage('colorize', ['Red' => 10, 'Green' => 20, 'Blue' => 30 ] ) ->applyImageManipulation(); //second manipulation resize then colorize then draw a line $media ->clearImageManipulationProperties() ->manipulateImage('resize',['w' => 250, 'h' => 250] ) ->manipulateImage('colorize', ['Red' => 10, 'Green' => 20, 'Blue' => 30 ] ) ->manipulateImage('line', ['x1' => '0', 'y1' => '0', 'x2' => 20, 'y2' => '20' ] ) ->applyImageManipulation();
操作结果
假设我们有一个具有以下属性的媒体
$media->directory = 'foo/bar'; $media->filename = 'baz'; $media->filename = 'jpg';
如果对媒体应用了操作,则不会写入原始文件,以保持原始文件,而是会生成一个新文件,其中包含应用的操作属性
如果您对媒体应用了 resize 操作,则将生成一个名为 foo/bar/baz-resize+250,250.jpg 的新文件
如果您对媒体应用了 resize 和 colorize 操作,则将生成一个名为 foo/bar/baz-colorize+10,20,30|resize+250,250.jpg 的新文件。
请注意,操作按字母顺序排序并添加到文件名中,如果您想知道原因,因为我们希望避免每次请求时都在原始文件上实现操作属性,而是包将搜索相应的文件,如果存在,则包将返回它。如果您想知道我们为什么要这样做,因为您知道处理/处理器/性能很昂贵,但存储空间不贵。
媒体访问器
Larave Mediable 引入了多个媒体访问器,如 getUrl、getDiskPath 和 getAbsolutePath,因此您可以使用它们来获取操作后的文件
$media->manipulateImage('resize',['w' => 250, 'h' => 250] ) ->manipulateImage('colorize', ['Red' => 10, 'Green' => 20, 'Blue' => 30 ] ) ->applyImageManipulation() ->getUrl()
前面的代码块将返回相对于媒体磁盘的文件的 URL foo/bar/baz-colorize+10,20,30|resize+250,250.jpg
如果您想获取操作后的原始文件,可以使用 clearImageManipulationProperties 方法清除操作属性并获取正确的文件
// this will return foo/bar/baz-colorize+10,20,30|resize+250,250.jpg $media->manipulateImage('resize',['w' => 250, 'h' => 250] ) ->manipulateImage('colorize', ['Red' => 10, 'Green' => 20, 'Blue' => 30 ] ) ->applyImageManipulation() ->getDiskPath(); // this will return foo/bar/baz-colorize+10,20,30|resize+250,250.jpg $media->getDiskPath(); //this will return foo/bar/baz.jpg $media->clearImageManipulationProperties() ->getDiskPath();
移动、重命名和存在
如果您想将原始文件从一个位置移动到另一个位置,请注意,所有相关文件(操作结果)也将移动,这将有助于节省存储空间。
使用 Laravel Mediable 方法进行移动和重命名。
$media->move( $destination, $newFilename ); $media->rename($newFilename); $media->exists();
启用/禁用图像操作
要启用图像处理,请使用方法 enableImageManipulation,例如 $media->enableImageManipulation(),默认情况下功能已启用。
要禁用图像处理,请使用方法 disableImageManipulation,例如 $media->disableImageManipulation()。
在媒体创建后创建处理
您可以使用 registerImageManipulations 方法在媒体创建后创建处理,此方法应返回包含预定义处理的数组数组。
//in Your Media Model public function registerImageManipulations(){ return [ [ 'resize' => ['w' => 250, 'h' => 250] ], [ 'resize' => ['w' => 250, 'h' => 250], 'colorize' => ['red' => 10, 'green' => 20, 'blue' => 30] ] ]; }
现在,当模型文件的 created 事件发生时,它将在媒体上应用这些处理。
接下来是什么
图像处理非常重要,这也是我们为什么从这里开始并停止在这里的原因,但我们的计划是很快添加 视频处理功能,在此之前,请尽情享受并给出反馈。