fzerorubigd / silex-lazy-thumbnail-generator
适用于 silex 的懒加载缩略图生成器
Requires
- monolog/monolog: *
- silex/silex: ~1.0
- sybio/image-workshop: @stable
This package is not auto-updated.
Last update: 2024-09-10 06:43:23 UTC
README
使用 image-workshop 库从现有图像创建按需缩略图
如何使用
如果你在网站根目录下有一个名为 images 的目录,里面有很多图片,那么你可以使用这个提供者从原始图片创建(可选保存)缩略图。
为了更好的性能,你可以关闭实时图像处理,然后第一次当目标目录中没有可用的缩略图图像时,Silex 会处理请求,下次当有图像时,web 服务器(无论是什么)提供文件。(通常在原始文件不可用时进行重写)
$app->register( new \Cybits\Silex\Provider\LazyThumbnailGenerator() ); $app['lazy.thumbnail.mount_paths'] = array( //Each key for one mount point '/images' => array( //Allowed extension for file, comma separated 'allowed_ext' => 'jpg,jpeg,png', //array of allowed size for generating thumbnail, // In width.height format. * means any size 'allowed_size' => array('*.*'), //Max size allowed. optional, but take care! 'max_size' => '512.512', // Do not save the cache, default 'on_the_fly' => true, //Route name to bind 'route_name' => 'lazy_image_thumbnail_images' ) ); // If the web root is different thn the index.php directory then absolute path to the web root is required //$app['lazy.thumbnail.web_root'] = '/path/to/web/root'
lazy.thumbnail.mount_paths
挂载点的数组,每个挂载点需要一个键,键是网站根目录中的实际文件夹名称,以 '/' 前缀开头。
allowed_ext
是逗号分隔的允许扩展名。默认是 jpg,jpeg,png,gif
allowed_size
是允许大小的数组,格式为 width.height。例如,允许 256x256
和 512x512
,你应该使用 array('256.256', '512.512')
。对于任何大小,你可以使用 *
,例如 100.*
表示允许任何宽度为 100 的请求。还有一个可选参数用于最大允许大小,max_size
是 max_width.max_height
。
记住:防止访问任何大小是很重要的。更大的尺寸意味着更多的内存使用,这是危险的。
如果你想使用缓存机制,那么使用 on_the_fly
并将其设置为 true(默认为 false),对于使用 UrlGeneratorServiceProvider 的这个路由,你可以使用 route_name
分配一个名称。
-- 如果你愿意,可以在数组中通过 route
键传递路由,然后忽略数组键。
lazy.thumbnail.web_root
通常网站根目录是 index.php 的位置,但如果不是(在使用 php-fpm 的情况下可能会发生这种情况)将网站根目录的完整路径设置为此参数。
调用路由
这是简单的,假设你有一个 /images
路由(以及网站根目录中的文件夹)并且在该文件夹中有一个名为 image.jpg
的图像,并且你被允许使用 jpg 图像,且 256x256
大小可用,那么获取此路由
http://example.com/images/265x256/image.jpg
显示从原始图像的 256x256
缩略图。如果你关闭实时选项,则图像将在 /path/to/web/root/images/256x256/image.jpg
中创建,对该文件的任何新请求现在由 web 服务器处理。
如何贡献?
欢迎拉取请求,特别是这个 README :)
致谢
原始想法来自这里: cgray/nailed