kiatng/php-sharp
最新稳定版本:v0.0.2
Composer 安装命令:
composer require kiatng/php-sharp
包简介
A PHP wrapper for sharp js
README 文档
README
A PHP wrapper for lovell/sharp, a high performance Node.js image processing library.
Common use cases:
- Resize image
- Convert image format
For more information on Sharp, see the official documentation here.
Prerequisites
Install node in your system. Installation of node is very simple.
Check if you have node installed:
node -v
The command should display the version number.
Installation
Install the packages at the root of your PHP project:
composer require kiatng/php-sharp npm install sharp
This last command will create a node_modules directory, package-lock.json and package.json.
Requirements
- PHP >= 7.4
- Node.js and npm (for Sharp installation)
- Sharp npm package
Usage
There is only one static method run in the Sharp class. It takes three arguments:
$io: Input and output parameters$params: Parameters for the image processing$constructorOptions(optional): Sharp constructor options (e.g.,density,failOn,limitInputPixels)
Refer to the Sharp documentation on the specifications of the parameters.
Examples
Convert SVG to PNG and resize the image:
use KiatNg\Sharp; $png = Sharp::run( [ 'input' => ['is_raw' => true, 'data' => $svg, 'ext' => 'svg'], 'output' => ['is_raw' => true], ], [ 'toFormat' => ['format' => 'png'], // Required for raw output 'resize' => ['width' => 300, 'height' => 200] ] ); // Returns a raw PNG binary string
is_raw is a boolean parameter that indicates if the input is a raw data or a file path.
$svg is the raw SVG XML string.
ext is the image format: jpg, png, svg of the source data; it's used as the file extension internally.
Example with file paths including filename and extension:
Sharp::run( [ 'input' => ['is_raw' => false, 'data' => $svgPath], 'output' => ['is_raw' => false, 'file' => $pngPath], ], [ //'toFormat' => ['format' => 'png'], Not required if $pngPath has .png extension 'resize' => ['width' => 300, 'height' => 200] ] );
Example with constructor options (e.g., setting DPI for SVG rendering):
Sharp::run( [ 'input' => ['is_raw' => false, 'data' => $svgPath], 'output' => ['is_raw' => false, 'file' => $pngPath], ], [ 'resize' => ['width' => 300, 'height' => 200] ], ['density' => 300] // Set DPI to 300 for higher quality SVG rendering );
Refer to the test cases in tests/Unit/SharpTest.php for more examples.
Contributing
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Install dependencies:
composer install npm install
- Make your changes and add your feature
- Write or update tests for your changes if applicable
- Run tests to ensure everything works:
composer test
- Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Code Style
- Follow PSR-12 coding standards
- Add tests for new features
- Update documentation as needed
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgement
This project was inspired by choowx/rasterize-svg.
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-07