blob: b9adcd6e5800901f7f50462695b67b12db00f7a8 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Eric Barnesb1673362011-12-05 22:05:38 -05008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Eric Barnesb1673362011-12-05 22:05:38 -050010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Image Manipulation class
42 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Image_lib
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/libraries/image_lib.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49class CI_Image_lib {
50
Timothy Warrenb8e62852012-04-26 18:40:54 -040051 /**
52 * PHP extension/library to use for image manipulation
Andrey Andreev56454792012-05-17 14:32:19 +030053 * Can be: imagemagick, netpbm, gd, gd2
Timothy Warrenb8e62852012-04-26 18:40:54 -040054 *
55 * @var string
56 */
57 public $image_library = 'gd2';
Andrey Andreev56454792012-05-17 14:32:19 +030058
Timothy Warrenb8e62852012-04-26 18:40:54 -040059 /**
60 * Path to the graphic library (if applicable)
61 *
62 * @var string
63 */
Andrey Andreev3a459572011-12-21 11:23:11 +020064 public $library_path = '';
Andrey Andreev56454792012-05-17 14:32:19 +030065
Timothy Warrenb8e62852012-04-26 18:40:54 -040066 /**
67 * Whether to send to browser or write to disk
68 *
69 * @var bool
70 */
71 public $dynamic_output = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +030072
Timothy Warrenb8e62852012-04-26 18:40:54 -040073 /**
74 * Path to original image
75 *
76 * @var string
77 */
Andrey Andreev3a459572011-12-21 11:23:11 +020078 public $source_image = '';
Andrey Andreev56454792012-05-17 14:32:19 +030079
Timothy Warrenb8e62852012-04-26 18:40:54 -040080 /**
81 * Path to the modified image
82 *
83 * @var string
84 */
Andrey Andreev56454792012-05-17 14:32:19 +030085 public $new_image = '';
86
Timothy Warrenb8e62852012-04-26 18:40:54 -040087 /**
88 * Image width
89 *
90 * @var int
91 */
Andrey Andreev56454792012-05-17 14:32:19 +030092 public $width = '';
93
Timothy Warrenb8e62852012-04-26 18:40:54 -040094 /**
95 * Image height
96 *
97 * @var int
98 */
Andrey Andreev56454792012-05-17 14:32:19 +030099 public $height = '';
100
Timothy Warrenb8e62852012-04-26 18:40:54 -0400101 /**
102 * Quality percentage of new image
103 *
104 * @var int
105 */
Andrey Andreev56454792012-05-17 14:32:19 +0300106 public $quality = 90;
107
Timothy Warrenb8e62852012-04-26 18:40:54 -0400108 /**
109 * Whether to create a thumbnail
110 *
111 * @var bool
112 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200113 public $create_thumb = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300114
Timothy Warrenb8e62852012-04-26 18:40:54 -0400115 /**
116 * String to add to thumbnail version of image
117 *
118 * @var string
119 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200120 public $thumb_marker = '_thumb';
Andrey Andreev56454792012-05-17 14:32:19 +0300121
Timothy Warrenb8e62852012-04-26 18:40:54 -0400122 /**
123 * Whether to maintain aspect ratio when resizing or use hard values
124 *
125 * @var bool
126 */
127 public $maintain_ratio = TRUE;
Andrey Andreev56454792012-05-17 14:32:19 +0300128
Timothy Warrenb8e62852012-04-26 18:40:54 -0400129 /**
130 * auto, height, or width. Determines what to use as the master dimension
131 *
132 * @var string
133 */
Andrey Andreev56454792012-05-17 14:32:19 +0300134 public $master_dim = 'auto';
135
Timothy Warrenb8e62852012-04-26 18:40:54 -0400136 /**
137 * Angle at to rotate image
138 *
139 * @var string
140 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200141 public $rotation_angle = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300142
Timothy Warrenb8e62852012-04-26 18:40:54 -0400143 /**
144 * X Coordinate for manipulation of the current image
145 *
146 * @var int
147 */
Andrey Andreev56454792012-05-17 14:32:19 +0300148 public $x_axis = '';
149
Timothy Warrenb8e62852012-04-26 18:40:54 -0400150 /**
151 * Y Coordinate for manipulation of the current image
152 *
153 * @var int
154 */
Andrey Andreev56454792012-05-17 14:32:19 +0300155 public $y_axis = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000156
Timothy Warrenb8e62852012-04-26 18:40:54 -0400157 // --------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 // Watermark Vars
Timothy Warrenb8e62852012-04-26 18:40:54 -0400159 // --------------------------------------------------------------------------
Andrey Andreev56454792012-05-17 14:32:19 +0300160
Timothy Warrenb8e62852012-04-26 18:40:54 -0400161 /**
162 * Watermark text if graphic is not used
163 *
164 * @var string
165 */
166 public $wm_text = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300167
Timothy Warrenb8e62852012-04-26 18:40:54 -0400168 /**
169 * Type of watermarking. Options: text/overlay
170 *
171 * @var string
172 */
173 public $wm_type = 'text';
Andrey Andreev56454792012-05-17 14:32:19 +0300174
Timothy Warrenb8e62852012-04-26 18:40:54 -0400175 /**
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400176 * Default transparency for watermark
Andrey Andreev56454792012-05-17 14:32:19 +0300177 *
Timothy Warrenb8e62852012-04-26 18:40:54 -0400178 * @var int
179 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200180 public $wm_x_transp = 4;
Andrey Andreev56454792012-05-17 14:32:19 +0300181
Timothy Warrenb8e62852012-04-26 18:40:54 -0400182 /**
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400183 * Default transparency for watermark
184 *
Timothy Warrenb8e62852012-04-26 18:40:54 -0400185 * @var int
186 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200187 public $wm_y_transp = 4;
Andrey Andreev56454792012-05-17 14:32:19 +0300188
Timothy Warrenb8e62852012-04-26 18:40:54 -0400189 /**
190 * Watermark image path
Andrey Andreev56454792012-05-17 14:32:19 +0300191 *
Timothy Warrenb8e62852012-04-26 18:40:54 -0400192 * @var string
193 */
Andrey Andreev56454792012-05-17 14:32:19 +0300194 public $wm_overlay_path = '';
195
Timothy Warrenb8e62852012-04-26 18:40:54 -0400196 /**
197 * TT font
198 *
199 * @var string
200 */
201 public $wm_font_path = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300202
Timothy Warrenb8e62852012-04-26 18:40:54 -0400203 /**
204 * Font size (different versions of GD will either use points or pixels)
205 *
206 * @var int
207 */
208 public $wm_font_size = 17;
Andrey Andreev56454792012-05-17 14:32:19 +0300209
Timothy Warrenb8e62852012-04-26 18:40:54 -0400210 /**
211 * Vertical alignment: T M B
212 *
213 * @var string
214 */
215 public $wm_vrt_alignment = 'B';
Andrey Andreev56454792012-05-17 14:32:19 +0300216
Timothy Warrenb8e62852012-04-26 18:40:54 -0400217 /**
218 * Horizontal alignment: L R C
219 *
220 * @var string
221 */
222 public $wm_hor_alignment = 'C';
Andrey Andreev56454792012-05-17 14:32:19 +0300223
Timothy Warrenb8e62852012-04-26 18:40:54 -0400224 /**
225 * Padding around text
226 *
227 * @var int
228 */
229 public $wm_padding = 0;
Andrey Andreev56454792012-05-17 14:32:19 +0300230
Timothy Warrenb8e62852012-04-26 18:40:54 -0400231 /**
232 * Lets you push text to the right
233 *
234 * @var int
235 */
236 public $wm_hor_offset = 0;
Andrey Andreev56454792012-05-17 14:32:19 +0300237
Timothy Warrenb8e62852012-04-26 18:40:54 -0400238 /**
239 * Lets you push text down
240 *
241 * @var int
242 */
243 public $wm_vrt_offset = 0;
Andrey Andreev56454792012-05-17 14:32:19 +0300244
Timothy Warrenb8e62852012-04-26 18:40:54 -0400245 /**
246 * Text color
247 *
248 * @var string
249 */
Andrey Andreev56454792012-05-17 14:32:19 +0300250 protected $wm_font_color = '#ffffff';
251
Timothy Warrenb8e62852012-04-26 18:40:54 -0400252 /**
253 * Dropshadow color
254 *
255 * @var string
256 */
Andrey Andreev56454792012-05-17 14:32:19 +0300257 protected $wm_shadow_color = '';
258
Timothy Warrenb8e62852012-04-26 18:40:54 -0400259 /**
260 * Dropshadow distance
261 *
262 * @var int
263 */
264 public $wm_shadow_distance = 2;
Andrey Andreev56454792012-05-17 14:32:19 +0300265
Timothy Warrenb8e62852012-04-26 18:40:54 -0400266 /**
267 * Image opacity: 1 - 100 Only works with image
268 *
269 * @var int
270 */
Andrey Andreev56454792012-05-17 14:32:19 +0300271 public $wm_opacity = 50;
Derek Allard2067d1a2008-11-13 22:59:24 +0000272
Timothy Warrenb8e62852012-04-26 18:40:54 -0400273 // --------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 // Private Vars
Timothy Warrenb8e62852012-04-26 18:40:54 -0400275 // --------------------------------------------------------------------------
Andrey Andreev56454792012-05-17 14:32:19 +0300276
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400277 /**
278 * Source image folder
279 *
280 * @var string
281 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200282 public $source_folder = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300283
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400284 /**
285 * Destination image folder
286 *
287 * @var string
288 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200289 public $dest_folder = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300290
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400291 /**
292 * Image mime-type
293 *
294 * @var string
295 */
Andrey Andreev56454792012-05-17 14:32:19 +0300296 public $mime_type = '';
297
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400298 /**
Andrey Andreev56454792012-05-17 14:32:19 +0300299 * Original image width
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400300 *
301 * @var int
302 */
Andrey Andreev56454792012-05-17 14:32:19 +0300303 public $orig_width = '';
304
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400305 /**
306 * Original image height
307 *
308 * @var int
309 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200310 public $orig_height = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300311
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400312 /**
313 * Image format
Andrey Andreev56454792012-05-17 14:32:19 +0300314 *
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400315 * @var string
316 */
Andrey Andreev56454792012-05-17 14:32:19 +0300317 public $image_type = '';
318
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400319 /**
320 * Size of current image
321 *
322 * @var string
323 */
Andrey Andreev56454792012-05-17 14:32:19 +0300324 public $size_str = '';
325
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400326 /**
327 * Full path to source image
328 *
329 * @var string
330 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200331 public $full_src_path = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300332
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400333 /**
334 * Full path to destination image
335 *
336 * @var string
337 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200338 public $full_dst_path = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300339
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400340 /**
Andrey Andreev45965742014-08-27 20:40:11 +0300341 * File permissions
342 *
343 * @var int
344 */
345 public $file_permissions = 0644;
346
347 /**
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400348 * Name of function to create image
349 *
350 * @var string
351 */
Andrey Andreev56454792012-05-17 14:32:19 +0300352 public $create_fnc = 'imagecreatetruecolor';
353
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400354 /**
355 * Name of function to copy image
356 *
357 * @var string
358 */
Andrey Andreev56454792012-05-17 14:32:19 +0300359 public $copy_fnc = 'imagecopyresampled';
360
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400361 /**
362 * Error messages
363 *
364 * @var array
365 */
Andrey Andreev56454792012-05-17 14:32:19 +0300366 public $error_msg = array();
367
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400368 /**
369 * Whether to have a drop shadow on watermark
370 *
371 * @var bool
372 */
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200373 protected $wm_use_drop_shadow = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300374
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400375 /**
376 * Whether to use truetype fonts
377 *
378 * @var bool
379 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200380 public $wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000381
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400382 /**
383 * Initialize Image Library
384 *
385 * @param array $props
Andrey Andreev56454792012-05-17 14:32:19 +0300386 * @return void
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400387 */
Greg Akera9263282010-11-10 15:26:43 -0600388 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 if (count($props) > 0)
391 {
392 $this->initialize($props);
393 }
394
Andrey Andreev8f9ab652017-01-10 12:30:38 +0200395 /**
396 * A work-around for some improperly formatted, but
397 * usable JPEGs; known to be produced by Samsung
398 * smartphones' front-facing cameras.
399 *
400 * @see https://github.com/bcit-ci/CodeIgniter/issues/4967
401 * @see https://bugs.php.net/bug.php?id=72404
402 */
403 ini_set('gd.jpeg_ignore_warning', 1);
404
Andrey Andreev90726b82015-01-20 12:39:22 +0200405 log_message('info', 'Image Lib Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
407
408 // --------------------------------------------------------------------
409
410 /**
411 * Initialize image properties
412 *
413 * Resets values in case this class is used in a loop
414 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 * @return void
416 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200417 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
Sam Doidge5cb5c0a2013-03-13 01:28:06 +0000419 $props = array('thumb_marker', 'library_path', 'source_image', 'new_image', 'width', 'height', 'rotation_angle', 'x_axis', 'y_axis', 'wm_text', 'wm_overlay_path', 'wm_font_path', 'wm_shadow_color', 'source_folder', 'dest_folder', 'mime_type', 'orig_width', 'orig_height', 'image_type', 'size_str', 'full_src_path', 'full_dst_path');
Derek Allard2067d1a2008-11-13 22:59:24 +0000420
421 foreach ($props as $val)
422 {
423 $this->$val = '';
424 }
425
Michael Denniscb07a322011-08-20 23:40:59 -0700426 $this->image_library = 'gd2';
427 $this->dynamic_output = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300428 $this->quality = 90;
Michael Denniscb07a322011-08-20 23:40:59 -0700429 $this->create_thumb = FALSE;
430 $this->thumb_marker = '_thumb';
431 $this->maintain_ratio = TRUE;
432 $this->master_dim = 'auto';
433 $this->wm_type = 'text';
434 $this->wm_x_transp = 4;
435 $this->wm_y_transp = 4;
436 $this->wm_font_size = 17;
437 $this->wm_vrt_alignment = 'B';
438 $this->wm_hor_alignment = 'C';
439 $this->wm_padding = 0;
440 $this->wm_hor_offset = 0;
441 $this->wm_vrt_offset = 0;
442 $this->wm_font_color = '#ffffff';
443 $this->wm_shadow_distance = 2;
444 $this->wm_opacity = 50;
445 $this->create_fnc = 'imagecreatetruecolor';
446 $this->copy_fnc = 'imagecopyresampled';
447 $this->error_msg = array();
448 $this->wm_use_drop_shadow = FALSE;
449 $this->wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
451
452 // --------------------------------------------------------------------
453
454 /**
455 * initialize image preferences
456 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * @param array
458 * @return bool
459 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200460 public function initialize($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200462 // Convert array elements into class variables
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 if (count($props) > 0)
464 {
465 foreach ($props as $key => $val)
466 {
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200467 if (property_exists($this, $key))
468 {
Andrey Andreeveac4adf2016-03-22 11:24:14 +0200469 if (in_array($key, array('wm_font_color', 'wm_shadow_color'), TRUE))
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200470 {
Andrey Andreev8323ae62011-12-31 18:39:10 +0200471 if (preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches))
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200472 {
Andrey Andreevcf2ba9e2012-01-01 21:57:13 +0200473 /* $matches[1] contains our hex color value, but it might be
Andrey Andreev8323ae62011-12-31 18:39:10 +0200474 * both in the full 6-length format or the shortened 3-length
475 * value.
Andrey Andreevbb96c8b2011-12-31 18:48:39 +0200476 * We'll later need the full version, so we keep it if it's
Andrey Andreev8323ae62011-12-31 18:39:10 +0200477 * already there and if not - we'll convert to it. We can
478 * access string characters by their index as in an array,
479 * so we'll do that and use concatenation to form the final
480 * value:
481 */
Andrey Andreev665af0c2011-12-30 14:39:29 +0200482 $val = (strlen($matches[1]) === 6)
483 ? '#'.$matches[1]
484 : '#'.$matches[1][0].$matches[1][0].$matches[1][1].$matches[1][1].$matches[1][2].$matches[1][2];
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200485 }
486 else
487 {
488 continue;
489 }
490 }
Andrey Andreeveac4adf2016-03-22 11:24:14 +0200491 elseif (in_array($key, array('width', 'height'), TRUE) && ! ctype_digit((string) $val))
492 {
493 continue;
494 }
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200495
496 $this->$key = $val;
497 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
499 }
500
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200501 // Is there a source image? If not, there's no reason to continue
Alex Bilbied261b1e2012-06-02 11:12:16 +0100502 if ($this->source_image === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
504 $this->set_error('imglib_source_image_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500505 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
507
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200508 /* Is getimagesize() available?
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 *
510 * We use it to determine the image properties (width/height).
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200511 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 * properties using ImageMagick and NetPBM
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 */
514 if ( ! function_exists('getimagesize'))
515 {
516 $this->set_error('imglib_gd_required_for_props');
517 return FALSE;
518 }
519
520 $this->image_library = strtolower($this->image_library);
521
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200522 /* Set the full server path
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 *
524 * The source image may or may not contain a path.
525 * Either way, we'll try use realpath to generate the
526 * full server path in order to more reliably read it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 */
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200528 if (($full_source_path = realpath($this->source_image)) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200530 $full_source_path = str_replace('\\', '/', $full_source_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
532 else
533 {
534 $full_source_path = $this->source_image;
535 }
536
537 $x = explode('/', $full_source_path);
538 $this->source_image = end($x);
539 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
540
541 // Set the Image Properties
542 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
543 {
Eric Barnesb1673362011-12-05 22:05:38 -0500544 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 }
546
547 /*
548 * Assign the "new" image name/path
549 *
550 * If the user has set a "new_image" name it means
551 * we are making a copy of the source image. If not
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200552 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 * set the destination filename and path accordingly.
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100555 if ($this->new_image === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 {
Andrey Andreev961271d2016-12-09 12:48:57 +0200557 $this->dest_image = $this->source_image;
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 $this->dest_folder = $this->source_folder;
559 }
Andrey Andreev961271d2016-12-09 12:48:57 +0200560 elseif (strpos($this->new_image, '/') === FALSE && strpos($this->new_image, '\\') === FALSE)
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200561 {
Andrey Andreev961271d2016-12-09 12:48:57 +0200562 $this->dest_image = $this->new_image;
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200563 $this->dest_folder = $this->source_folder;
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200564 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 else
566 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200567 // Is there a file name?
Andrey Andreevf2a613d2016-12-12 11:39:38 +0200568 if ( ! preg_match('#\.(jpg|jpeg|gif|png)$#i', $this->new_image))
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200569 {
Andrey Andreev961271d2016-12-09 12:48:57 +0200570 $this->dest_image = $this->source_image;
Andrey Andreevf2a613d2016-12-12 11:39:38 +0200571 $this->dest_folder = $this->new_image;
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200572 }
573 else
574 {
Andrey Andreevf2a613d2016-12-12 11:39:38 +0200575 $x = explode('/', str_replace('\\', '/', $this->new_image));
Andrey Andreev961271d2016-12-09 12:48:57 +0200576 $this->dest_image = end($x);
Andrey Andreevf2a613d2016-12-12 11:39:38 +0200577 $this->dest_folder = str_replace($this->dest_image, '', $this->new_image);
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 }
Andrey Andreevf2a613d2016-12-12 11:39:38 +0200579
580 $this->dest_folder = realpath($this->dest_folder).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 }
582
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200583 /* Compile the finalized filenames/paths
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 *
585 * We'll create two master strings containing the
586 * full server path to the source image and the
587 * full server path to the destination image.
588 * We'll also split the destination image name
589 * so we can insert the thumbnail marker if needed.
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100591 if ($this->create_thumb === FALSE OR $this->thumb_marker === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 $this->thumb_marker = '';
594 }
595
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200596 $xp = $this->explode_name($this->dest_image);
Derek Allard2067d1a2008-11-13 22:59:24 +0000597
598 $filename = $xp['name'];
599 $file_ext = $xp['ext'];
600
601 $this->full_src_path = $this->source_folder.$this->source_image;
602 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
603
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200604 /* Should we maintain image proportions?
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 *
606 * When creating thumbs or copies, the target width/height
607 * might not be in correct proportion with the source
Andrey Andreev8e70b792012-01-12 20:19:24 +0200608 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100610 if ($this->maintain_ratio === TRUE && ($this->width !== 0 OR $this->height !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
612 $this->image_reproportion();
613 }
614
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200615 /* Was a width and height specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 *
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200617 * If the destination width/height was not submitted we
618 * will use the values from the actual file
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100620 if ($this->width === '')
Andrey Andreev8e70b792012-01-12 20:19:24 +0200621 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 $this->width = $this->orig_width;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200623 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000624
Alex Bilbied261b1e2012-06-02 11:12:16 +0100625 if ($this->height === '')
Andrey Andreev8e70b792012-01-12 20:19:24 +0200626 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 $this->height = $this->orig_height;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200628 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000629
630 // Set the quality
Andrey Andreev8e70b792012-01-12 20:19:24 +0200631 $this->quality = trim(str_replace('%', '', $this->quality));
Derek Allard2067d1a2008-11-13 22:59:24 +0000632
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200633 if ($this->quality === '' OR $this->quality === 0 OR ! ctype_digit($this->quality))
Andrey Andreev8e70b792012-01-12 20:19:24 +0200634 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 $this->quality = 90;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200636 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000637
638 // Set the x/y coordinates
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300639 is_numeric($this->x_axis) OR $this->x_axis = 0;
640 is_numeric($this->y_axis) OR $this->y_axis = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000641
642 // Watermark-related Stuff...
Alex Bilbied261b1e2012-06-02 11:12:16 +0100643 if ($this->wm_overlay_path !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200645 $this->wm_overlay_path = str_replace('\\', '/', realpath($this->wm_overlay_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 }
647
Alex Bilbied261b1e2012-06-02 11:12:16 +0100648 if ($this->wm_shadow_color !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
650 $this->wm_use_drop_shadow = TRUE;
651 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100652 elseif ($this->wm_use_drop_shadow === TRUE && $this->wm_shadow_color === '')
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200653 {
654 $this->wm_use_drop_shadow = FALSE;
655 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000656
Alex Bilbied261b1e2012-06-02 11:12:16 +0100657 if ($this->wm_font_path !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
659 $this->wm_use_truetype = TRUE;
660 }
661
662 return TRUE;
663 }
664
665 // --------------------------------------------------------------------
666
667 /**
668 * Image Resize
669 *
670 * This is a wrapper function that chooses the proper
671 * resize function based on the protocol specified
672 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 * @return bool
674 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200675 public function resize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
Andrey Andreev1a9f52c2012-01-07 01:06:34 +0200677 $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 return $this->$protocol('resize');
679 }
680
681 // --------------------------------------------------------------------
682
683 /**
684 * Image Crop
685 *
686 * This is a wrapper function that chooses the proper
687 * cropping function based on the protocol specified
688 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 * @return bool
690 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200691 public function crop()
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
Andrey Andreev5a67e3d2012-01-07 01:21:09 +0200693 $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 return $this->$protocol('crop');
695 }
696
697 // --------------------------------------------------------------------
698
699 /**
700 * Image Rotate
701 *
702 * This is a wrapper function that chooses the proper
703 * rotation function based on the protocol specified
704 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 * @return bool
706 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200707 public function rotate()
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
709 // Allowed rotation values
710 $degs = array(90, 180, 270, 'vrt', 'hor');
711
Alex Bilbied261b1e2012-06-02 11:12:16 +0100712 if ($this->rotation_angle === '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
714 $this->set_error('imglib_rotation_angle_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500715 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 }
717
718 // Reassign the width and height
Alex Bilbied261b1e2012-06-02 11:12:16 +0100719 if ($this->rotation_angle === 90 OR $this->rotation_angle === 270)
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 {
721 $this->width = $this->orig_height;
722 $this->height = $this->orig_width;
723 }
724 else
725 {
726 $this->width = $this->orig_width;
727 $this->height = $this->orig_height;
728 }
729
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 // Choose resizing function
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200731 if ($this->image_library === 'imagemagick' OR $this->image_library === 'netpbm')
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 {
733 $protocol = 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 return $this->$protocol('rotate');
735 }
736
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200737 return ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
738 ? $this->image_mirror_gd()
739 : $this->image_rotate_gd();
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 }
741
742 // --------------------------------------------------------------------
743
744 /**
745 * Image Process Using GD/GD2
746 *
747 * This function will resize or crop
748 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 * @param string
750 * @return bool
751 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200752 public function image_process_gd($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 {
754 $v2_override = FALSE;
755
756 // If the target width/height match the source, AND if the new file name is not equal to the old file name
757 // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
Alex Bilbied261b1e2012-06-02 11:12:16 +0100758 if ($this->dynamic_output === FALSE && $this->orig_width === $this->width && $this->orig_height === $this->height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100760 if ($this->source_image !== $this->new_image && @copy($this->full_src_path, $this->full_dst_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 {
Andrey Andreev45965742014-08-27 20:40:11 +0300762 chmod($this->full_dst_path, $this->file_permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200764
765 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 }
767
768 // Let's set up our values based on the action
Alex Bilbied261b1e2012-06-02 11:12:16 +0100769 if ($action === 'crop')
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200771 // Reassign the source width/height if cropping
Derek Jones4b9c6292011-07-01 17:40:48 -0500772 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 $this->orig_height = $this->height;
774
775 // GD 2.0 has a cropping bug so we'll test for it
776 if ($this->gd_version() !== FALSE)
777 {
778 $gd_version = str_replace('0', '', $this->gd_version());
Andrey Andreevd4818b72014-10-23 17:15:32 +0300779 $v2_override = ($gd_version == 2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 }
781 }
782 else
783 {
784 // If resizing the x/y axis must be zero
785 $this->x_axis = 0;
786 $this->y_axis = 0;
787 }
788
Andrey Andreev10fb7d12015-08-03 10:05:29 +0300789 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 if ( ! ($src_img = $this->image_create_gd()))
791 {
792 return FALSE;
793 }
794
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200795 /* Create the image
796 *
797 * Old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
798 * it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
799 * below should that ever prove inaccurate.
800 *
Alex Bilbied261b1e2012-06-02 11:12:16 +0100801 * if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor') && $v2_override === FALSE)
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200802 */
Andrey Andreev8e70b792012-01-12 20:19:24 +0200803 if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 {
805 $create = 'imagecreatetruecolor';
806 $copy = 'imagecopyresampled';
807 }
808 else
809 {
810 $create = 'imagecreate';
811 $copy = 'imagecopyresized';
812 }
813
814 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500815
Alex Bilbied261b1e2012-06-02 11:12:16 +0100816 if ($this->image_type === 3) // png we can actually preserve transparency
Derek Jones595bfd12010-08-20 10:28:22 -0500817 {
818 imagealphablending($dst_img, FALSE);
819 imagesavealpha($dst_img, TRUE);
820 }
Barry Mienydd671972010-10-04 16:33:58 +0200821
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
823
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200824 // Show the image
Alex Bilbied261b1e2012-06-02 11:12:16 +0100825 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 {
827 $this->image_display_gd($dst_img);
828 }
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200829 elseif ( ! $this->image_save_gd($dst_img)) // Or save it
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200831 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 }
833
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200834 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 imagedestroy($dst_img);
836 imagedestroy($src_img);
837
Andrey Andreev45965742014-08-27 20:40:11 +0300838 chmod($this->full_dst_path, $this->file_permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000839
840 return TRUE;
841 }
842
843 // --------------------------------------------------------------------
844
845 /**
846 * Image Process Using ImageMagick
847 *
848 * This function will resize, crop or rotate
849 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 * @param string
851 * @return bool
852 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200853 public function image_process_imagemagick($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 {
Andrey Andreev3a197592015-05-21 01:05:06 +0300855 // Do we have a vaild library path?
Alex Bilbied261b1e2012-06-02 11:12:16 +0100856 if ($this->library_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 {
858 $this->set_error('imglib_libpath_invalid');
859 return FALSE;
860 }
861
Andrey Andreev8e70b792012-01-12 20:19:24 +0200862 if ( ! preg_match('/convert$/i', $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200864 $this->library_path = rtrim($this->library_path, '/').'/convert';
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 }
866
867 // Execute the command
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200868 $cmd = $this->library_path.' -quality '.$this->quality;
Derek Allard2067d1a2008-11-13 22:59:24 +0000869
Alex Bilbied261b1e2012-06-02 11:12:16 +0100870 if ($action === 'crop')
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 {
Andrey Andreevb3f69342016-03-22 11:31:58 +0200872 $cmd .= ' -crop '.$this->width.'x'.$this->height.'+'.$this->x_axis.'+'.$this->y_axis;
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100874 elseif ($action === 'rotate')
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 {
Andrey Andreevb3f69342016-03-22 11:31:58 +0200876 $cmd .= ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
877 ? ' -flop'
878 : ' -rotate '.$this->rotation_angle;
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 }
Andrey Andreev8e70b792012-01-12 20:19:24 +0200880 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 {
Omar24063af2012-07-02 13:50:17 -0300882 if($this->maintain_ratio === TRUE)
883 {
Andrey Andreevb3f69342016-03-22 11:31:58 +0200884 $cmd .= ' -resize '.$this->width.'x'.$this->height;
Omar24063af2012-07-02 13:50:17 -0300885 }
Omarbb531d62012-06-29 10:48:28 -0300886 else
Omar24063af2012-07-02 13:50:17 -0300887 {
Andrey Andreevb3f69342016-03-22 11:31:58 +0200888 $cmd .= ' -resize '.$this->width.'x'.$this->height.'\!';
Omar24063af2012-07-02 13:50:17 -0300889 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 }
891
Andrey Andreevbe8bd922016-11-07 12:31:31 +0200892 $cmd .= ' '.escapeshellarg($this->full_src_path).' '.escapeshellarg($this->full_dst_path).' 2>&1';
Andrey Andreevb3f69342016-03-22 11:31:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 $retval = 1;
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200895 // exec() might be disabled
896 if (function_usable('exec'))
897 {
898 @exec($cmd, $output, $retval);
899 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000900
Andrey Andreev8e70b792012-01-12 20:19:24 +0200901 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 if ($retval > 0)
903 {
904 $this->set_error('imglib_image_process_failed');
905 return FALSE;
906 }
907
Andrey Andreev45965742014-08-27 20:40:11 +0300908 chmod($this->full_dst_path, $this->file_permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000909
910 return TRUE;
911 }
912
913 // --------------------------------------------------------------------
914
915 /**
916 * Image Process Using NetPBM
917 *
918 * This function will resize, crop or rotate
919 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 * @param string
921 * @return bool
922 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200923 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100925 if ($this->library_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 {
927 $this->set_error('imglib_libpath_invalid');
928 return FALSE;
929 }
930
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200931 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 switch ($this->image_type)
933 {
934 case 1 :
Andrey Andreev56454792012-05-17 14:32:19 +0300935 $cmd_in = 'giftopnm';
936 $cmd_out = 'ppmtogif';
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 break;
938 case 2 :
Andrey Andreev56454792012-05-17 14:32:19 +0300939 $cmd_in = 'jpegtopnm';
940 $cmd_out = 'ppmtojpeg';
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 break;
942 case 3 :
Andrey Andreev56454792012-05-17 14:32:19 +0300943 $cmd_in = 'pngtopnm';
944 $cmd_out = 'ppmtopng';
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 break;
946 }
947
Alex Bilbied261b1e2012-06-02 11:12:16 +0100948 if ($action === 'crop')
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
950 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
951 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100952 elseif ($action === 'rotate')
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
954 switch ($this->rotation_angle)
955 {
Andrey Andreev56454792012-05-17 14:32:19 +0300956 case 90: $angle = 'r270';
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300958 case 180: $angle = 'r180';
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300960 case 270: $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300962 case 'vrt': $angle = 'tb';
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300964 case 'hor': $angle = 'lr';
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 break;
966 }
967
968 $cmd_inner = 'pnmflip -'.$angle.' ';
969 }
970 else // Resize
971 {
972 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
973 }
974
Andrey Andreev6cab7892017-07-17 13:14:00 +0300975 $cmd = $this->library_path.$cmd_in.' '.escapeshellarg($this->full_src_path).' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
Derek Allard2067d1a2008-11-13 22:59:24 +0000976
977 $retval = 1;
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200978 // exec() might be disabled
979 if (function_usable('exec'))
980 {
981 @exec($cmd, $output, $retval);
982 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000983
Andrey Andreev8e70b792012-01-12 20:19:24 +0200984 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 if ($retval > 0)
986 {
987 $this->set_error('imglib_image_process_failed');
988 return FALSE;
989 }
990
991 // With NetPBM we have to create a temporary image.
992 // If you try manipulating the original it fails so
993 // we have to rename the temp file.
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200994 copy($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200995 unlink($this->dest_folder.'netpbm.tmp');
Andrey Andreev45965742014-08-27 20:40:11 +0300996 chmod($this->full_dst_path, $this->file_permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000997
998 return TRUE;
999 }
1000
1001 // --------------------------------------------------------------------
1002
1003 /**
1004 * Image Rotate Using GD
1005 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 * @return bool
1007 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001008 public function image_rotate_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001010 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 if ( ! ($src_img = $this->image_create_gd()))
1012 {
1013 return FALSE;
1014 }
1015
1016 // Set the background color
1017 // This won't work with transparent PNG files so we are
1018 // going to have to figure out how to determine the color
1019 // of the alpha channel in a future release.
1020
Andrey Andreev3a197592015-05-21 01:05:06 +03001021 $white = imagecolorallocate($src_img, 255, 255, 255);
Derek Allard2067d1a2008-11-13 22:59:24 +00001022
Andrey Andreev8e70b792012-01-12 20:19:24 +02001023 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
1025
Andrey Andreev8e70b792012-01-12 20:19:24 +02001026 // Show the image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001027 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 {
1029 $this->image_display_gd($dst_img);
1030 }
Andrey Andreev8e70b792012-01-12 20:19:24 +02001031 elseif ( ! $this->image_save_gd($dst_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001033 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 }
1035
Andrey Andreev8e70b792012-01-12 20:19:24 +02001036 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 imagedestroy($dst_img);
1038 imagedestroy($src_img);
1039
Andrey Andreev45965742014-08-27 20:40:11 +03001040 chmod($this->full_dst_path, $this->file_permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +00001041
Pascal Kriete8761ef52011-02-14 13:13:52 -05001042 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 }
1044
1045 // --------------------------------------------------------------------
1046
1047 /**
1048 * Create Mirror Image using GD
1049 *
1050 * This function will flip horizontal or vertical
1051 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 * @return bool
1053 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001054 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 {
1056 if ( ! $src_img = $this->image_create_gd())
1057 {
1058 return FALSE;
1059 }
1060
Derek Jones4b9c6292011-07-01 17:40:48 -05001061 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 $height = $this->orig_height;
1063
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001064 if ($this->rotation_angle === 'hor')
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 {
Andrey Andreevfbde2792015-05-11 11:03:06 +03001066 for ($i = 0; $i < $height; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 {
Andrey Andreevfbde2792015-05-11 11:03:06 +03001068 $left = 0;
1069 $right = $width - 1;
1070
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 while ($left < $right)
1072 {
1073 $cl = imagecolorat($src_img, $left, $i);
1074 $cr = imagecolorat($src_img, $right, $i);
1075
1076 imagesetpixel($src_img, $left, $i, $cr);
1077 imagesetpixel($src_img, $right, $i, $cl);
1078
1079 $left++;
1080 $right--;
1081 }
1082 }
1083 }
1084 else
1085 {
Andrey Andreevfbde2792015-05-11 11:03:06 +03001086 for ($i = 0; $i < $width; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 {
Andrey Andreevfbde2792015-05-11 11:03:06 +03001088 $top = 0;
1089 $bottom = $height - 1;
1090
Andrey Andreev5d78fd82015-05-11 18:19:01 +03001091 while ($top < $bottom)
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 {
1093 $ct = imagecolorat($src_img, $i, $top);
Andrey Andreevfbde2792015-05-11 11:03:06 +03001094 $cb = imagecolorat($src_img, $i, $bottom);
Derek Allard2067d1a2008-11-13 22:59:24 +00001095
1096 imagesetpixel($src_img, $i, $top, $cb);
Andrey Andreevfbde2792015-05-11 11:03:06 +03001097 imagesetpixel($src_img, $i, $bottom, $ct);
Derek Allard2067d1a2008-11-13 22:59:24 +00001098
1099 $top++;
Andrey Andreevfbde2792015-05-11 11:03:06 +03001100 $bottom--;
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 }
1102 }
1103 }
1104
Andrey Andreev8e70b792012-01-12 20:19:24 +02001105 // Show the image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001106 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 {
1108 $this->image_display_gd($src_img);
1109 }
Andrey Andreev8e70b792012-01-12 20:19:24 +02001110 elseif ( ! $this->image_save_gd($src_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001112 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 }
1114
Andrey Andreev8e70b792012-01-12 20:19:24 +02001115 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 imagedestroy($src_img);
1117
Andrey Andreev45965742014-08-27 20:40:11 +03001118 chmod($this->full_dst_path, $this->file_permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +00001119
1120 return TRUE;
1121 }
1122
1123 // --------------------------------------------------------------------
1124
1125 /**
1126 * Image Watermark
1127 *
1128 * This is a wrapper function that chooses the type
1129 * of watermarking based on the specified preference.
1130 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 * @return bool
1132 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001133 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001135 return ($this->wm_type === 'overlay') ? $this->overlay_watermark() : $this->text_watermark();
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 }
1137
1138 // --------------------------------------------------------------------
1139
1140 /**
1141 * Watermark - Graphic Version
1142 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 * @return bool
1144 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001145 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 {
1147 if ( ! function_exists('imagecolortransparent'))
1148 {
1149 $this->set_error('imglib_gd_required');
1150 return FALSE;
1151 }
1152
Andrey Andreev8e70b792012-01-12 20:19:24 +02001153 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 $this->get_image_properties();
1155
Andrey Andreev8e70b792012-01-12 20:19:24 +02001156 // Fetch watermark image properties
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001157 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 $wm_img_type = $props['image_type'];
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001159 $wm_width = $props['width'];
1160 $wm_height = $props['height'];
Derek Allard2067d1a2008-11-13 22:59:24 +00001161
Andrey Andreev8e70b792012-01-12 20:19:24 +02001162 // Create two image resources
Derek Jones4b9c6292011-07-01 17:40:48 -05001163 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 $src_img = $this->image_create_gd($this->full_src_path);
1165
1166 // Reverse the offset if necessary
1167 // When the image is positioned at the bottom
1168 // we don't want the vertical offset to push it
Andrey Andreev8e70b792012-01-12 20:19:24 +02001169 // further down. We want the reverse, so we'll
1170 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 // offset when the image is at the right
1172
Andrey Andreev8e70b792012-01-12 20:19:24 +02001173 $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
1174 $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +00001175
Alex Bilbied261b1e2012-06-02 11:12:16 +01001176 if ($this->wm_vrt_alignment === 'B')
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1178
Alex Bilbied261b1e2012-06-02 11:12:16 +01001179 if ($this->wm_hor_alignment === 'R')
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1181
Andrey Andreev8e70b792012-01-12 20:19:24 +02001182 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1184 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1185
Andrey Andreev8e70b792012-01-12 20:19:24 +02001186 // Set the vertical position
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001187 if ($this->wm_vrt_alignment === 'M')
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001189 $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
1190 }
1191 elseif ($this->wm_vrt_alignment === 'B')
1192 {
1193 $y_axis += $this->orig_height - $wm_height;
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 }
1195
Andrey Andreev8e70b792012-01-12 20:19:24 +02001196 // Set the horizontal position
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001197 if ($this->wm_hor_alignment === 'C')
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001199 $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
1200 }
1201 elseif ($this->wm_hor_alignment === 'R')
1202 {
1203 $x_axis += $this->orig_width - $wm_width;
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 }
1205
Andrey Andreev10fb7d12015-08-03 10:05:29 +03001206 // Build the finalized image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001207 if ($wm_img_type === 3 && function_exists('imagealphablending'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 {
1209 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +02001210 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001211
1212 // Set RGB values for text and shadow
1213 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
1214 $alpha = ($rgba & 0x7F000000) >> 24;
1215
1216 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
1217 if ($alpha > 0)
1218 {
1219 // copy the image directly, the image's alpha transparency being the sole determinant of blending
1220 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
1221 }
1222 else
1223 {
1224 // set our RGB value from above to be transparent and merge the images with the specified opacity
1225 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
1226 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
1227 }
1228
Andrey Andreevdb037db2015-01-12 13:45:12 +02001229 // We can preserve transparency for PNG images
Дмитрийb23b8fc2014-10-20 00:36:55 +04001230 if ($this->image_type === 3)
1231 {
1232 imagealphablending($src_img, FALSE);
1233 imagesavealpha($src_img, TRUE);
1234 }
1235
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001236 // Output the image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001237 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 {
1239 $this->image_display_gd($src_img);
1240 }
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001241 elseif ( ! $this->image_save_gd($src_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001243 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 }
1245
1246 imagedestroy($src_img);
1247 imagedestroy($wm_img);
1248
1249 return TRUE;
1250 }
1251
1252 // --------------------------------------------------------------------
1253
1254 /**
1255 * Watermark - Text Version
1256 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 * @return bool
1258 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001259 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 {
1261 if ( ! ($src_img = $this->image_create_gd()))
1262 {
1263 return FALSE;
1264 }
1265
Alex Bilbied261b1e2012-06-02 11:12:16 +01001266 if ($this->wm_use_truetype === TRUE && ! file_exists($this->wm_font_path))
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 {
1268 $this->set_error('imglib_missing_font');
1269 return FALSE;
1270 }
1271
Andrey Andreev8e70b792012-01-12 20:19:24 +02001272 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 $this->get_image_properties();
1274
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 // Reverse the vertical offset
1276 // When the image is positioned at the bottom
1277 // we don't want the vertical offset to push it
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001278 // further down. We want the reverse, so we'll
Andrey Andreev8e70b792012-01-12 20:19:24 +02001279 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 // offset flips itself automatically
1281
Alex Bilbied261b1e2012-06-02 11:12:16 +01001282 if ($this->wm_vrt_alignment === 'B')
Andrey Andreeve52fc262014-02-11 13:27:01 +02001283 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
Andrey Andreeve52fc262014-02-11 13:27:01 +02001285 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001286
Alex Bilbied261b1e2012-06-02 11:12:16 +01001287 if ($this->wm_hor_alignment === 'R')
Andrey Andreeve52fc262014-02-11 13:27:01 +02001288 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 $this->wm_hor_offset = $this->wm_hor_offset * -1;
Andrey Andreeve52fc262014-02-11 13:27:01 +02001290 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001291
1292 // Set font width and height
1293 // These are calculated differently depending on
1294 // whether we are using the true type font or not
Alex Bilbied261b1e2012-06-02 11:12:16 +01001295 if ($this->wm_use_truetype === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 {
Andrey Andreeve52fc262014-02-11 13:27:01 +02001297 if (empty($this->wm_font_size))
Andrey Andreeva92b9032011-12-24 19:05:58 +02001298 {
1299 $this->wm_font_size = 17;
1300 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001301
Andrey Andreeve52fc262014-02-11 13:27:01 +02001302 if (function_exists('imagettfbbox'))
1303 {
1304 $temp = imagettfbbox($this->wm_font_size, 0, $this->wm_font_path, $this->wm_text);
1305 $temp = $temp[2] - $temp[0];
1306
1307 $fontwidth = $temp / strlen($this->wm_text);
1308 }
1309 else
1310 {
1311 $fontwidth = $this->wm_font_size - ($this->wm_font_size / 4);
1312 }
1313
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 $fontheight = $this->wm_font_size;
1315 $this->wm_vrt_offset += $this->wm_font_size;
1316 }
1317 else
1318 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001319 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001320 $fontheight = imagefontheight($this->wm_font_size);
1321 }
1322
1323 // Set base X and Y axis values
1324 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1325 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1326
Alex Bilbied261b1e2012-06-02 11:12:16 +01001327 if ($this->wm_use_drop_shadow === FALSE)
omar0779e992015-01-31 11:57:07 -07001328 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 $this->wm_shadow_distance = 0;
omar0779e992015-01-31 11:57:07 -07001330 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001331
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001332 $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
1333 $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +00001334
omar0779e992015-01-31 11:57:07 -07001335 // Set vertical alignment
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001336 if ($this->wm_vrt_alignment === 'M')
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001338 $y_axis += ($this->orig_height / 2) + ($fontheight / 2);
1339 }
1340 elseif ($this->wm_vrt_alignment === 'B')
1341 {
1342 $y_axis += $this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight / 2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 }
Andrey Andreev3a197592015-05-21 01:05:06 +03001344
omar0779e992015-01-31 11:57:07 -07001345 // Set horizontal alignment
1346 if ($this->wm_hor_alignment === 'R')
1347 {
omar9c855322015-01-31 11:58:03 -07001348 $x_axis += $this->orig_width - ($fontwidth * strlen($this->wm_text)) - $this->wm_shadow_distance;
omar0779e992015-01-31 11:57:07 -07001349 }
1350 elseif ($this->wm_hor_alignment === 'C')
1351 {
1352 $x_axis += floor(($this->orig_width - ($fontwidth * strlen($this->wm_text))) / 2);
1353 }
Andrey Andreev3a197592015-05-21 01:05:06 +03001354
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001355 if ($this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 {
omar0779e992015-01-31 11:57:07 -07001357 // Offset from text
1358 $x_shad = $x_axis + $this->wm_shadow_distance;
1359 $y_shad = $y_axis + $this->wm_shadow_distance;
Andrey Andreev3a197592015-05-21 01:05:06 +03001360
omar0779e992015-01-31 11:57:07 -07001361 /* Set RGB values for shadow
Andrey Andreev8323ae62011-12-31 18:39:10 +02001362 *
1363 * First character is #, so we don't really need it.
1364 * Get the rest of the string and split it into 2-length
1365 * hex values:
1366 */
Andrey Andreev8323ae62011-12-31 18:39:10 +02001367 $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2);
Ronald Beilsma8f80bc42012-01-12 16:41:49 +01001368 $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[1]), hexdec($drp_color[2]));
Andrey Andreev3a197592015-05-21 01:05:06 +03001369
omar0779e992015-01-31 11:57:07 -07001370 // Add the shadow to the source image
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001371 if ($this->wm_use_truetype)
1372 {
1373 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001374 }
1375 else
1376 {
1377 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001378 }
omar0779e992015-01-31 11:57:07 -07001379 }
Andrey Andreev3a197592015-05-21 01:05:06 +03001380
omar0779e992015-01-31 11:57:07 -07001381 /* Set RGB values for text
1382 *
1383 * First character is #, so we don't really need it.
1384 * Get the rest of the string and split it into 2-length
1385 * hex values:
1386 */
1387 $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2);
1388 $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
Andrey Andreev99ae2262012-10-05 15:14:30 +03001389
omar0779e992015-01-31 11:57:07 -07001390 // Add the text to the source image
1391 if ($this->wm_use_truetype)
1392 {
1393 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1394 }
1395 else
1396 {
1397 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1398 }
Andrey Andreev3a197592015-05-21 01:05:06 +03001399
omar0779e992015-01-31 11:57:07 -07001400 // We can preserve transparency for PNG images
1401 if ($this->image_type === 3)
1402 {
1403 imagealphablending($src_img, FALSE);
1404 imagesavealpha($src_img, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 }
1406
Andrey Andreev8e70b792012-01-12 20:19:24 +02001407 // Output the final image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001408 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 {
1410 $this->image_display_gd($src_img);
1411 }
1412 else
1413 {
1414 $this->image_save_gd($src_img);
1415 }
1416
1417 imagedestroy($src_img);
1418
1419 return TRUE;
1420 }
1421
1422 // --------------------------------------------------------------------
1423
1424 /**
1425 * Create Image - GD
1426 *
1427 * This simply creates an image resource handle
1428 * based on the type of image being processed
1429 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001431 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001432 * @return resource
1433 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001434 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001435 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001436 if ($path === '')
Andrey Andreeve52fc262014-02-11 13:27:01 +02001437 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 $path = $this->full_src_path;
Andrey Andreeve52fc262014-02-11 13:27:01 +02001439 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001440
Alex Bilbied261b1e2012-06-02 11:12:16 +01001441 if ($image_type === '')
Andrey Andreeve52fc262014-02-11 13:27:01 +02001442 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001443 $image_type = $this->image_type;
Andrey Andreeve52fc262014-02-11 13:27:01 +02001444 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001445
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 switch ($image_type)
1447 {
Andrey Andreev3a197592015-05-21 01:05:06 +03001448 case 1:
Andrey Andreeve52fc262014-02-11 13:27:01 +02001449 if ( ! function_exists('imagecreatefromgif'))
1450 {
1451 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1452 return FALSE;
1453 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001454
Andrey Andreeve52fc262014-02-11 13:27:01 +02001455 return imagecreatefromgif($path);
Andrey Andreev3a197592015-05-21 01:05:06 +03001456 case 2:
Andrey Andreeve52fc262014-02-11 13:27:01 +02001457 if ( ! function_exists('imagecreatefromjpeg'))
1458 {
1459 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1460 return FALSE;
1461 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001462
Andrey Andreeve52fc262014-02-11 13:27:01 +02001463 return imagecreatefromjpeg($path);
Andrey Andreev3a197592015-05-21 01:05:06 +03001464 case 3:
Andrey Andreeve52fc262014-02-11 13:27:01 +02001465 if ( ! function_exists('imagecreatefrompng'))
1466 {
1467 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1468 return FALSE;
1469 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001470
Andrey Andreeve52fc262014-02-11 13:27:01 +02001471 return imagecreatefrompng($path);
1472 default:
1473 $this->set_error(array('imglib_unsupported_imagecreate'));
1474 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001475 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 }
1477
1478 // --------------------------------------------------------------------
1479
1480 /**
1481 * Write image file to disk - GD
1482 *
1483 * Takes an image resource as input and writes the file
1484 * to the specified destination
1485 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 * @param resource
1487 * @return bool
1488 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001489 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001490 {
1491 switch ($this->image_type)
1492 {
Andrey Andreev56454792012-05-17 14:32:19 +03001493 case 1:
1494 if ( ! function_exists('imagegif'))
1495 {
1496 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1497 return FALSE;
1498 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001499
Andrey Andreev56454792012-05-17 14:32:19 +03001500 if ( ! @imagegif($resource, $this->full_dst_path))
1501 {
1502 $this->set_error('imglib_save_failed');
1503 return FALSE;
1504 }
1505 break;
1506 case 2:
1507 if ( ! function_exists('imagejpeg'))
1508 {
1509 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1510 return FALSE;
1511 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001512
Andrey Andreev56454792012-05-17 14:32:19 +03001513 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1514 {
1515 $this->set_error('imglib_save_failed');
1516 return FALSE;
1517 }
1518 break;
1519 case 3:
1520 if ( ! function_exists('imagepng'))
1521 {
1522 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1523 return FALSE;
1524 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001525
Andrey Andreev56454792012-05-17 14:32:19 +03001526 if ( ! @imagepng($resource, $this->full_dst_path))
1527 {
1528 $this->set_error('imglib_save_failed');
1529 return FALSE;
1530 }
1531 break;
1532 default:
1533 $this->set_error(array('imglib_unsupported_imagecreate'));
1534 return FALSE;
1535 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 }
1537
1538 return TRUE;
1539 }
1540
1541 // --------------------------------------------------------------------
1542
1543 /**
1544 * Dynamically outputs an image
1545 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 * @param resource
1547 * @return void
1548 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001549 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001550 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001551 header('Content-Disposition: filename='.$this->source_image.';');
1552 header('Content-Type: '.$this->mime_type);
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 header('Content-Transfer-Encoding: binary');
1554 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1555
1556 switch ($this->image_type)
1557 {
Andrey Andreev56454792012-05-17 14:32:19 +03001558 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 break;
Ted Wood9cc707b2013-01-08 19:41:45 -08001560 case 2 : imagejpeg($resource, NULL, $this->quality);
Derek Allard2067d1a2008-11-13 22:59:24 +00001561 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001562 case 3 : imagepng($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001563 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001564 default: echo 'Unable to display the image';
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 break;
1566 }
1567 }
1568
1569 // --------------------------------------------------------------------
1570
1571 /**
1572 * Re-proportion Image Width/Height
1573 *
1574 * When creating thumbs, the desired width/height
1575 * can end up warping the image due to an incorrect
1576 * ratio between the full-sized image and the thumb.
1577 *
1578 * This function lets us re-proportion the width/height
1579 * if users choose to maintain the aspect ratio when resizing.
1580 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001581 * @return void
1582 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001583 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001584 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001585 if (($this->width === 0 && $this->height === 0) OR $this->orig_width === 0 OR $this->orig_height === 0
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001586 OR ( ! ctype_digit((string) $this->width) && ! ctype_digit((string) $this->height))
1587 OR ! ctype_digit((string) $this->orig_width) OR ! ctype_digit((string) $this->orig_height))
Derek Allard2067d1a2008-11-13 22:59:24 +00001588 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001589 return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 }
1591
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001592 // Sanitize
Andrey Andreev8e70b792012-01-12 20:19:24 +02001593 $this->width = (int) $this->width;
1594 $this->height = (int) $this->height;
1595
1596 if ($this->master_dim !== 'width' && $this->master_dim !== 'height')
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001598 if ($this->width > 0 && $this->height > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001599 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001600 $this->master_dim = ((($this->orig_height/$this->orig_width) - ($this->height/$this->width)) < 0)
1601 ? 'width' : 'height';
Derek Allard2067d1a2008-11-13 22:59:24 +00001602 }
1603 else
1604 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001605 $this->master_dim = ($this->height === 0) ? 'width' : 'height';
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 }
1607 }
Andrey Andreev8e70b792012-01-12 20:19:24 +02001608 elseif (($this->master_dim === 'width' && $this->width === 0)
1609 OR ($this->master_dim === 'height' && $this->height === 0))
1610 {
1611 return;
1612 }
1613
1614 if ($this->master_dim === 'width')
1615 {
1616 $this->height = (int) ceil($this->width*$this->orig_height/$this->orig_width);
1617 }
1618 else
1619 {
1620 $this->width = (int) ceil($this->orig_width*$this->height/$this->orig_height);
1621 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001622 }
1623
1624 // --------------------------------------------------------------------
1625
1626 /**
1627 * Get image properties
1628 *
1629 * A helper function that gets info about the file
1630 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001632 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001633 * @return mixed
1634 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001635 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 {
1637 // For now we require GD but we should
1638 // find a way to determine this using IM or NetPBM
1639
Alex Bilbied261b1e2012-06-02 11:12:16 +01001640 if ($path === '')
Andrey Andreev8e70b792012-01-12 20:19:24 +02001641 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001642 $path = $this->full_src_path;
Andrey Andreev8e70b792012-01-12 20:19:24 +02001643 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001644
1645 if ( ! file_exists($path))
1646 {
1647 $this->set_error('imglib_invalid_path');
1648 return FALSE;
1649 }
1650
Phil Sturgeon901998a2011-08-26 10:03:33 +01001651 $vals = getimagesize($path);
Andrey Andreev747deff2017-01-06 13:22:16 +02001652 if ($vals === FALSE)
1653 {
1654 $this->set_error('imglib_invalid_image');
1655 return FALSE;
1656 }
Andrey Andreev5f7302c2017-01-06 13:26:08 +02001657
Derek Allard2067d1a2008-11-13 22:59:24 +00001658 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
Andrey Andreev5f7302c2017-01-06 13:26:08 +02001659 $mime = isset($types[$vals[2]]) ? 'image/'.$types[$vals[2]] : 'image/jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001660
Alex Bilbied261b1e2012-06-02 11:12:16 +01001661 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001662 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001663 return array(
Andrey Andreev5f7302c2017-01-06 13:26:08 +02001664 'width' => $vals[0],
1665 'height' => $vals[1],
1666 'image_type' => $vals[2],
1667 'size_str' => $vals[3],
1668 'mime_type' => $mime
1669 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001670 }
1671
Andrey Andreev5f7302c2017-01-06 13:26:08 +02001672 $this->orig_width = $vals[0];
1673 $this->orig_height = $vals[1];
1674 $this->image_type = $vals[2];
1675 $this->size_str = $vals[3];
1676 $this->mime_type = $mime;
Derek Allard2067d1a2008-11-13 22:59:24 +00001677
1678 return TRUE;
1679 }
1680
1681 // --------------------------------------------------------------------
1682
1683 /**
1684 * Size calculator
1685 *
1686 * This function takes a known width x height and
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001687 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001688 * new variable needs to be known
1689 *
1690 * $props = array(
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001691 * 'width' => $width,
1692 * 'height' => $height,
1693 * 'new_width' => 40,
1694 * 'new_height' => ''
1695 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001696 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001697 * @param array
1698 * @return array
1699 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001700 public function size_calculator($vals)
Derek Allard2067d1a2008-11-13 22:59:24 +00001701 {
1702 if ( ! is_array($vals))
1703 {
1704 return;
1705 }
1706
1707 $allowed = array('new_width', 'new_height', 'width', 'height');
1708
1709 foreach ($allowed as $item)
1710 {
Andrey Andreev56454792012-05-17 14:32:19 +03001711 if (empty($vals[$item]))
1712 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001713 $vals[$item] = 0;
Andrey Andreev56454792012-05-17 14:32:19 +03001714 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001715 }
1716
Alex Bilbied261b1e2012-06-02 11:12:16 +01001717 if ($vals['width'] === 0 OR $vals['height'] === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001718 {
1719 return $vals;
1720 }
1721
Alex Bilbied261b1e2012-06-02 11:12:16 +01001722 if ($vals['new_width'] === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 {
1724 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1725 }
Alex Bilbied261b1e2012-06-02 11:12:16 +01001726 elseif ($vals['new_height'] === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 {
1728 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1729 }
1730
1731 return $vals;
1732 }
1733
1734 // --------------------------------------------------------------------
1735
1736 /**
1737 * Explode source_image
1738 *
1739 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001740 * from the source_image. This function lets us deal with
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001741 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001743 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 * $array['name'] = 'my.cool';
1745 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 * @param array
1747 * @return array
1748 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001749 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 {
Derek Jones08cae632009-02-10 20:03:29 +00001751 $ext = strrchr($source_image, '.');
1752 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001753
Derek Jones08cae632009-02-10 20:03:29 +00001754 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001755 }
1756
1757 // --------------------------------------------------------------------
1758
1759 /**
1760 * Is GD Installed?
1761 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 * @return bool
1763 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001764 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 {
1766 if ( ! extension_loaded('gd'))
1767 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001768 /* As it is stated in the PHP manual, dl() is not always available
1769 * and even if so - it could generate an E_WARNING message on failure
1770 */
Andrey Andreev8e70b792012-01-12 20:19:24 +02001771 return (function_exists('dl') && @dl('gd.so'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 }
1773
1774 return TRUE;
1775 }
1776
1777 // --------------------------------------------------------------------
1778
1779 /**
1780 * Get GD version
1781 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 * @return mixed
1783 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001784 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 {
1786 if (function_exists('gd_info'))
1787 {
1788 $gd_version = @gd_info();
Andrey Andreev8e70b792012-01-12 20:19:24 +02001789 return preg_replace('/\D/', '', $gd_version['GD Version']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001790 }
1791
1792 return FALSE;
1793 }
1794
1795 // --------------------------------------------------------------------
1796
1797 /**
1798 * Set error message
1799 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 * @param string
1801 * @return void
1802 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001803 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 {
1805 $CI =& get_instance();
1806 $CI->lang->load('imglib');
1807
1808 if (is_array($msg))
1809 {
1810 foreach ($msg as $val)
1811 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001812 $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001813 $this->error_msg[] = $msg;
1814 log_message('error', $msg);
1815 }
1816 }
1817 else
1818 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001819 $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg);
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 $this->error_msg[] = $msg;
1821 log_message('error', $msg);
1822 }
1823 }
1824
1825 // --------------------------------------------------------------------
1826
1827 /**
1828 * Show error messages
1829 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001831 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001832 * @return string
1833 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001834 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001836 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001837 }
1838
1839}