|
| 1 | +# ImageOptim API PHP client |
| 2 | + |
| 3 | +This library allows you to resize and optimize images using ImageOptim API. |
| 4 | + |
| 5 | +ImageOptim offers [advanced compression, high-DPI/responsive image mode, and color profile support](https://imageoptim.com/features.html) that are much better than PHP's built-in image resizing functions. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +The easiest is to use [PHP Composer](https://getcomposer.org/): |
| 10 | + |
| 11 | +```sh |
| 12 | +composer require imageoptim/imageoptim |
| 13 | +``` |
| 14 | + |
| 15 | +If you don't use Composer, then `require` or autoload files from the `src` directory. |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +First, [register to use the API](https://im2.io/register). |
| 20 | + |
| 21 | +```php |
| 22 | +<?php |
| 23 | +require "vendor/autoload.php"; // created by Composer |
| 24 | + |
| 25 | +$api = new ImageOptim\API("🔶your api username goes here🔶"); |
| 26 | + |
| 27 | +$imageData = $api->fromURL('http://example.com/photo.jpg') // read this image |
| 28 | + ->resize(160, 100, 'crop') // optional: resize to a thumbnail |
| 29 | + ->dpr(2) // optional: double number of pixels for high-resolution "Retina" displays |
| 30 | + ->getBytes(); // perform these operations and return the image data as binary string |
| 31 | + |
| 32 | +file_put_contents("images/photo_optimized.jpg", $imageData); |
| 33 | +``` |
| 34 | + |
| 35 | +### Methods |
| 36 | + |
| 37 | +#### `API($username)` constructor |
| 38 | + |
| 39 | + new ImageOptim\API("your api username goes here"); |
| 40 | + |
| 41 | +Creates new instance of the API. You need to give it [your username](https://im2.io/api/username). |
| 42 | + |
| 43 | +#### `fromURL($url)` — source image |
| 44 | + |
| 45 | +Creates new request that will read image from the given URL, and then resize and optimize it. |
| 46 | + |
| 47 | +Please pass full absolute URL to images on your website. |
| 48 | + |
| 49 | +Ideally you should supply source image at very high quality (e.g. JPEG saved at 99%), so that ImageOptim can adjust quality itself. If source images you provide are already saved at low quality, ImageOptim will not be able to make them look better. |
| 50 | + |
| 51 | +#### `resize($width, $height = optional, $fit = optional)` — desired dimensions |
| 52 | + |
| 53 | +* `resize($width)` — sets maximum width for the image, so it'll be resized to this width. If the image is smaller than this, it won't be enlarged. |
| 54 | + |
| 55 | +* `resize($width, $height)` — same as above, but image will also have height same or smaller. Aspect ratio is always preserved. |
| 56 | + |
| 57 | +* `resize($width, $height, 'crop')` — resizes and crops image exactly to these dimensions. |
| 58 | + |
| 59 | +If you don't call `resize()`, then the original image size will be preserved. |
| 60 | + |
| 61 | +[See options reference](https://im2.io/api/post#options) for more resizing options. |
| 62 | + |
| 63 | +#### `dpr($x)` — pixel doubling for responsive images (HTML `srcset`) |
| 64 | + |
| 65 | +The default is `dpr(1)`, which means image is for regular displays, and `resize()` does the obvious thing you'd expect. |
| 66 | + |
| 67 | +If you set `dpr(2)` then pixel width and height of the image will be *doubled* to match density of "2x" displays. This is better than `resize($width*2)`, because it also adjusts sharpness and image quality to be optimal for high-DPI displays. |
| 68 | + |
| 69 | +[See options reference](https://im2.io/api/post#opt-2x) for explanation how DPR works. |
| 70 | + |
| 71 | +#### `quality($preset)` — if you need even smaller or extra sharp images |
| 72 | + |
| 73 | +Quality is set as a string, and can be `low`, `medium` or `high`. The default is `medium` and should be good enough for most cases. |
| 74 | + |
| 75 | +#### `getBytes()` — get the resized image |
| 76 | + |
| 77 | +Makes request to ImageOptim API and returns optimized image as a string. You should save that to your server's disk. |
| 78 | + |
| 79 | +ImageOptim performs optimizations that sometimes may take a few seconds, so instead of converting images on the fly on every request, you should convert them once and keep them. |
| 80 | + |
| 81 | +#### `apiURL()` — debug or use another HTTPS client |
| 82 | + |
| 83 | +Returns string with URL to `https://im2.io/…` that is equivalent of the options set. You can open this URL in your web browser to get more information about it. Or you can [make a `POST` request to it](https://im2.io/api/post#making-the-request) to download the image yourself, if you don't want to use the `getBytes()` method. |
| 84 | + |
| 85 | +### Error handling |
| 86 | + |
| 87 | +All methods throw on error. You can expect the following exception subclasses: |
| 88 | + |
| 89 | +* `ImageOptim\InvalidArgumentException` means arguments to functions are incorrect and you need to fix your code. |
| 90 | +* `ImageOptim\NetworkException` is thrown when there is problem comunicating with the API. You can retry the request. |
| 91 | +* `ImageOptim\NotFoundException` is thrown when URL given to `fromURL()` returns 404. Make sure paths and urlencoding are correct. [More](https://im2.io/api/post#response). |
| 92 | + |
| 93 | +### Help and info |
| 94 | + |
| 95 | +See [imageoptim.com/api](https://imageoptim.com/api) for documentation and contact info. I'm happy to help! |
0 commit comments