Skip to content

Commit 8b9accd

Browse files
committed
v0.0.1 commit
1 parent 67f25a4 commit 8b9accd

File tree

9 files changed

+2331
-0
lines changed

9 files changed

+2331
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/tests/fixtures/tmp/*
2+
/node_modules
3+
/vendor
4+
create-test-image.php
5+
package-lock.json
6+
package.json
7+
.phpunit.result.cache

README.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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).

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "kiatng/php-sharp",
3+
"description": "A PHP wrapper for sharp js",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Ng Kiat Siong",
8+
"email": "kiatsiong.ng@gmail.com"
9+
}
10+
],
11+
"require": {
12+
"php": ">=7.4",
13+
"symfony/process": "^5.4|^6.0"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Kiatng\\Sharp\\": "src/"
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"Kiatng\\Sharp\\Tests\\": "tests/"
23+
}
24+
},
25+
"scripts": {
26+
"test": "phpunit"
27+
},
28+
"keywords": [
29+
"sharp",
30+
"image",
31+
"processing",
32+
"resize",
33+
"optimize"
34+
],
35+
"minimum-stability": "stable",
36+
"prefer-stable": true,
37+
"require-dev": {
38+
"phpunit/phpunit": "^9.0"
39+
}
40+
}

0 commit comments

Comments
 (0)