1 <?php
2 namespace Elboletaire\Watimage;
3
4 use Elboletaire\Watimage\Exception\InvalidArgumentException;
5
6 /**
7 * This is a backwards compatibility class. It just has the old Watimage methods
8 * and workflow so you can upgrade any project to the new Watimage without
9 * changing your code.
10 *
11 * @author Òscar Casajuana Alonso <elboletaire@underave.net>
12 * @copyright 2015 Òscar Casajuana <elboletaire at underave dot net>
13 * @link https://github.com/elboletaire/Watimage
14 * @license https://opensource.org/licenses/MIT MIT
15 */
16 class Watimage
17 {
18 /**
19 * Image handler.
20 *
21 * @var Image
22 */
23 protected $image;
24
25 /**
26 * Watermark handler.
27 *
28 * @var Watermark False if no watermark set, Watermark otherwise.
29 */
30 protected $watermark = false;
31
32 /**
33 * Any error returned by the class will be stored here
34 * @public array $errors
35 */
36 public $errors = [];
37
38 /**
39 * Construct method. Accepts file and watermark as parameters so you
40 * can avoid the setImage and setWatermark methods.
41 *
42 * @param mixed $file Image details if is an array, image file
43 * location otherwise
44 * @param mixed $watermark Watermark details if is an array, watermark file
45 * location otherwise.
46 */
47 public function __construct($file = null, $watermark = null)
48 {
49 $this->image = new Image($file);
50
51 if (!is_null($watermark)) {
52 $this->watermark = new Watermark();
53 // This setWatermark method is backwards compatible!
54 $this->setWatermark($watermark);
55 }
56 }
57
58 /**
59 * Sets image and (optionally) its options
60 *
61 * @param mixed $filename Filename string or array containing both filename and quality
62 * @return Watimage
63 * @throws Exception
64 */
65 public function setImage($filename)
66 {
67 try {
68 $this->image->load($filename);
69
70 return true;
71 } catch (Exception $e) {
72 array_push($this->errors, $e->getMessage());
73
74 return false;
75 }
76 }
77
78 /**
79 * Sets quality for gif and jpg files.
80 *
81 * @param int $quality A value from 0 (zero quality) to 100 (max quality).
82 */
83 public function setQuality($quality)
84 {
85 try {
86 $this->image->setQuality($quality);
87
88 return true;
89 } catch (Exception $e) {
90 array_push($this->errors, $e->getMessage());
91
92 return false;
93 }
94 }
95
96 /**
97 * Sets compression for png files.
98 *
99 * @param int $compression A value from 0 (no compression, not recommended) to 9.
100 */
101 public function setCompression($compression)
102 {
103 try {
104 $this->image->setQuality($compression);
105
106 return true;
107 } catch (Exception $e) {
108 array_push($this->errors, $e->getMessage());
109
110 return false;
111 }
112 }
113
114 /**
115 * Set watermark and (optionally) its options.
116 *
117 * @param mixed $options You can set the watermark without options or you can
118 * set an array with any of these $options = [
119 * 'file' => 'watermark.png',
120 * 'position' => 'bottom right', // default
121 * 'margin' => ['20', '10'] // 0 by default,
122 * 'size' => 'full' // 100% by default
123 * ];
124 * @return true on success; false on failure
125 */
126 public function setWatermark($options = [])
127 {
128 try {
129 if (!$this->watermark) {
130 $this->watermark = new Watermark();
131 }
132
133 if (!is_array($options)) {
134 $this->watermark->load($options);
135
136 return true;
137 }
138
139 if (!isset($options['file'])) {
140 throw new InvalidArgumentException("Watermark \"file\" param not specified");
141 }
142
143 $this->watermark->load($options['file']);
144
145 foreach (['position', 'margin', 'size'] as $option) {
146 if (!array_key_exists($option, $options)) {
147 continue;
148 }
149
150 $method = 'set' . ucfirst($option);
151 $this->watermark->$method($options[$option]);
152 }
153
154 return true;
155 } catch (Exception $e) {
156 array_push($this->errors, $e->getMessage());
157
158 return false;
159 }
160 }
161
162 /**
163 * Resizes the image.
164 *
165 * @param array $options = [
166 * 'type' => 'resizemin|resizecrop|resize|crop|reduce',
167 * 'size' => ['x' => 2000, 'y' => 500]
168 * ]
169 * You can also set the size without specifying x and y:
170 * [2000, 500]. Or directly 'size' => 2000 (takes 2000x2000)
171 * @return bool true on success; otherwise false
172 */
173 public function resize($options = [])
174 {
175 try {
176 $this->image->resize($options['type'], $options['size']);
177
178 return true;
179 } catch (Exception $e) {
180 array_push($this->errors, $e->getMessage());
181
182 return false;
183 }
184 }
185
186 /**
187 * Crops an image based on specified coords and size.
188 *
189 * @param mixed $options Specifying x & y position and width & height, like
190 * so [
191 * 'x' => 23,
192 * 'y' => 23,
193 * 'width' => 230,
194 * 'height' => 230
195 * ]
196 * @return bool success
197 */
198 public function crop($options = [])
199 {
200 try {
201 $this->image->crop($options);
202
203 return true;
204 } catch (Exception $e) {
205 array_push($this->errors, $e->getMessage());
206
207 return false;
208 }
209 }
210
211 /**
212 * Rotates an image.
213 *
214 * @param mixed $options Can either be an integer with the degrees or an array with
215 * keys `bgcolor` for the rotation bgcolor and `degrees` for
216 * the angle.
217 * @return bool
218 */
219 public function rotateImage($options = [])
220 {
221 try {
222 if (is_array($options)) {
223 if (empty($options['bgcolor'])) {
224 $options['bgcolor'] = -1;
225 }
226
227 $this->image->rotate($options['degrees'], $options['bgcolor']);
228 return true;
229 }
230
231 $this->image->rotate($options);
232 return true;
233 } catch (Exception $e) {
234 array_push($this->errors, $e->getMessage());
235
236 return false;
237 }
238 }
239
240 /**
241 * rotateImage alias.
242 *
243 * @see self::rotateImage()
244 */
245 public function rotate($options = [])
246 {
247 return $this->rotateImage($options);
248 }
249
250 /**
251 * Applies a watermark to the image. Needs to be initialized with $this->setWatermark()
252 *
253 * @return true on success, otherwise false
254 */
255 public function applyWatermark()
256 {
257 try {
258 $this->watermark->apply($this->image);
259
260 return true;
261 } catch (Exception $e) {
262 array_push($this->errors, $e->getMessage());
263
264 return false;
265 }
266 }
267
268 /**
269 * Flips an image.
270 *
271 * @param string $type type of flip: horizontal / vertical / both
272 * @return true on success. Otherwise false
273 */
274 public function flip($type = 'horizontal')
275 {
276 try {
277 $this->image->flip($type);
278
279 return true;
280 } catch (Exception $e) {
281 array_push($this->errors, $e->getMessage());
282
283 return false;
284 }
285 }
286
287 /**
288 * Generates the image file.
289 *
290 * @param string $path if not specified image will be printed on screen
291 * @param string $output mime type for output image (image/png, image/gif, image/jpeg)
292 * @return true on success. Otherwise false
293 */
294 public function generate($path = null, $output = null)
295 {
296 try {
297 $this->image->generate($path, $output);
298
299 return true;
300 } catch (Exception $e) {
301 array_push($this->errors, $e->getMessage());
302
303 return false;
304 }
305 }
306 }
307