webcito/php-auto-image-resizer
Composer 安装命令:
composer require webcito/php-auto-image-resizer
包简介
description
README 文档
README
A PHP file that scales and delivers images based on the screen width.
Requirements
You must have at least PHP 8.2 with the PHP extension ext-imagick.
Preparation
First, the HTACCESS file must be adapted or created in the document root.
Add the following lines:
<IfModule mod_rewrite.c> # Turning on the apache rewrite engine RewriteEngine On # Specifies the base URL for per-directory rewrites RewriteBase / # First rule: If the requested path is "image-resizer.php", stop processing further rules. # ^image-resizer\.php$ - is a regular expression that exactly matches "image-resizer.php" # [L] is a flag that tells mod_rewrite to stop processing the rule set RewriteRule ^image-resizer\.php$ - [L] # Second rule: If the requested URI is any(.) file that ends (anchors the end position $) # with jpeg, jpg, gif, png, or webp, rewrite the request to "/image-resizer.php" # \.(?:jpeg|jpg|gif|png|webp)$ - is a regular expression that matches jpeg, jpg, gif, png, or webp files. # /image-resizer.php is the target where the matched request will be rewritten to # [QSA,L] are flags. QSA, or 'QueryString Append', forces the rewrite engine to append # a query string part in the substitution string to the existing one. L, or 'last', # stops mod_rewrite from processing any more rules. RewriteRule \.(?:jpeg|jpg|gif|png|webp)$ /image-resizer.php [QSA,L] </IfModule>
The lines cause all images with the extension jpeg, jpg, gif, png or webp to be sent to a PHP file called image-resizer.php. The name of the PHP file can be changed, but must then also be in the htaccess can be adjusted.
Make sure your web server has the rewrite module active. I describe it using Apache2.
For Apache2, it might look like this:
sudo a2enmod rewrite
If not already done, set AllowOverride to All: Apache does not allow .htaccess overrides by default.
Edit your apache2.conf file (usually found in /etc/apache2/apache2.conf) and change AllowOverride None to
AllowOverride All for the corresponding directory(s).
<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory>
Restart the web server:
sudo service apache2 restart
Installation
via composer
composer install webcito/php-auto-image-resizer dev-main composer dump-autoload
manually
Load the PHP class /src/ImageCache.php into your project.
Use
Create a PHP file called image-resizer.php in the DocumentRoot
The contents of the file should look something like this:
<?php use Webcito\ImageCache; require_once "vendor/autoload.php"; // via composer // require_once "path/to/ImageCache.php"; // or manual $options = [ "cache" => "/cache/", // the folder from documentRoot "resolution" => null, // the max screen width "breakpoints" => [1200, 992, 768, 480, 320], // the breakpoints "compressionQuality" => 85 // quality ]; new ImageCache($options);
Make sure there are enough rights on the documentRoot to create the cache folder (e.g., 0755).
This should lay the foundation.
Passing the maximum screen width or height
Since there is no direct way to measure screen width in PHP, I use Javascript.
On each of my public pages I set a cookie in the head and pass it on into the options.
<script> document.cookie = "resolution=" + Math.max(screen.width, screen.height)+"; path=/; SameSite=None"; </script>
$options = [ 'resolution' => $_COOKIE['resolution'] ?? zero ]; new ImageCache($options);
That's it!
webcito/php-auto-image-resizer 适用场景与选型建议
webcito/php-auto-image-resizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 05 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 webcito/php-auto-image-resizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webcito/php-auto-image-resizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 24
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2024-05-23