takshak / imager
为 Laravel 提供的 Imager 插件包
Requires
- intervention/image: ^2.7
README
此包提供了三个不同的工具,这些工具用于模拟、种子、生成占位符以及操作图像。这些工具分别称为 Picsum(提供种子数据库的图像或帮助创建模型工厂)、Placeholder(为特定位置的图像生成占位符图像,具有不同的尺寸和颜色)、Imager(以所需尺寸操作图像,如果您不想裁剪图像的任何边缘,则可以使用此功能来调整图像大小)。这些操作也可以从 URL 中调用。
快速入门
使用 Composer 安装包
composer require takshak/imager
简单用法
use Takshak\Imager\Facades\Picsum;
use Takshak\Imager\Facades\Placeholder;
use Takshak\Imager\Facades\Imager;
Picsum::dimensions(500, 500)->response();
Placeholder::dimensions(500, 500)->text('Some Info Text')->response();
Imager::init($request->file('image_file'))->resizeFit(500, 500)->response();
用户可以直接使用相应的别名,而不需要在顶部使用类名。例如
\Picsum::dimensions(500, 500)->response();
\Placeholder::dimensions(500, 500)->text('Some Info Text')->response();
\Imager::init($request->file('image_file'))->resizeFit(500, 500)->response();
Placeholder(占位符/头像图像提供者)
您可以拥有一些假图像、一些具有指定尺寸的占位符图像,上面有一些文字,也可以使用不同的颜色。您还可以生成用户头像。图像可以进一步调整大小、保存到文件夹,也可以保存到模型中。
background($hexColor):
设置图像背景颜色
image($width=null, $height=null, $background=null):
如果图像尚未生成,则实例化图像。所有参数都是可选的
text($text='Placeholder Image', $format=[]):
在图像上写文字并格式化文字。可能的格式数组键是:size、color、align、valign、angle。例如。
\Placeholder::dimensions(500, 500)
->text('dummy text', [
'size' => 50,
'color' => '#000',
'align' => 'center',
'valign' => 'center',
'angle' => 45
])
->response();
url():
生成给定类型图像的本地 URL。指定的图像也可以通过此 URL 调用
有关所有其他函数,请参阅 常用方法
默认值:$width = 500;
$height = 500;
$background = '#ccc';
$extension = 'jpg';
$textFormating = [ 'size' => 24, 'color' => '#000', 'align' => 'center', 'valign' => 'center', 'angle' => null ];
从 URL 生成占位符图像
基本 URL:http://project.com/imgr/placeholder?paramters
示例图像 URL:http://project.com/imgr/placeholder?w=150&h=150&text=JD&text_size=60&background=c00c0c
Picsum(假图像提供者)
它提供一些图像以伪造数据库或用作占位符。它将一些图像存储在其存储桶中,然后根据请求随机生成图像。下面给出了函数和默认参数
bucket($foldername):
图像将伪造的源文件夹名称。如果没有设置,默认使用 'imgr-bucket' 文件夹。
disk($disk='local'):
存储磁盘名称
seed(int: $count=10):
使用一些假图像填充存储桶
flush():
从存储桶中清除所有图像
refresh(int: $count=10):
从存储桶中清除所有图像并填充新图像
isEmpty():
检查存储桶是否为空
url():
生成给定类型图像的本地 URL。指定的图像也可以通过此 URL 调用
image():
如果图像尚未生成,则实例化图像
有关所有其他函数,请参阅 常用方法
向存储桶中添加图像
要添加/写入一些图像(以便在请求 Picsum 时提高性能)执行以下命令
php artisan imager:seed
此命令也可以接受多个参数和选项
参数
count
:要添加的图像数量(默认 '50')
选项
--disk
:存储磁盘类型(默认 'local')--bucket
:这是保存图像的文件夹名称(默认 'imgr-bucket/')--action
:命令的操作。可能的值是,seed、refresh、flush(默认 'seed')--width
:要添加的图像宽度(默认 '2000')--height
:要添加的图像高度(默认 '1500')
php artisan imager:seed 25
php artisan imager:seed 25 --width=1200 --height=800
从 URL 生成 Picsum 图像
基本URL: http://project.com/imgr/picsum?paramters
示例图片URL: http://project.com/imgr/picsum?w=150&h=150&blur=1&greyscale=1
Imager(简易图像处理)
此功能可用于调整图像大小、适应特定尺寸或其他操作。提供以下方法:
init($image):
接收要处理的图像文件/路径/URL
resizeHeight(int: $height):
定义图像的像素高度。(如果已调用height()函数,则参数为可选)
resize(int: $width, int: $height):
定义图像的宽度和高度(像素)(图像可能会挤压或裁剪边缘)。(如果已调用height()、width()或dimensions()函数,则参数为可选)
resizeFit(int: $width, int: $height):
定义图像的宽度和高度(像素),以适应最佳,不会裁剪任何边缘,图像也不会拉伸。(如果已调用height()、width()或dimensions()函数,则参数为可选)
inCanvas($bg=null):
如果你想生成带有任何颜色的背景的图像,例如。(#fff)
有关所有其他函数,请参阅 常用方法
常用方法
以下方法可用于所有上述工具
width(int: $width):
定义图像的宽度
height(int: $height):
定义图像的高度
dimensions(int: $dimensions):
使用此函数一次定义宽度和高度,而不是width()和height()
extension($extention):
图像扩展名,例如:'png'或'jpg'
response($extention=null):
返回图像作为响应。
basePath($path):
设置要存储图像的基本文件夹名称
save($path, $width=null):
将路径保存到任何位置。宽度参数为可选。如果传递宽度,则图像将调整大小。
saveModel($model, $column, $filePath):
将图像路径保存到模型。
blur(int: $amount=1):
模糊图像
greyscale():
将图像转换为灰度
rotate(int: $deg=45):
在$deg度旋转图像
flip($direction='h'):
翻转图像,可能的值(v或h)
others(function($image){}):
将图像通过其他函数(intervention/image)进行进一步处理。
destroy():
销毁创建的图像实例。
占位符 - 一些用途
获取默认占位图像
\Placeholder::text('Some Info Text')->response();
在图像中添加自定义文本
\Placeholder::text('Some Info Text', [
'size' => 24,
'color' => '#000',
'align' => 'center',
'valign' => 'center',
'angle' => 45
])->response();
创建占位用户头像
\Placeholder::dimensions(200, 200)->text('JD')->response();
将图像保存到文件夹和模型
\Placeholder::text('Some dummy text')
->basePath(\Storage::disk('local')->path('images'))
->save('image-lg.jpg')
->saveModel($user, 'profile_img', 'image-lg.jpg')
->save('image-md.jpg', 300)
->saveModel($user, 'profile_img', 'image-md.jpg')
->response();
Picsum - 一些用途
在保存时调整图像大小
Picsum::dimensions(200, 200)->save(path:'path/image.jpg', 100);
在多个位置保存
Picsum::dimensions(500, 500)
->save(path:'path/image.jpg')
->save(path:'path/image-md.jpg', 300)
->save(path:'path/image-sm.jpg', 100);
获取图像URL
Picsum::dimensions(500, 500)->image()->url();
将路径保存到模型
Picsum::dimensions(500, 500)
->save(path:'path/image.jpg')
->saveModel(User::first(), 'profime_img', path:'path/image.jpg');
获取图像的URL
Picsum::dimensions(500, 500)->url();
其他intervention回调函数
$img = Picsum::image()->flip();
$img->others(function($img){
$img->crop(100, 100);
});
return $img->response();
将图像保存到模型
$post = Post::first();
Picsum::dimensions(500, 500)
->save(path:'path/image.jpg')->saveModel($post, 'image_lg', path:'path/image.jpg')
->save(path:'path/image-sm.jpg', 100)->saveModel($post, 'image_sm', path:'path/image-sm.jpg')
->image(500, 400)
->save(path:'path/image.jpg')->saveModel(Post::find(2), 'image_lg', path:'path/image.jpg')
->url()
Imager - 一些用途
将同一图像的三个版本保存到storage/app/public/images/... .jpg
Imager::init($request->file('thumbnail'))
->resizeFit(800, 500)
->basePath(Storage::disk('public')->path('/'))
->save('images/large.jpg')
->save('images/medium.jpg', 400)
->save('images/small.jpg', 200);
# Or you can pass the path as follow
Imager::init($request->file('thumbnail'))
->resizeFit(800, 500)
->save(Storage::disk('public')->path('images/large.jpg'))
->save(Storage::disk('public')->path('images/medium.jpg'), 400)
->save(Storage::disk('public')->path('images/small.jpg'), 200);
将图像保存为白色(默认)背景的画布,并进行一些额外处理
Imager::init($request->file('thumbnail'))
->resizeFit(800, 500)->inCanvas('#fff') // resize to fit in given dimension
->inCanvas('#fff') // put the white canvas if image is shorter
->rotate(45) // rotate image at 45 deg.
->flip('h') // flip image horizontally (use 'v' for vertical flip)
->save(Storage::disk('public')->path('images/large.jpg')) // save the image
->save(Storage::disk('public')->path('images/medium.jpg'), 400) // save another with small size of 400