thapp / jitimage
即时图像处理。
Requires
- php: >=5.4.0
- illuminate/support: ~4.1
- symfony/filesystem: ~2.4
- symfony/http-foundation: ~2.4
Requires (Dev)
- mikey179/vfsstream: dev-master
- mockery/mockery: dev-master
This package is not auto-updated.
Last update: 2024-09-25 11:16:44 UTC
README
即时图像处理,支持Laravel 4集成,支持GD、ImageMagick和Imagick。
安装
将thapp/jitimage添加到composer.json的要求中
{ "require": { "php":">=5.4.0", "thapp/jitimage": "~0.1" } }
然后运行composer update
或composer install
下一步是告诉Laravel加载服务提供者。在app/config/app.php
中添加
// ... 'Thapp\JitImage\JitImageServiceProvider' // ...
到providers
数组,并将JitImage
添加到别名数组中
'aliases' => [ 'JitImage' => 'Thapp\JitImage\Facades\JitImage' ],
发布配置
php artisan config:publish thapp/jitimage
配置
路由 (字符串)
动态图像处理的基础路由
基础 (字符串)
存储图像的文件系统基础路径。
驱动 (字符串)
处理驱动。可用的驱动有im
、gd
和ìmagick
缓存路由 (字符串)
按缓存ID检索图像的基础路由
缓存路径 (字符串)
缓存目录
缓存环境 (数组)
应启用imagecache的环境数组
缓存前缀 (字符串)
缓存图像的名称前缀
质量 (字符串)
压缩质量,0 - 100(越高越好,但也越大)
imagemagick (数组)
此数组包含两个值:path
,imagick二进制文件的路径,和bin
,二进制名称。
通常二进制名称为convert
。
过滤器 (数组)
应默认启用的可用过滤器数组
食谱 (数组)
预定义参数的数组,这些参数被用作路由别名,例如。
'recipes' => [ 'thumbs' => '2/200/200/5, filter:gs' ],
会创建一个名为'thumbs'的路由,可以通过以下方式调用:http://example.com/thumbs/path/to/my/image.jpg
。
定义食谱将禁用动态图像处理。
响应类型 (字符串)
可以选择generic
或xsend
。
注意:当使用xsend
响应类型时,您的服务器必须能够处理x-send headers。
'response-type' => 'generic'
可信站点 (数组)
提供资产的可信站点列表,例如。
http://25.media.tumblr.com
或作为正则表达式
http://[0-9]+.media.tumblr.(com|de|net)
图像处理器
GD
GD是标准的PHP图像处理库。如果您没有访问ImageMagick或Imagick PHP扩展的权限,请选择gd
。
尽管使用GD有一些缺点,例如,颜色配置文件不会被保留,没有支持在处理动画GIF文件时保留图像序列。它还具有更大的内存占用,因此在某些情况下可能变得不可行(例如共享主机平台上的内存限制等)。
ImageMagick
ImageMagick是一个快速且功能丰富的图像处理库。如果您可以访问convert
二进制文件,请在您的config.php
中选择im
。
有关ImageMagick的更多信息,请访问官方网站
Imagick
Imagick是PHP的图像Magick面向对象封装。如果您已安装imagick扩展但无法访问图像Magick二进制文件,请选择imagick
。
用法
动态图像处理
警告:动态图像处理可能会损害您的系统,应在生产环境中禁用。
图像URI的解剖结构
{base}/{parameter}/{imagesource}/filter:{filter}
参数由一个到五个组件组成,包括mode
、width
、height
、gravity
(裁剪位置)和background
。
一个图像URL可能看起来像这样:http://exmaple.com/images/2/200/200/5/path/to/my/image.jpg
要应用额外的过滤器,请附加过滤器URL段。过滤器段以filter:
开头,后跟过滤器别名和过滤器选项。过滤器由双冒号:
分隔,过滤器参数由分号;
分隔,例如:filter:gs;s=100;c=1:circ;o=12
。
示例
示例URL(假设您已将route
设置为'images'
,并且您的图像存储在public/uploads/images
中)。
调整大小
按比例调整图像宽度为200px
http://example.com/images/1/200/0/uploads/images/image.jpg
调整图像大小为200 * 200 px,忽略其宽高比
http://example.com/images/1/200/200/uploads/images/image.jpg
按比例调整图像以最佳适应400 * 280 px
http://example.com/images/4/400/280/uploads/images/image.jpg
将图像缩小到50%
http://example.com/images/5/50/uploads/images/image.jpg
限制为200.000px像素
http://example.com/images/6/200000/uploads/images/image.jpg
裁剪
按比例裁剪并调整图像大小为200px * 200px,重力为5(中心)
http://example.com/images/2/200/200/5/uploads/images/image.jpg
预定义图像处理
(将禁用动态处理)
您可以使用预定义的食谱将图像处理别名化。
示例
将模式2裁剪缩放映射到http://example.com/thumbs/uploads/images/image.jpg
,裁剪为200x200 px,并应用灰度滤镜
'thumbs' => '2/200/200/5, filter:gs'
将模式1调整大小映射到http://example.com/gellery/uploads/images/image.jpg
,宽度调整为800px,并应用灰度滤镜
'gallery' => '1/800/0, filter:gs',
将模式4最佳适应映射到http://example.com/preview/uploads/images/image.jpg
,最大宽度为800px,高度为600px
'preview' => '4/800/600'
模式
模式0
直接通过,不处理。
模式1 < width/height >
使用给定的宽度和高度值调整图像大小,除非其中一个值为零,否则忽略宽高比。
模式2 < width/height/gravity >
调整图像大小以适应由宽度和高度定义的裁剪边界。
重力解释
-------------
| 1 | 2 | 3 |
-------------
| 4 | 5 | 6 |
-------------
| 7 | 8 | 9 |
-------------
模式3 < width/height/gravity/[color] >
使用宽度和高度定义的裁剪边界裁剪图像。如果图像小于裁剪区域,将创建一个框架。
模式4 < width/height >
在给定边界内最佳适应。
模式5 < percentage >
百分比缩放。
模式6 < pixelcount >
像素限制。
转换图像格式(自v0.1.4起)
您可以使用Convert
过滤器(conv
)将图像转换为不同的文件格式。
作为URI
// convert png to jpg: '/images/<params>/<source>/filter:conf;f=jpg'
类JitImage
还提供了一些快捷方法:toJpeg
、toPng
和toGif
。
// convert png to jpg: JitImage::source($filePNG)->toJpeg()->get(); JitImage::source($fileJPP)->toPng()->scale(50);
过滤器
JitImage包含4个预定义过滤器:GreyScale
、Cirlce
、Overlay
、Colorize
和Convert
(自v0.1.3起)
注意:自v0.1.4起,在过滤器上调用无效参数将抛出\InvalidArgumentException
。
灰度
- alias `gs`
- options (not available for the `gd` driver)
- `b` (Brightness){integer}, 0-100
- `s` (Satturation){integer}, 0-100
- `h` (Hue){integer}, 0-100
- `c` (Contrast){integer} 0 or 1
圆形
- alias `circ`
- options
- `o` {integer} offset, any positive integer value
叠加
- alias `ovly`
- options
- `a` (alpha) {float} a float value between 0 and 1
- `c` (color) {string} hex representation of an rgb value
着色
- alias `clrz`
- options
- `c` (color){string} hex representation of an rgb value
转换
- alias `conv`
- options
- `f` (file format){string} a valid image file extension such as `png`, `jpg`, etc.
外观类
这是在blade模板中缩放图片的一种便捷方法。它将创建一个类似 /jit/storage/2egf4gfg/jit_139e2ead8b71b8c7e.jpg
的imageurl。
注意:如果禁用了缓存和动态处理,则此功能将无法正常工作。
注意:过滤器(包括转换快捷方式)必须在其他任何操作方法之前调用,因为resize
、scale
等会立即将计算出的文件源作为字符串返回。
// get the original image: JitImage::source('path/to/myimage.jpg')->get(); // proportionally resize the image have a width of 200px: JitImage::source('path/to/myimage.jpg')->resize(200, 0); // resize the image have a width and height of 200px (ignores aspect ratio): JitImage::source('path/to/myimage.jpg')->resize(200, 200); // crop 500px * 500px of the image from the center, creates a frame if image is smaller. JitImage::source('path/to/myimage.jpg')->crop(500, 500, 5); // You may also specify a background color for the frame: JitImage::source('path/to/myimage.jpg')->crop(500, 500, 5, 'fff'); // crop 500px * 500px of the image from the center, resize image if image is smaller: JitImage::source('path/to/myimage.jpg')->cropAndResize(500, 500, 5); // resize the image to best fit within the given sizes: JitImage::source('path/to/myimage.jpg')->fit(200, 200); // crop 200px * 200px of the image from the center, resize image if image is smaller and apply a greyscale filter: JitImage::source('path/to/myimage.jpg')->filter('gs')->cropAndResize(200, 200, 5); // Percentual scale the image: JitImage::source('path/to/myimage.jpg')->scale(50); // Limit the image to max. 200000px: JitImage::source('path/to/myimage.jpg')->pixel(200000); // Convert png to jpg: JitImage::source('path/to/myimage.png')->toJpeg()->get();
注册外部过滤器
您可以为JitImage添加自己的过滤器类。
(待续)。
Event::listen('jitimage.registerfilter', function ($driverName) { return [ "mf" => sprintf("Namespace\\Filter\MyFilter\\%s%s", ucfirst($driverName) , 'MfFilter') ]; });
缓存
Artisan命令
目前只有一个命令。使用php artisan jitimage:clearcache
可以清除整个图像缓存。
如果源文件被替换,则删除缓存的图片
可以删除由特定源创建的缓存图片。假设您需要替换uploads/images
中的名为myimage.jpg
的图片,您可以指示缓存类删除此特定的缓存目录。
$app['jitimage.cache']->delete('uploads/images/myimage.jpg');
您也可以将其钩到上传事件上
// attention! pseudo code: Event::listen('image.upload', function ($event) use ($app) { $app['jitimage.cache']->delete($event->image); });
API
API文档将很快更新。