tomkirsch/resizer
Composer 安装命令:
composer require tomkirsch/resizer
包简介
Image resizer library for CI4
README 文档
README
Installation
Create the config file app/Config/ResizerConfig.php
<?php
namespace Config;
class ResizerConfig extends \Tomkirsch\Resizer\ResizerConfig
{
public bool $useCache = TRUE; // turning this off may cause performance issues
public bool $allowUpscale = FALSE; // whether to allow upscaling images, recommended FALSE (note this could break a server if a user requests a very large image)
public int $maxSize = 0; // max output size for all images, set to 0 to disable
public string $rewriteSegment = 'img'; // this "folder" lets htaccess know to rewrite the request to Resizer controller. must match your public/.htaccess file regex
public string $rewriteSizeSep = '-'; // separator from base file name and requested size. must match your .htaccess file regex
public string $realImagePath = ROOTPATH . '/public'; // real path to source images. this can be a private folder.
public string $resizerCachePath = ROOTPATH . '/writable/resizercache'; // path to store cached image files. be careful with this path, it can be wiped out when clearing cache!
public bool $addBaseUrl = TRUE; // whether to add base_url() to the output of publicFile()
public int $ttl = 60 * 60 * 24 * 7; // clean cached images older than this (seconds). Whenever a cache is accessed, it resets the timer
public float $randomCleanChance = 0.01; // chance of cleaning expired images on each image request (default at 10%). set to 0 to disable
public ?string $cacheControlHeader = 'public, max-age=2592000'; // Cache-Control header for browser caching (default is 30 days)
public bool $debugHeaders = FALSE; // whether to output extra headers for debugging
public array $pictureDefaultBreakpoints = [576, 768, 992, 1200, 1400]; // default sizes for picture element, based off bootstrap breakpoints
public array $pictureDefaultDprs = [1, 2]; // default device pixel ratios to support
public string $pictureDefaultSourceExt = '.jpg'; // default source extension for picture element
public string $pictureDefaultDestExt = ''; // default output extension for picture element. leave empty to use source extension by default.
public string $pictureNewlines = "\n"; // newlines for picture element output, set to '' for minified output
public string $loading = 'auto'; // default loading attribute for images: 'lazy', 'eager', or 'auto'
public string $fetchPriority = 'auto'; // default fetchpriority attribute for images: 'high', 'low', or 'auto'
public string $pictureDefaultLowRes = 'pixel64'; // 'pixel64' (transparent pixel), 'first', 'last', 'custom', or supply the name to be appended to the file option.
}
Create the service in app/Config/Services.php
public static function resizer($config = null, bool $getShared = TRUE): \Tomkirsch\Resizer\Resizer
{
$config = $config ?? new ResizerConfig();
return $getShared ? static::getSharedInstance('resizer', $config) : new \Tomkirsch\Resizer\Resizer($config);
}
Add the routes in app/Config/Routes.php
$routes->get('resizer/read', '\Tomkirsch\Resizer\ResizerController::read'); // expects these GET params: file, size, sourceExt, destExt, (dpr)
$routes->get('resizer/cleandir', '\Tomkirsch\Resizer\ResizerController::cleandir'); // pass ?force=1 to delete all files regardless of age
$routes->get('resizer/cleanfile', '\Tomkirsch\Resizer\ResizerController::cleanfile'); // pass ?file=filename.ext to delete all versions of a single file
Modify public/.htaccess to use the resizer controller:
# Image resizer
# img/some-folder/filename-1024.jpg.webp
RewriteRule ^img\/(.+)-([0-9]+)([.]\w+)([.]\w+)? resizer/read?file=$1&size=$2&sourceExt=$3&destExt=$4 [L,NC,QSA]
Add cache dir to .gitignore:
writable/resizercache/*
If you get a page not found, change $uriProtocol to use PATH_INFO in app\Config\App.php (public string $uriProtocol = 'PATH_INFO';) or .env (app.uriProtocol = 'PATH_INFO')
Usage
Use the Resizer::publicFile() method to display links to the files.
<?= img([
'src'=>Config\Services::resizer()->publicFile('kitten-src', 600),
]) ?>
Use the picture utility to automate breakpoints:
Config\Services::resizer()->picture(
[ // <picture> attr
'class' => 'my-picture',
],
[ // <img> attr
'alt' => 'kitten picture',
],
[ // options
'file' => 'kitten-src',
'breakpoints' => [576, 768, 992], // custom breakpoints
'loading' => 'lazy', // load only when in viewport
'fetchpriority' => 'low', // priority once in viewport
],
// additional <source>s. These take priority.
['media' => '(min-width: 600px)', 'breakpoints' => [390]] // show a 390px image if screen is 600px or larger. This will also support DPR (2x)
)
tomkirsch/resizer 适用场景与选型建议
tomkirsch/resizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 10 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tomkirsch/resizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tomkirsch/resizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 28
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-08