pelock/yii2-imgopt
Composer 安装命令:
composer require pelock/yii2-imgopt
包简介
Image optimization widget for Yii2 Framework with auto WebP/AVIF image format generation from PNG/JPG files.
README 文档
README
ImgOpt is an image optimization widget for Yii2 Framework with auto WebP & AVIF image formats generation from PNG and JPG files.
https://www.yiiframework.com/extension/pelock/yii2-imgopt
How to make my website faster?
My website had all the beautiful images and screenshots, but there was one problem. Most of them were in PNG format, some of them weighted around 200 kB. And it adds up to the point where my website loading time was just slow.
I found about the WebP format, read that it's supported in the latest browsers and if it's not (only older Safari browsers), there's a way to overcome this and serve the default PNG or JPG images. Perfect.
But the entire process would require me to go manually and use some sort of image conversion tool, upload new WebP images to the server and upgrade my HTML code.
To hell with that! We can do better!
Automate PNG & JPG to WebP & AVIF conversion
I have decided to create a Yii2 widget that would automate this task.
What it does? Instead of static images like this:
<img src="/images/product/extra.png" alt="Extra product">
it will automatically generate an extra image in new WebP format (in the same directory, where the provided image is located) and serve it to your browser in HTML <picture> tag, with a default fallback to the original image for browsers that don't support WebP images.
Replace your IMG tag within your HTML templates with a call to:
<?= \PELock\ImgOpt\ImgOpt::widget(["src" => "/images/product/extra.png", "alt" => "Extra product" ]) ?>
(Image path is relative to Yii2 Framework @webroot alias)
And once run, the widget code will generate a new WebP & AVIF image files on the fly (original image is left untouched) and he following HTML code gets generated:
<picture> <source type="image/avif" srcset="/images/product/extra.avif"> <source type="image/webp" srcset="/images/product/extra.webp"> <img src="/images/product/extra.png" alt="Extra product"> </picture>
The browser will pick up the best source for the provided image, and thanks to revolutionary WebP and AVIF compression, it will make your website loading faster.
Image lazy-loading
Lazy images loading makes the browser load the images when it reach a certain point, after which the image became visible in the current browser tab. You can use this pure HTML feature (no JS involved) from within the widget:
<?= \PELock\ImgOpt\ImgOpt::widget(["src" => "/images/product/extra.png", "loading" => "lazy" ]) ?>
The generated output looks like this:
<picture> <source type="image/avif" srcset="/images/product/extra.avif"> <source type="image/webp" srcset="/images/product/extra.webp"> <img src="/images/product/extra.png" loading="lazy"> </picture>
Use it to make your website loading times even faster.
AVIF image generation (new in v1.3.0)
ImgOpt will automatically generate AVIF file if it's supported by the existing PHP installation. If the conversion function is not available, it will just skip this step.
Automatic WebP generation for updated images (new in v1.2.0)
ImgOpt will set the modification date of the generated WebP image to match the modification date of the original image file.
If ImgOpt detects that a file modification date of the source image file is different than the date of the previously generated WebP image file - it will automatically re-create the new WebP image file!
Installation
The preferred way to install the library is through the composer.
Run:
php composer.phar require --prefer-dist pelock/yii2-imgopt "*"
Or add:
"pelock/yii2-imgopt": "*"
to therequire section within your composer.json config file.
The installation package is available at https://packagist.org/packages/pelock/yii2-imgopt
The Yii2 extension is available at https://www.yiiframework.com/extension/pelock/yii2-imgopt
Source code is available at https://github.com/PELock/yii2-imgopt
Image quality
I knew you would ask about it! By default the conversion tries all the steps from 100% output image quality down to 70% to generate the WebP file that is smaller than the original image.
| Original PNG (181 kB) | Optimized WebP (60 kB) |
|---|---|
If the generated WebP or AVIF image is larger than the original image, the default <img> tag will be generated.
Disable WebP/AVIF images serving
If for some reason you want to disable WebP file serving via the HTML <picture> tag, you can do it per widget settings:
<?= \PELock\ImgOpt\ImgOpt::widget(["src" => "/images/product/extra.png", "alt" => "Extra product", "disable" => true ]) ?>
Recreate WebP/AVIF files
The widget code automatically detects if there's a WebP/AVIF images in the directory with the original image. If it's not there - it will recreate them. It's only done once.
If you wish to force the widget code to recreate it anyway, pass the special param to the widget code:
<?= \PELock\ImgOpt\ImgOpt::widget(["src" => "/images/product/extra.png", "alt" => "Extra product", "recreate" => true ]) ?>
You might want to recreate all of the WebP and AVIF files and to do that without modifying, change the widget source code from:
/** * @var bool set to TRUE to recreate *ALL* of the WebP and AVIF files again (optional) */ const RECREATE_ALL = false;
to:
/** * @var bool set to TRUE to recreate *ALL* of the WebP files again (optional) */ const RECREATE_ALL = true;
Lightbox 2 integration
You can also generate Lightbox (https://lokeshdhakar.com/projects/lightbox2/) friendly images.
Instead of:
<a href="/images/sunset.jpg" data-lightbox="image-1" data-title="Sunset"> <img src="/images/sunset-thumbnail.jpg" alt="Sunset"> </a>
You can replace it with more compact widget code:
<?= \PELock\ImgOpt\ImgOpt::widget(["lightbox_data" => "image-1", "lightbox_src" => "/images/sunset.jpg", "src" => "/images/sunset-thumbnail.jpg", "alt" => "Sunset" ]) ?>
And it will generate this HTML code:
<a href="/images/sunset.jpg" data-lightbox="image-1" data-title="Sunset"> <picture> <source type="image/avif" srcset="/images/sunset-thumbnail.avif"> <source type="image/webp" srcset="/images/sunset-thumbnail.webp"> <img src="/images/sunset-thumbnail.png" alt="Sunset"> </picture> </a>
Bugs, questions, feature requests
If you are interested in my software or have any questions regarding it, technical or legal issues, or if something is not clear, please contact me. I'll be happy to answer all of your questions.
Bartosz Wójcik
- Visit my site at — https://www.pelock.com
- Twitter — https://twitter.com/PELock
- GitHub — https://github.com/PELock
pelock/yii2-imgopt 适用场景与选型建议
pelock/yii2-imgopt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.5k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2021 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「image」 「optimization」 「png」 「yii2」 「jpg」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pelock/yii2-imgopt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pelock/yii2-imgopt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pelock/yii2-imgopt 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Image optimization / compression library. This library is able to optimize png, jpg and gif files in very easy and handy way. It uses optipng, pngquant, pngcrush, pngout, gifsicle, jpegoptim and jpegtran tools.
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
Optimize your email templates' styles via native Laravel templating mechanism.
A/B Testing of content elements
统计信息
- 总下载量: 7.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2021-03-17