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