1<?php
2
3
4namespace splitbrain\slika;
5
6/**
7 * Base class for image adapters. Defines what image processing adapters need to provide
8 */
9abstract class Adapter
10{
11    /** @var string path to the image */
12    protected $imagepath;
13
14    /** @var array Adapter Options */
15    protected $options;
16
17    /**
18     * New Slika Adapter
19     *
20     * @param string $imagepath path to the original image
21     * @param array $options set options
22     * @throws Exception
23     */
24    public function __construct($imagepath, $options = [])
25    {
26        if (!file_exists($imagepath)) {
27            throw new Exception('image file does not exist');
28        }
29
30        if (!is_readable($imagepath)) {
31            throw new Exception('image file is not readable');
32        }
33
34        $this->imagepath = $imagepath;
35        $this->options = array_merge(Slika::DEFAULT_OPTIONS, $options);
36    }
37
38    /**
39     * Rote the image based on the rotation exif tag
40     *
41     * @return Adapter
42     */
43    abstract public function autorotate();
44
45    /**
46     * Rotate and/or flip the image
47     *
48     * This expects an orientation flag as stored in EXIF data. For typical operations,
49     * Slika::ROTATE_* constants are defined.
50     *
51     * @param int $orientation Exif rotation flags
52     * @return Adapter
53     * @see https://stackoverflow.com/a/53697440 for info on the rotation constants
54     */
55    abstract public function rotate($orientation);
56
57    /**
58     * Resize to make image fit the given dimension (maintaining the aspect ratio)
59     *
60     * You may omit one of the dimensions to auto calculate it based on the aspect ratio
61     *
62     * @param int|string $width in pixels or %
63     * @param int|string $height in pixels or %
64     * @return Adapter
65     */
66    abstract public function resize($width, $height);
67
68
69    /**
70     * Resize to the given dimension, cropping the image as needed
71     *
72     * You may omit one of the dimensions to use a square area
73     *
74     * @param int|string $width in pixels or %
75     * @param int|string $height in pixels or %
76     * @return Adapter
77     */
78    abstract public function crop($width, $height);
79
80    /**
81     * Save the new file
82     *
83     * @param string $path
84     * @param string $extension The type of image to save, empty for original
85     * @return void
86     */
87    abstract public function save($path, $extension = '');
88
89}
90