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 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## Inspecting images without processing them 98 99Sometimes you need to know what dimensions an image *would* have after a chain of operations without actually decoding pixels or calling ImageMagick — for example, to emit `<img width="…" height="…">` attributes on a page that references a resize URL. 100 101`\splitbrain\slika\ImageInfo` mirrors the Adapter's fluent API at the dimension level. It reads only `getimagesize()` and the EXIF orientation tag, never touching the pixel data. 102 103```php 104use \splitbrain\slika\ImageInfo; 105 106$info = new ImageInfo('input.jpg'); 107 108// on-disk state (stable regardless of chain operations) 109$info->getRawWidth(); // e.g. 4000 110$info->getRawHeight(); // e.g. 3000 111$info->getExtension(); // 'jpeg' 112$info->getOrientation(); // EXIF orientation 1..8 113 114// the fluent chain simulates autorotate/rotate/resize/crop 115// and returns the final tracked dimensions 116list($w, $h) = (new ImageInfo('input.jpg')) 117 ->autorotate() 118 ->resize(500, 500) 119 ->getDimensions(); 120``` 121 122This lets you predict the output of `Slika::run(...)->autorotate()->resize(500,500)` 123without doing any actual image work. 124 125## Options 126 127Options can be passed as associatiave array as the second parameter in `Slika::run`. 128 129The following options are availble currently: 130 131| Option | Default | Description | 132|-------------|--------------------|--------------------------------------------| 133| `imconvert` | `/usr/bin/convert` | The path to ImageMagick's `convert` binary | 134| `quality` | `92` | The quality when writing JPEG images | 135