Skip to content

Commit 0b4370a

Browse files
committed
Expanded README
1 parent 3e389fa commit 0b4370a

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

README.md

+51
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ $imageData = $api->imageFromURL('http://example.com/photo.jpg') // read this ima
3232
file_put_contents("images/photo_optimized.jpg", $imageData);
3333
```
3434

35+
There's a longer example at the end of the readme.
36+
3537
### Methods
3638

3739
#### `API($username)` constructor
@@ -97,6 +99,55 @@ All methods throw on error. You can expect the following exception subclasses:
9799
* `ImageOptim\NotFoundException` is thrown when URL given to `imageFromURL()` returned 404. Make sure paths and urlencoding are correct. [More](https://im2.io/api/post#response).
98100
* `ImageOptim\OriginServerException` is thrown when URL given to `imageFromURL()` returned 4xx or 5xx error. Make sure your server allows access to the file.
99101

102+
If you're writing a script that processes a large number of images in one go, don't launch it from a web browser, as it will likely time out. It's best to launch such scripts via CLI (e.g. via SSH).
103+
100104
### Help and info
101105

102106
See [imageoptim.com/api](https://imageoptim.com/api) for documentation and contact info. I'm happy to help!
107+
108+
### Example
109+
110+
This is a script that optimizes an image. Such script usually would be ran when a new image is uploaded to the server. You don't need to run any PHP code to *serve* optimized images.
111+
112+
The API operates on a single image at a time. When you want to generate multiple image sizes/thumbnails, repeat the whole procedure for each image at each size.
113+
114+
```php
115+
<?php
116+
117+
// This line is required once to set up Composer
118+
// If this file can't be found, try changing the path
119+
// and or run `composer update` in your project's directory
120+
require "vendor/autoload.php";
121+
122+
$api = new ImageOptim\API("🔶your api username goes here🔶");
123+
124+
// imageFromURL/imageFromPath creates a temporary object used to store
125+
// settings of the optimization.
126+
$imageParams = $api->imageFromURL('http://example.com/photo.jpg');
127+
128+
// You set various settings on this object (or none to get the defaults).
129+
$imageParams->quality('low');
130+
$imageParams->resize(1024);
131+
132+
// Next, to start the optimizations and get the optimized image, call:
133+
$imageData = $imageParams->getBytes();
134+
135+
/*
136+
the getBytes() call may take a while to run, so it's intended to be
137+
called only once per image (e.g. only when a new image is uploaded
138+
to your server). If you'd like to "lazily" optimize arbitrary images
139+
on-the-fly when they're requested, there is a better API for that:
140+
https://im2.io/api/get
141+
*/
142+
143+
// Save the image data somewhere on the server, e.g.
144+
file_put_contents("images/photo_optimized.jpg", $imageData);
145+
146+
// Note that this script only prepares a static image file
147+
// (in this example in images/photo_optimized.jpg),
148+
// and does not serve it to the browser. Once the optimized
149+
// image is saved to disk you should serve it normally
150+
// as you'd do with any regular image file.
151+
152+
```
153+

0 commit comments

Comments
 (0)