|
| 1 | +# smallpics-php |
| 2 | + |
| 3 | +A PHP client library for generating URLs with SmallPics.io. This package provides a simple way to build URLs with SmallPics.io processing options. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +composer require smallpics/smallpics-php |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Creating Processing Options |
| 14 | + |
| 15 | +```php |
| 16 | +use smallpics\smallpics\Options; |
| 17 | + |
| 18 | +// Create a new Options object |
| 19 | +$options = new Options(); |
| 20 | + |
| 21 | +// Set options with individual setters |
| 22 | +$options |
| 23 | + ->setWidth(300) |
| 24 | + ->setHeight(400); |
| 25 | + |
| 26 | +// Generate the query string parameters |
| 27 | +echo $options->toString(); |
| 28 | +// Output: "width=300&height=400" |
| 29 | + |
| 30 | +// Options object can also be used directly in a string context |
| 31 | +echo $options; |
| 32 | +// Output: "width=300&height=400" |
| 33 | +``` |
| 34 | + |
| 35 | +### Alternative Initialization |
| 36 | + |
| 37 | +It is possible to initialize the Options object with an associative array. The keys of the array are the option names and the values are the option values. |
| 38 | + |
| 39 | +The values can either be a single value, an array of values, or an associative array of values. |
| 40 | + |
| 41 | +- When a single value is used, it is passed as the first argument to the setter method. |
| 42 | +- When an array of values is used, they are destructured as passed as arguments in order to the setter method. |
| 43 | +- When an associative array is used, the key/value pairs are destructured and passed in as named arguments to the setter method. |
| 44 | + |
| 45 | +```php |
| 46 | +// Create Options object with array of values |
| 47 | +$options = new Options([ |
| 48 | + 'width' => 300, |
| 49 | + 'height' => 400, |
| 50 | + 'resize' => [ |
| 51 | + 'width' => 300, |
| 52 | + 'height' => 400, |
| 53 | + 'enlarge' => false, |
| 54 | + 'resize_type' => 'fill', |
| 55 | + ], |
| 56 | + 'gravity' => 'sm', |
| 57 | + 'png_options' => [ |
| 58 | + 'interlaced' => true, |
| 59 | + 'quantize' => false, |
| 60 | + ], |
| 61 | +]); |
| 62 | +``` |
| 63 | + |
| 64 | +### Building URLs |
| 65 | + |
| 66 | +```php |
| 67 | +use smallpics\smallpics\Options; |
| 68 | +use smallpics\smallpics\UrlBuilder; |
| 69 | + |
| 70 | +// Create options object |
| 71 | +$options = new Options(); |
| 72 | +$options |
| 73 | + ->setQuality(80) |
| 74 | + ->setFormat('png'); |
| 75 | + |
| 76 | +// Create URL builder (without signing, with plain URLs) |
| 77 | +$builder = new UrlBuilder('https://images.my-origin.com', null, null, false); |
| 78 | + |
| 79 | +// Build URL |
| 80 | +$imageUrl = 'https://example.com/images/image.jpg'; |
| 81 | +$url = $builder->buildUrl($imageUrl, $options); |
| 82 | + |
| 83 | +echo $url; |
| 84 | +// Output: https://images.my-origin.com/unsafe/preset:sharp/resize:fill:300:400:0/gravity:sm/quality:80/format:png/plain/https://example.com/images/image.jpg |
| 85 | +``` |
| 86 | + |
| 87 | +### Encoded URLs |
| 88 | + |
| 89 | +```php |
| 90 | +use smallpics\smallpics\Options; |
| 91 | +use smallpics\smallpics\UrlBuilder; |
| 92 | + |
| 93 | +// Create options object |
| 94 | +$options = new Options(); |
| 95 | +$options |
| 96 | + ->setWidth(300) |
| 97 | + ->setHeight(400); |
| 98 | + |
| 99 | +// Create URL builder |
| 100 | +$builder = new UrlBuilder('https://images.my-origin.com'); |
| 101 | + |
| 102 | +// Build URL with encoded source |
| 103 | +$imageUrl = 'images/image.jpg'; |
| 104 | +$url = $builder->buildUrl($imageUrl, $options); |
| 105 | + |
| 106 | +echo $url; |
| 107 | +// Output: https://images.my-origin.com/images/image.jpg?width=300&height=400 |
| 108 | +``` |
| 109 | + |
| 110 | +### Signed URLs |
| 111 | + |
| 112 | +```php |
| 113 | +// Create URL builder with signing keys |
| 114 | +$key = '0123456789abcdef0123456789abcdef'; |
| 115 | +$salt = 'fedcba9876543210fedcba9876543210'; |
| 116 | +$builder = new UrlBuilder('https://images.my-origin.com', $key, $salt); |
| 117 | + |
| 118 | +// Build signed URL |
| 119 | +$imageUrl = 'images/image.jpg'; |
| 120 | +$url = $builder->buildUrl($imageUrl, $options); |
| 121 | + |
| 122 | +echo $url; |
| 123 | +// Output will include a signature: https://images.my-origin.com/images/image.jpg?width=300&height=400&signature=[signature] |
| 124 | +``` |
| 125 | + |
| 126 | +## Development |
| 127 | + |
| 128 | +### Testing |
| 129 | + |
| 130 | +This package uses [Pest PHP](https://pestphp.com/) for testing. To run the tests: |
| 131 | + |
| 132 | +```bash |
| 133 | +composer test |
| 134 | +``` |
| 135 | + |
| 136 | +or |
| 137 | + |
| 138 | +```bash |
| 139 | +vendor/bin/pest |
| 140 | +``` |
0 commit comments