|
| 1 | +# PHP Sharp |
| 2 | + |
| 3 | +A PHP wrapper for [lovell/sharp](https://github.com/lovell/sharp), a high performance Node.js image processing library. |
| 4 | + |
| 5 | +Common use cases: |
| 6 | +- Resize image |
| 7 | +- Convert image format |
| 8 | + |
| 9 | +For more information on Sharp, see the official documentation [here](https://sharp.pixelplumbing.com/). |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +Install [node](https://nodejs.org/) in your system. Installation of node is very simple. |
| 14 | + |
| 15 | +Check if you have node installed: |
| 16 | +```bash |
| 17 | +node -v |
| 18 | +``` |
| 19 | +The command should display the version number. |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +Install the packages at the root of your PHP project: |
| 24 | +```bash |
| 25 | +composer require kiatng/php-sharp |
| 26 | +npm install sharp |
| 27 | +``` |
| 28 | + |
| 29 | +This last command will create a `node_modules` directory, `package-lock.json` and `package.json`. |
| 30 | + |
| 31 | +## Requirements |
| 32 | + |
| 33 | +- PHP >= 7.4 |
| 34 | +- Node.js and npm (for Sharp installation) |
| 35 | +- Sharp npm package |
| 36 | + |
| 37 | +## Usage |
| 38 | +There is only one static method `run` in the `Sharp` class. It takes two arguments: |
| 39 | +- `$io`: Input and output parameters |
| 40 | +- `$params`: Parameters for the image processing |
| 41 | + |
| 42 | +Refer to the [Sharp documentation](https://sharp.pixelplumbing.com/) on the specifications of the parameters. |
| 43 | + |
| 44 | +### Examples |
| 45 | + |
| 46 | +Convert SVG to PNG and resize the image: |
| 47 | +```php |
| 48 | +use KiatNg\Sharp; |
| 49 | + |
| 50 | +$png = Sharp::run( |
| 51 | + [ |
| 52 | + 'input' => ['is_raw' => true, 'data' => $svg, 'ext' => 'svg'], |
| 53 | + 'output' => ['is_raw' => true], |
| 54 | + ], |
| 55 | + [ |
| 56 | + 'toFormat' => ['format' => 'png'], // Required for raw output |
| 57 | + 'resize' => ['width' => 300, 'height' => 200] |
| 58 | + ] |
| 59 | +); |
| 60 | +``` |
| 61 | + |
| 62 | +`is_raw` is a boolean parameter that indicates if the input is a raw data or a file path. |
| 63 | +`$svg` is the raw SVG XML string. |
| 64 | +`ext` is the image format: jpg, png, svg of the source data; it's used as the file extension internally. |
| 65 | + |
| 66 | +File example: |
| 67 | +```php |
| 68 | +$png = Sharp::run( |
| 69 | + [ |
| 70 | + 'input' => ['is_raw' => false, 'data' => $svgPath], |
| 71 | + 'output' => ['is_raw' => false, 'file' => $pngPath], |
| 72 | + ], |
| 73 | + [ |
| 74 | + //'toFormat' => ['format' => 'png'], Not required if $pngPath has .png extension |
| 75 | + 'resize' => ['width' => 300, 'height' => 200] |
| 76 | + ] |
| 77 | +); |
| 78 | +``` |
| 79 | + |
| 80 | +Refer to the test cases in `tests/Unit/SharpTest.php` for more examples. |
| 81 | + |
| 82 | +## Contributing |
| 83 | + |
| 84 | +Contributions are welcome! Here's how you can help: |
| 85 | + |
| 86 | +1. Fork the repository |
| 87 | +2. Create your feature branch (`git checkout -b feature/amazing-feature`) |
| 88 | +3. Install dependencies: |
| 89 | +```bash |
| 90 | +composer install |
| 91 | +npm install |
| 92 | +``` |
| 93 | + |
| 94 | +4. Make your changes and add your feature |
| 95 | +5. Write or update tests for your changes if applicable |
| 96 | +6. Run tests to ensure everything works: |
| 97 | +```bash |
| 98 | +composer test |
| 99 | +``` |
| 100 | + |
| 101 | +7. Commit your changes (`git commit -m 'Add some amazing feature'`) |
| 102 | +8. Push to the branch (`git push origin feature/amazing-feature`) |
| 103 | +9. Open a Pull Request |
| 104 | + |
| 105 | +### Code Style |
| 106 | +- Follow PSR-12 coding standards |
| 107 | +- Add tests for new features |
| 108 | +- Update documentation as needed |
| 109 | + |
| 110 | +## License |
| 111 | + |
| 112 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 113 | + |
| 114 | + |
| 115 | +## Acknowledgement |
| 116 | + |
| 117 | +This project was inspired by [choowx/rasterize-svg](https://github.com/choowx/rasterize-svg). |
0 commit comments