1# Slika - simple image handling for PHP
2
3This is a library that covers only the bare basics you need when handling images:
4
5  * resizing
6  * cropping
7  * rotation
8
9It can use either PHP's libGD or a locally installed ImageMagick binary.
10
11## Installation
12
13Use composer
14
15    composer require splitbrain/slika
16
17## Usage
18
19Simply get an Adapter from the Slika factory, run some operations on it and call `save`.
20
21Operations can be chained together. Consider the chain to be one command. Do not reuse the adapter returned by `run()`, it is a single use object. All operations can potentially throw a `\splitbrain\slika\Exception`.
22
23Options (see below) can be passed as a second parameter to the `run` factory.
24
25```php
26use \splitbrain\slika\Slika;
27use \splitbrain\slika\Exception;
28
29$options = [
30    'quality' => 75
31];
32
33try {
34    Slika::run('input.png', $options)
35        ->resize(500,500)
36        ->rotate(Slika::ROTATE_CCW
37        ->save('output.jpg', 'jpg');
38} catch (Exception $e) {
39    // conversion went wrong, handle it
40}
41```
42
43Please also check the [API Docs](https://splitbrain.github.io/slika/) for details.
44
45## Operations
46
47### resize
48
49All resize operations will keep the original aspect ratio of the image. There will be no distortion.
50
51Keeping either width or height at zero will auto calculate the value for you.
52
53```php
54# fit the image into a bounding box of 500x500 pixels
55Slika::run('input.jpg')->resize(500,500)->save('output.png', 'png');
56
57# adjust the image to a maximum width of 500 pixels
58Slika::run('input.jpg')->resize(500,0)->save('output.png', 'png');
59
60# adjust the image to a maximum height of 500 pixels
61Slika::run('input.jpg')->resize(0,500)->save('output.png', 'png');
62```
63
64### crop
65
66Similar to resizing, but this time the image will be cropped to fit the new aspect ratio.
67
68```php
69Slika::run('input.jpg')->crop(500,500)->save('output.png', 'png');
70```
71
72### rotate
73
74Rotates the image. The parameter passed is one of the EXIF orientation flags:
75
76![orientation flags](https://i.stack.imgur.com/BFqgu.gif)
77
78For your convenience there are three Constants defined:
79
80
81* `Slika::ROTATE_CCW` counter clockwise rotation
82* `Slika::ROTATE_CW` clockwise rotation
83* `Slika::ROTATE_TOPDOWN` full 180 degree rotation
84
85```php
86Slika::run('input.jpg')->rotate(Slika::ROTATE_CW)->save('output.png', 'png');
87```
88
89### autorotate
90
91Rotates the image according to the EXIF rotation tag if found.
92
93```php
94Slika::run('input.jpg')->autorotate()->save('output.png', 'png');
95```
96
97## Options
98
99Options can be passed as associatiave array as the second parameter in `Slika::run`.
100
101The following options are availble currently:
102
103| Option      | Default            | Description                                |
104|-------------|--------------------|--------------------------------------------|
105| `imconvert` | `/usr/bin/convert` | The path to ImageMagick's `convert` binary |
106| `quality`   | `92`               | The quality when writing JPEG images       |
107