xref: /dokuwiki/vendor/splitbrain/slika/src/Adapter.php (revision 18cdf1023bb5e614d1cc65cda5973b9bc352388c)
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     * @param bool $upscale when false, an image smaller than the target is kept at its original size
65     * @return Adapter
66     */
67    abstract public function resize($width, $height, $upscale = true);
68
69
70    /**
71     * Resize to the given dimension, cropping the image as needed
72     *
73     * You may omit one of the dimensions to use a square area
74     *
75     * @param int $width in pixels
76     * @param int $height in pixels
77     * @param bool $upscale when false, an image smaller than the target area is cropped but never enlarged
78     * @return Adapter
79     */
80    abstract public function crop($width, $height, $upscale = true);
81
82    /**
83     * Save the new file
84     *
85     * @param string $path
86     * @param string $extension The type of image to save, empty for original
87     * @return void
88     */
89    abstract public function save($path, $extension = '');
90
91}
92