定制 kiatng/php-sharp 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Install dependencies:
composer install
npm install
  1. Make your changes and add your feature
  2. Write or update tests for your changes if applicable
  3. Run tests to ensure everything works:
composer test
  1. Commit your changes (git commit -m 'Add some amazing feature')
  2. Push to the branch (git push origin feature/amazing-feature)
  3. 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

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-07