Skip to content

Commit 93cefcc

Browse files
committed
Hello
0 parents  commit 93cefcc

13 files changed

+486
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.lock
2+
vendor/

README.md

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

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "imageoptim/imageoptim",
3+
"description": "ImageOptim API for PHP",
4+
"license": "BSD-2-Clause",
5+
"authors": [
6+
{
7+
"name": "Kornel",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"homepage": "https://imageoptim.com/api",
12+
"keywords": ["image","resize","optimize","scale","performance"],
13+
"autoload": {
14+
"psr-4" : {
15+
"ImageOptim\\" : "src"
16+
}
17+
},
18+
"require": {
19+
"php" : "^5.4 || ^7.0"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "^5.3"
23+
}
24+
}

phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="phpunit.xsd"
4+
backupGlobals="false"
5+
bootstrap="vendor/autoload.php"
6+
verbose="true">
7+
<testsuites>
8+
<testsuite name="imageoptim">
9+
<directory suffix="Test.php">test</directory>
10+
</testsuite>
11+
</testsuites>
12+
</phpunit>
13+

src/API.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace ImageOptim;
4+
5+
class API {
6+
private $username;
7+
8+
function __construct($username) {
9+
if (empty($username) || !is_string($username)) {
10+
throw new InvalidArgumentException("First argument to ImageOptim\\API must be the username\nGet your username from https://im2.io/register\n");
11+
}
12+
$this->username = $username;
13+
}
14+
15+
function imageFromURL($url) {
16+
return new Request($this->username, $url);
17+
}
18+
}

src/APIException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace ImageOptim;
4+
5+
class APIException extends \RuntimeException {
6+
7+
}

src/AccessDeniedException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace ImageOptim;
4+
5+
class AccessDeniedException extends \RuntimeException {
6+
7+
}

src/InvalidArgumentException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace ImageOptim;
4+
5+
class InvalidArgumentException extends \InvalidArgumentException {
6+
7+
}

src/NetworkException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace ImageOptim;
4+
5+
class NetworkException extends \RuntimeException {
6+
7+
}

src/NotFoundException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace ImageOptim;
4+
5+
class NotFoundException extends \RuntimeException {
6+
7+
}

0 commit comments

Comments
 (0)