ngfw/webpconverter
Composer 安装命令:
composer require ngfw/webpconverter
包简介
A Laravel package that automatically converts images to WebP format, serves them efficiently, and caches the converted files to optimize performance.
README 文档
README
The WebP Converter package is your go-to tool for converting images to the WebP format. It’s built to make your images smaller, faster, and more efficient without sacrificing quality. Whether you're dealing with JPEGs, PNGs, or even BMPs, this package handles them all with ease. It supports both GD and Imagick drivers, giving you flexibility depending on your server environment. And yes, it’s PSR-4 compliant, so it fits seamlessly into your modern PHP projects.
Installation
You can install the package via Composer:
composer require ngfw/WebpConverter
After installing the package, you may want to publish the configuration file to customize the settings according to your project’s needs. To publish the configuration file, run the following command:
php artisan vendor:publish --tag="webp_converter"
This will create a webp_converter.php file in your config directory. Inside this file, you can configure the following options:
driver: Specifies the image processing library to use (gd or imagick).
quality: Sets the default quality for WebP conversion.
storage_path: Defines the default storage path for the converted WebP images.
Example configuration file (config/webp_converter.php):
return [ /* * Default Image Processing Driver */ 'driver' => env('WEBP_CONVERTER_DRIVER', 'gd'), /* * Default WebP Quality */ 'quality' => env('WEBP_CONVERTER_QUALITY', 80), /* * Storage Path */ 'storage_path' => env('WEBP_CONVERTER_STORAGE_PATH', 'public/webp_images'), ];
You can then customize these settings as needed to better fit your application’s requirements.
Usage
Using the WebP Converter is super simple. Here’s how you can integrate it into your project:
Loading an Image:
You can load an image from a local path or even a remote URL. The package is smart enough to handle both.
use Ngfw\WebpConverter\WebpConverterFacade as WebpConverter; // Load a local image // WebpConverter::load('/path/to/image.jpg'); // Load a remote image $imgUrl = "https://images.unsplash.com/photo-1724169913051-49f6ff76a070?q=80&w=3570&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"; $image = WebpConverter::load($imgUrl)->convert(); // /storage/webp_images/photo-1724169913051-49f6ff76a070.webp
Example of generating Thumbnail image:
$thumbnailUrl = WebpConverter::load($imgUrl) ->width(200) ->quality(70) // Set the quality for optimization ->optimize() // Apply optimization ->saveAs('optimized_thumbnail') ->convert(); // /storage/webp_images/optimized_thumbnail.webp
Setting Quality:
Want to control the quality of the output WebP image? No problem. Adjust the quality easily.
$converter->quality(80); // Set quality to 80%
Resizing the Image:
Need to resize the image? Just specify the width and height.
$converter->width(300)->height(200); // Resize to 300x200
Optimizing the Image:
Optimize your image to reduce file size even further. The package applies smart optimizations to deliver the best results.
$converter->optimize();
Converting to WebP:
Finally, convert your image to WebP. You can even specify a custom filename for the output.
$webpUrl = $converter->saveAs('optimized_image')->convert(); echo $webpUrl; // Outputs the URL to the converted WebP image
Chaining Multiple Methods:
You can chain multiple methods for a concise, single-line conversion:
$webpUrl = $converter->load('/path/to/image.jpg') ->quality(90) ->width(500) ->height(300) ->optimize() ->saveAs('final_image') ->convert(); echo $webpUrl; // Outputs the URL to the optimized WebP image
Serving the Image:
Serve the WebP image directly, or get the raw data if you need to do something custom.
$content = $converter->serve(true); // Serve the image as a response array
Blade Examples
Here are a couple of examples on how to use the WebP Converter in a Blade template:
Local Image Conversion: Convert a local image using Laravel's asset helper:
<img src="@webpConverter(asset('/images/png1.png'))" />
This will convert the image located at public/images/png1.png into WebP format and serve it.
Remote Image Conversion:
Convert an image from a remote URL:
<img src="@webpConverter('https://images.unsplash.com/photo-1724217552369-22b256e395d9?q=80&w=3270&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D')" />
This will download the image from the specified URL, convert it to WebP format, and then serve it.
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email the author instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
ngfw/webpconverter 适用场景与选型建议
ngfw/webpconverter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 407 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 08 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「ngfw」 「laravel webpconverter」 「webpconverter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ngfw/webpconverter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ngfw/webpconverter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ngfw/webpconverter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
WebParser is a PHP library that allows developers to parse and query webpages using an ORM-like syntax. It facilitates the extraction of HTML elements by chaining operations such as filtering by ID or class, ordering results, and limiting output. WebParser offers a flexible interface for exploring a
A Laravel package to fetch DNS reconnaissance data from the DNSDumpster API, easily installable via Composer and configurable as a service provider.
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
统计信息
- 总下载量: 407
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-25