blob: 9eb05c2fd6fe377114e2a3aacc51b06a384dcce7 [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 *
Master Yodada60e9b2016-12-31 08:46:18 -08009 * Copyright (c) 2014 - 2017, 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/)
Master Yodada60e9b2016-12-31 08:46:18 -080032 * @copyright Copyright (c) 2014 - 2017, 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 Andreev90726b82015-01-20 12:39:22 +0200395 log_message('info', 'Image Lib Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * Initialize image properties
402 *
403 * Resets values in case this class is used in a loop
404 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @return void
406 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200407 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
Sam Doidge5cb5c0a2013-03-13 01:28:06 +0000409 $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 +0000410
411 foreach ($props as $val)
412 {
413 $this->$val = '';
414 }
415
Michael Denniscb07a322011-08-20 23:40:59 -0700416 $this->image_library = 'gd2';
417 $this->dynamic_output = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300418 $this->quality = 90;
Michael Denniscb07a322011-08-20 23:40:59 -0700419 $this->create_thumb = FALSE;
420 $this->thumb_marker = '_thumb';
421 $this->maintain_ratio = TRUE;
422 $this->master_dim = 'auto';
423 $this->wm_type = 'text';
424 $this->wm_x_transp = 4;
425 $this->wm_y_transp = 4;
426 $this->wm_font_size = 17;
427 $this->wm_vrt_alignment = 'B';
428 $this->wm_hor_alignment = 'C';
429 $this->wm_padding = 0;
430 $this->wm_hor_offset = 0;
431 $this->wm_vrt_offset = 0;
432 $this->wm_font_color = '#ffffff';
433 $this->wm_shadow_distance = 2;
434 $this->wm_opacity = 50;
435 $this->create_fnc = 'imagecreatetruecolor';
436 $this->copy_fnc = 'imagecopyresampled';
437 $this->error_msg = array();
438 $this->wm_use_drop_shadow = FALSE;
439 $this->wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
441
442 // --------------------------------------------------------------------
443
444 /**
445 * initialize image preferences
446 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @param array
448 * @return bool
449 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200450 public function initialize($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200452 // Convert array elements into class variables
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 if (count($props) > 0)
454 {
455 foreach ($props as $key => $val)
456 {
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200457 if (property_exists($this, $key))
458 {
Andrey Andreeveac4adf2016-03-22 11:24:14 +0200459 if (in_array($key, array('wm_font_color', 'wm_shadow_color'), TRUE))
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200460 {
Andrey Andreev8323ae62011-12-31 18:39:10 +0200461 if (preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches))
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200462 {
Andrey Andreevcf2ba9e2012-01-01 21:57:13 +0200463 /* $matches[1] contains our hex color value, but it might be
Andrey Andreev8323ae62011-12-31 18:39:10 +0200464 * both in the full 6-length format or the shortened 3-length
465 * value.
Andrey Andreevbb96c8b2011-12-31 18:48:39 +0200466 * We'll later need the full version, so we keep it if it's
Andrey Andreev8323ae62011-12-31 18:39:10 +0200467 * already there and if not - we'll convert to it. We can
468 * access string characters by their index as in an array,
469 * so we'll do that and use concatenation to form the final
470 * value:
471 */
Andrey Andreev665af0c2011-12-30 14:39:29 +0200472 $val = (strlen($matches[1]) === 6)
473 ? '#'.$matches[1]
474 : '#'.$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 +0200475 }
476 else
477 {
478 continue;
479 }
480 }
Andrey Andreeveac4adf2016-03-22 11:24:14 +0200481 elseif (in_array($key, array('width', 'height'), TRUE) && ! ctype_digit((string) $val))
482 {
483 continue;
484 }
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200485
486 $this->$key = $val;
487 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489 }
490
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200491 // Is there a source image? If not, there's no reason to continue
Alex Bilbied261b1e2012-06-02 11:12:16 +0100492 if ($this->source_image === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
494 $this->set_error('imglib_source_image_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500495 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 }
497
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200498 /* Is getimagesize() available?
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 *
500 * We use it to determine the image properties (width/height).
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200501 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 * properties using ImageMagick and NetPBM
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 */
504 if ( ! function_exists('getimagesize'))
505 {
506 $this->set_error('imglib_gd_required_for_props');
507 return FALSE;
508 }
509
510 $this->image_library = strtolower($this->image_library);
511
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200512 /* Set the full server path
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 *
514 * The source image may or may not contain a path.
515 * Either way, we'll try use realpath to generate the
516 * full server path in order to more reliably read it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 */
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200518 if (($full_source_path = realpath($this->source_image)) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200520 $full_source_path = str_replace('\\', '/', $full_source_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 }
522 else
523 {
524 $full_source_path = $this->source_image;
525 }
526
527 $x = explode('/', $full_source_path);
528 $this->source_image = end($x);
529 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
530
531 // Set the Image Properties
532 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
533 {
Eric Barnesb1673362011-12-05 22:05:38 -0500534 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536
537 /*
538 * Assign the "new" image name/path
539 *
540 * If the user has set a "new_image" name it means
541 * we are making a copy of the source image. If not
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200542 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * set the destination filename and path accordingly.
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100545 if ($this->new_image === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
Andrey Andreev961271d2016-12-09 12:48:57 +0200547 $this->dest_image = $this->source_image;
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 $this->dest_folder = $this->source_folder;
549 }
Andrey Andreev961271d2016-12-09 12:48:57 +0200550 elseif (strpos($this->new_image, '/') === FALSE && strpos($this->new_image, '\\') === FALSE)
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200551 {
Andrey Andreev961271d2016-12-09 12:48:57 +0200552 $this->dest_image = $this->new_image;
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200553 $this->dest_folder = $this->source_folder;
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200554 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 else
556 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200557 // Is there a file name?
Andrey Andreevf2a613d2016-12-12 11:39:38 +0200558 if ( ! preg_match('#\.(jpg|jpeg|gif|png)$#i', $this->new_image))
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200559 {
Andrey Andreev961271d2016-12-09 12:48:57 +0200560 $this->dest_image = $this->source_image;
Andrey Andreevf2a613d2016-12-12 11:39:38 +0200561 $this->dest_folder = $this->new_image;
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200562 }
563 else
564 {
Andrey Andreevf2a613d2016-12-12 11:39:38 +0200565 $x = explode('/', str_replace('\\', '/', $this->new_image));
Andrey Andreev961271d2016-12-09 12:48:57 +0200566 $this->dest_image = end($x);
Andrey Andreevf2a613d2016-12-12 11:39:38 +0200567 $this->dest_folder = str_replace($this->dest_image, '', $this->new_image);
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 }
Andrey Andreevf2a613d2016-12-12 11:39:38 +0200569
570 $this->dest_folder = realpath($this->dest_folder).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 }
572
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200573 /* Compile the finalized filenames/paths
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 *
575 * We'll create two master strings containing the
576 * full server path to the source image and the
577 * full server path to the destination image.
578 * We'll also split the destination image name
579 * so we can insert the thumbnail marker if needed.
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100581 if ($this->create_thumb === FALSE OR $this->thumb_marker === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 {
583 $this->thumb_marker = '';
584 }
585
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200586 $xp = $this->explode_name($this->dest_image);
Derek Allard2067d1a2008-11-13 22:59:24 +0000587
588 $filename = $xp['name'];
589 $file_ext = $xp['ext'];
590
591 $this->full_src_path = $this->source_folder.$this->source_image;
592 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
593
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200594 /* Should we maintain image proportions?
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 *
596 * When creating thumbs or copies, the target width/height
597 * might not be in correct proportion with the source
Andrey Andreev8e70b792012-01-12 20:19:24 +0200598 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100600 if ($this->maintain_ratio === TRUE && ($this->width !== 0 OR $this->height !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
602 $this->image_reproportion();
603 }
604
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200605 /* Was a width and height specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 *
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200607 * If the destination width/height was not submitted we
608 * will use the values from the actual file
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100610 if ($this->width === '')
Andrey Andreev8e70b792012-01-12 20:19:24 +0200611 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 $this->width = $this->orig_width;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200613 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000614
Alex Bilbied261b1e2012-06-02 11:12:16 +0100615 if ($this->height === '')
Andrey Andreev8e70b792012-01-12 20:19:24 +0200616 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 $this->height = $this->orig_height;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200618 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000619
620 // Set the quality
Andrey Andreev8e70b792012-01-12 20:19:24 +0200621 $this->quality = trim(str_replace('%', '', $this->quality));
Derek Allard2067d1a2008-11-13 22:59:24 +0000622
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200623 if ($this->quality === '' OR $this->quality === 0 OR ! ctype_digit($this->quality))
Andrey Andreev8e70b792012-01-12 20:19:24 +0200624 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 $this->quality = 90;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200626 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000627
628 // Set the x/y coordinates
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300629 is_numeric($this->x_axis) OR $this->x_axis = 0;
630 is_numeric($this->y_axis) OR $this->y_axis = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000631
632 // Watermark-related Stuff...
Alex Bilbied261b1e2012-06-02 11:12:16 +0100633 if ($this->wm_overlay_path !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200635 $this->wm_overlay_path = str_replace('\\', '/', realpath($this->wm_overlay_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 }
637
Alex Bilbied261b1e2012-06-02 11:12:16 +0100638 if ($this->wm_shadow_color !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
640 $this->wm_use_drop_shadow = TRUE;
641 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100642 elseif ($this->wm_use_drop_shadow === TRUE && $this->wm_shadow_color === '')
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200643 {
644 $this->wm_use_drop_shadow = FALSE;
645 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000646
Alex Bilbied261b1e2012-06-02 11:12:16 +0100647 if ($this->wm_font_path !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
649 $this->wm_use_truetype = TRUE;
650 }
651
652 return TRUE;
653 }
654
655 // --------------------------------------------------------------------
656
657 /**
658 * Image Resize
659 *
660 * This is a wrapper function that chooses the proper
661 * resize function based on the protocol specified
662 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 * @return bool
664 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200665 public function resize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 {
Andrey Andreev1a9f52c2012-01-07 01:06:34 +0200667 $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 return $this->$protocol('resize');
669 }
670
671 // --------------------------------------------------------------------
672
673 /**
674 * Image Crop
675 *
676 * This is a wrapper function that chooses the proper
677 * cropping function based on the protocol specified
678 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 * @return bool
680 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200681 public function crop()
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
Andrey Andreev5a67e3d2012-01-07 01:21:09 +0200683 $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 return $this->$protocol('crop');
685 }
686
687 // --------------------------------------------------------------------
688
689 /**
690 * Image Rotate
691 *
692 * This is a wrapper function that chooses the proper
693 * rotation function based on the protocol specified
694 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 * @return bool
696 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200697 public function rotate()
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
699 // Allowed rotation values
700 $degs = array(90, 180, 270, 'vrt', 'hor');
701
Alex Bilbied261b1e2012-06-02 11:12:16 +0100702 if ($this->rotation_angle === '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 {
704 $this->set_error('imglib_rotation_angle_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500705 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 }
707
708 // Reassign the width and height
Alex Bilbied261b1e2012-06-02 11:12:16 +0100709 if ($this->rotation_angle === 90 OR $this->rotation_angle === 270)
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
711 $this->width = $this->orig_height;
712 $this->height = $this->orig_width;
713 }
714 else
715 {
716 $this->width = $this->orig_width;
717 $this->height = $this->orig_height;
718 }
719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 // Choose resizing function
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200721 if ($this->image_library === 'imagemagick' OR $this->image_library === 'netpbm')
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 {
723 $protocol = 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 return $this->$protocol('rotate');
725 }
726
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200727 return ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
728 ? $this->image_mirror_gd()
729 : $this->image_rotate_gd();
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 }
731
732 // --------------------------------------------------------------------
733
734 /**
735 * Image Process Using GD/GD2
736 *
737 * This function will resize or crop
738 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 * @param string
740 * @return bool
741 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200742 public function image_process_gd($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 {
744 $v2_override = FALSE;
745
746 // If the target width/height match the source, AND if the new file name is not equal to the old file name
747 // 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 +0100748 if ($this->dynamic_output === FALSE && $this->orig_width === $this->width && $this->orig_height === $this->height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100750 if ($this->source_image !== $this->new_image && @copy($this->full_src_path, $this->full_dst_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 {
Andrey Andreev45965742014-08-27 20:40:11 +0300752 chmod($this->full_dst_path, $this->file_permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200754
755 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 }
757
758 // Let's set up our values based on the action
Alex Bilbied261b1e2012-06-02 11:12:16 +0100759 if ($action === 'crop')
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200761 // Reassign the source width/height if cropping
Derek Jones4b9c6292011-07-01 17:40:48 -0500762 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 $this->orig_height = $this->height;
764
765 // GD 2.0 has a cropping bug so we'll test for it
766 if ($this->gd_version() !== FALSE)
767 {
768 $gd_version = str_replace('0', '', $this->gd_version());
Andrey Andreevd4818b72014-10-23 17:15:32 +0300769 $v2_override = ($gd_version == 2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 }
771 }
772 else
773 {
774 // If resizing the x/y axis must be zero
775 $this->x_axis = 0;
776 $this->y_axis = 0;
777 }
778
Andrey Andreev10fb7d12015-08-03 10:05:29 +0300779 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 if ( ! ($src_img = $this->image_create_gd()))
781 {
782 return FALSE;
783 }
784
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200785 /* Create the image
786 *
787 * Old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
788 * it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
789 * below should that ever prove inaccurate.
790 *
Alex Bilbied261b1e2012-06-02 11:12:16 +0100791 * if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor') && $v2_override === FALSE)
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200792 */
Andrey Andreev8e70b792012-01-12 20:19:24 +0200793 if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
795 $create = 'imagecreatetruecolor';
796 $copy = 'imagecopyresampled';
797 }
798 else
799 {
800 $create = 'imagecreate';
801 $copy = 'imagecopyresized';
802 }
803
804 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500805
Alex Bilbied261b1e2012-06-02 11:12:16 +0100806 if ($this->image_type === 3) // png we can actually preserve transparency
Derek Jones595bfd12010-08-20 10:28:22 -0500807 {
808 imagealphablending($dst_img, FALSE);
809 imagesavealpha($dst_img, TRUE);
810 }
Barry Mienydd671972010-10-04 16:33:58 +0200811
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
813
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200814 // Show the image
Alex Bilbied261b1e2012-06-02 11:12:16 +0100815 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
817 $this->image_display_gd($dst_img);
818 }
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200819 elseif ( ! $this->image_save_gd($dst_img)) // Or save it
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200821 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 }
823
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200824 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 imagedestroy($dst_img);
826 imagedestroy($src_img);
827
Andrey Andreev45965742014-08-27 20:40:11 +0300828 chmod($this->full_dst_path, $this->file_permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000829
830 return TRUE;
831 }
832
833 // --------------------------------------------------------------------
834
835 /**
836 * Image Process Using ImageMagick
837 *
838 * This function will resize, crop or rotate
839 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 * @param string
841 * @return bool
842 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200843 public function image_process_imagemagick($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 {
Andrey Andreev3a197592015-05-21 01:05:06 +0300845 // Do we have a vaild library path?
Alex Bilbied261b1e2012-06-02 11:12:16 +0100846 if ($this->library_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 {
848 $this->set_error('imglib_libpath_invalid');
849 return FALSE;
850 }
851
Andrey Andreev8e70b792012-01-12 20:19:24 +0200852 if ( ! preg_match('/convert$/i', $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200854 $this->library_path = rtrim($this->library_path, '/').'/convert';
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 }
856
857 // Execute the command
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200858 $cmd = $this->library_path.' -quality '.$this->quality;
Derek Allard2067d1a2008-11-13 22:59:24 +0000859
Alex Bilbied261b1e2012-06-02 11:12:16 +0100860 if ($action === 'crop')
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 {
Andrey Andreevb3f69342016-03-22 11:31:58 +0200862 $cmd .= ' -crop '.$this->width.'x'.$this->height.'+'.$this->x_axis.'+'.$this->y_axis;
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100864 elseif ($action === 'rotate')
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 {
Andrey Andreevb3f69342016-03-22 11:31:58 +0200866 $cmd .= ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
867 ? ' -flop'
868 : ' -rotate '.$this->rotation_angle;
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 }
Andrey Andreev8e70b792012-01-12 20:19:24 +0200870 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 {
Omar24063af2012-07-02 13:50:17 -0300872 if($this->maintain_ratio === TRUE)
873 {
Andrey Andreevb3f69342016-03-22 11:31:58 +0200874 $cmd .= ' -resize '.$this->width.'x'.$this->height;
Omar24063af2012-07-02 13:50:17 -0300875 }
Omarbb531d62012-06-29 10:48:28 -0300876 else
Omar24063af2012-07-02 13:50:17 -0300877 {
Andrey Andreevb3f69342016-03-22 11:31:58 +0200878 $cmd .= ' -resize '.$this->width.'x'.$this->height.'\!';
Omar24063af2012-07-02 13:50:17 -0300879 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 }
881
Andrey Andreevbe8bd922016-11-07 12:31:31 +0200882 $cmd .= ' '.escapeshellarg($this->full_src_path).' '.escapeshellarg($this->full_dst_path).' 2>&1';
Andrey Andreevb3f69342016-03-22 11:31:58 +0200883
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 $retval = 1;
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200885 // exec() might be disabled
886 if (function_usable('exec'))
887 {
888 @exec($cmd, $output, $retval);
889 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000890
Andrey Andreev8e70b792012-01-12 20:19:24 +0200891 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 if ($retval > 0)
893 {
894 $this->set_error('imglib_image_process_failed');
895 return FALSE;
896 }
897
Andrey Andreev45965742014-08-27 20:40:11 +0300898 chmod($this->full_dst_path, $this->file_permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000899
900 return TRUE;
901 }
902
903 // --------------------------------------------------------------------
904
905 /**
906 * Image Process Using NetPBM
907 *
908 * This function will resize, crop or rotate
909 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 * @param string
911 * @return bool
912 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200913 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100915 if ($this->library_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
917 $this->set_error('imglib_libpath_invalid');
918 return FALSE;
919 }
920
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200921 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 switch ($this->image_type)
923 {
924 case 1 :
Andrey Andreev56454792012-05-17 14:32:19 +0300925 $cmd_in = 'giftopnm';
926 $cmd_out = 'ppmtogif';
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 break;
928 case 2 :
Andrey Andreev56454792012-05-17 14:32:19 +0300929 $cmd_in = 'jpegtopnm';
930 $cmd_out = 'ppmtojpeg';
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 break;
932 case 3 :
Andrey Andreev56454792012-05-17 14:32:19 +0300933 $cmd_in = 'pngtopnm';
934 $cmd_out = 'ppmtopng';
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 break;
936 }
937
Alex Bilbied261b1e2012-06-02 11:12:16 +0100938 if ($action === 'crop')
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
940 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
941 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100942 elseif ($action === 'rotate')
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 {
944 switch ($this->rotation_angle)
945 {
Andrey Andreev56454792012-05-17 14:32:19 +0300946 case 90: $angle = 'r270';
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300948 case 180: $angle = 'r180';
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300950 case 270: $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300952 case 'vrt': $angle = 'tb';
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300954 case 'hor': $angle = 'lr';
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 break;
956 }
957
958 $cmd_inner = 'pnmflip -'.$angle.' ';
959 }
960 else // Resize
961 {
962 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
963 }
964
965 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
966
967 $retval = 1;
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200968 // exec() might be disabled
969 if (function_usable('exec'))
970 {
971 @exec($cmd, $output, $retval);
972 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000973
Andrey Andreev8e70b792012-01-12 20:19:24 +0200974 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 if ($retval > 0)
976 {
977 $this->set_error('imglib_image_process_failed');
978 return FALSE;
979 }
980
981 // With NetPBM we have to create a temporary image.
982 // If you try manipulating the original it fails so
983 // we have to rename the temp file.
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200984 copy($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200985 unlink($this->dest_folder.'netpbm.tmp');
Andrey Andreev45965742014-08-27 20:40:11 +0300986 chmod($this->full_dst_path, $this->file_permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000987
988 return TRUE;
989 }
990
991 // --------------------------------------------------------------------
992
993 /**
994 * Image Rotate Using GD
995 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 * @return bool
997 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200998 public function image_rotate_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001000 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 if ( ! ($src_img = $this->image_create_gd()))
1002 {
1003 return FALSE;
1004 }
1005
1006 // Set the background color
1007 // This won't work with transparent PNG files so we are
1008 // going to have to figure out how to determine the color
1009 // of the alpha channel in a future release.
1010
Andrey Andreev3a197592015-05-21 01:05:06 +03001011 $white = imagecolorallocate($src_img, 255, 255, 255);
Derek Allard2067d1a2008-11-13 22:59:24 +00001012
Andrey Andreev8e70b792012-01-12 20:19:24 +02001013 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
1015
Andrey Andreev8e70b792012-01-12 20:19:24 +02001016 // Show the image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001017 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 {
1019 $this->image_display_gd($dst_img);
1020 }
Andrey Andreev8e70b792012-01-12 20:19:24 +02001021 elseif ( ! $this->image_save_gd($dst_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001023 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 }
1025
Andrey Andreev8e70b792012-01-12 20:19:24 +02001026 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 imagedestroy($dst_img);
1028 imagedestroy($src_img);
1029
Andrey Andreev45965742014-08-27 20:40:11 +03001030 chmod($this->full_dst_path, $this->file_permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +00001031
Pascal Kriete8761ef52011-02-14 13:13:52 -05001032 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 }
1034
1035 // --------------------------------------------------------------------
1036
1037 /**
1038 * Create Mirror Image using GD
1039 *
1040 * This function will flip horizontal or vertical
1041 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 * @return bool
1043 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001044 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
1046 if ( ! $src_img = $this->image_create_gd())
1047 {
1048 return FALSE;
1049 }
1050
Derek Jones4b9c6292011-07-01 17:40:48 -05001051 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 $height = $this->orig_height;
1053
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001054 if ($this->rotation_angle === 'hor')
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 {
Andrey Andreevfbde2792015-05-11 11:03:06 +03001056 for ($i = 0; $i < $height; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 {
Andrey Andreevfbde2792015-05-11 11:03:06 +03001058 $left = 0;
1059 $right = $width - 1;
1060
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 while ($left < $right)
1062 {
1063 $cl = imagecolorat($src_img, $left, $i);
1064 $cr = imagecolorat($src_img, $right, $i);
1065
1066 imagesetpixel($src_img, $left, $i, $cr);
1067 imagesetpixel($src_img, $right, $i, $cl);
1068
1069 $left++;
1070 $right--;
1071 }
1072 }
1073 }
1074 else
1075 {
Andrey Andreevfbde2792015-05-11 11:03:06 +03001076 for ($i = 0; $i < $width; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 {
Andrey Andreevfbde2792015-05-11 11:03:06 +03001078 $top = 0;
1079 $bottom = $height - 1;
1080
Andrey Andreev5d78fd82015-05-11 18:19:01 +03001081 while ($top < $bottom)
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 {
1083 $ct = imagecolorat($src_img, $i, $top);
Andrey Andreevfbde2792015-05-11 11:03:06 +03001084 $cb = imagecolorat($src_img, $i, $bottom);
Derek Allard2067d1a2008-11-13 22:59:24 +00001085
1086 imagesetpixel($src_img, $i, $top, $cb);
Andrey Andreevfbde2792015-05-11 11:03:06 +03001087 imagesetpixel($src_img, $i, $bottom, $ct);
Derek Allard2067d1a2008-11-13 22:59:24 +00001088
1089 $top++;
Andrey Andreevfbde2792015-05-11 11:03:06 +03001090 $bottom--;
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 }
1092 }
1093 }
1094
Andrey Andreev8e70b792012-01-12 20:19:24 +02001095 // Show the image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001096 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 {
1098 $this->image_display_gd($src_img);
1099 }
Andrey Andreev8e70b792012-01-12 20:19:24 +02001100 elseif ( ! $this->image_save_gd($src_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001102 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 }
1104
Andrey Andreev8e70b792012-01-12 20:19:24 +02001105 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 imagedestroy($src_img);
1107
Andrey Andreev45965742014-08-27 20:40:11 +03001108 chmod($this->full_dst_path, $this->file_permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +00001109
1110 return TRUE;
1111 }
1112
1113 // --------------------------------------------------------------------
1114
1115 /**
1116 * Image Watermark
1117 *
1118 * This is a wrapper function that chooses the type
1119 * of watermarking based on the specified preference.
1120 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 * @return bool
1122 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001123 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001125 return ($this->wm_type === 'overlay') ? $this->overlay_watermark() : $this->text_watermark();
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 }
1127
1128 // --------------------------------------------------------------------
1129
1130 /**
1131 * Watermark - Graphic Version
1132 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 * @return bool
1134 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001135 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
1137 if ( ! function_exists('imagecolortransparent'))
1138 {
1139 $this->set_error('imglib_gd_required');
1140 return FALSE;
1141 }
1142
Andrey Andreev8e70b792012-01-12 20:19:24 +02001143 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 $this->get_image_properties();
1145
Andrey Andreev8e70b792012-01-12 20:19:24 +02001146 // Fetch watermark image properties
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001147 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 $wm_img_type = $props['image_type'];
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001149 $wm_width = $props['width'];
1150 $wm_height = $props['height'];
Derek Allard2067d1a2008-11-13 22:59:24 +00001151
Andrey Andreev8e70b792012-01-12 20:19:24 +02001152 // Create two image resources
Derek Jones4b9c6292011-07-01 17:40:48 -05001153 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 $src_img = $this->image_create_gd($this->full_src_path);
1155
1156 // Reverse the offset if necessary
1157 // When the image is positioned at the bottom
1158 // we don't want the vertical offset to push it
Andrey Andreev8e70b792012-01-12 20:19:24 +02001159 // further down. We want the reverse, so we'll
1160 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 // offset when the image is at the right
1162
Andrey Andreev8e70b792012-01-12 20:19:24 +02001163 $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
1164 $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +00001165
Alex Bilbied261b1e2012-06-02 11:12:16 +01001166 if ($this->wm_vrt_alignment === 'B')
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1168
Alex Bilbied261b1e2012-06-02 11:12:16 +01001169 if ($this->wm_hor_alignment === 'R')
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1171
Andrey Andreev8e70b792012-01-12 20:19:24 +02001172 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1174 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1175
Andrey Andreev8e70b792012-01-12 20:19:24 +02001176 // Set the vertical position
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001177 if ($this->wm_vrt_alignment === 'M')
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001179 $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
1180 }
1181 elseif ($this->wm_vrt_alignment === 'B')
1182 {
1183 $y_axis += $this->orig_height - $wm_height;
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 }
1185
Andrey Andreev8e70b792012-01-12 20:19:24 +02001186 // Set the horizontal position
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001187 if ($this->wm_hor_alignment === 'C')
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001189 $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
1190 }
1191 elseif ($this->wm_hor_alignment === 'R')
1192 {
1193 $x_axis += $this->orig_width - $wm_width;
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 }
1195
Andrey Andreev10fb7d12015-08-03 10:05:29 +03001196 // Build the finalized image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001197 if ($wm_img_type === 3 && function_exists('imagealphablending'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 {
1199 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +02001200 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001201
1202 // Set RGB values for text and shadow
1203 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
1204 $alpha = ($rgba & 0x7F000000) >> 24;
1205
1206 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
1207 if ($alpha > 0)
1208 {
1209 // copy the image directly, the image's alpha transparency being the sole determinant of blending
1210 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
1211 }
1212 else
1213 {
1214 // set our RGB value from above to be transparent and merge the images with the specified opacity
1215 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
1216 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
1217 }
1218
Andrey Andreevdb037db2015-01-12 13:45:12 +02001219 // We can preserve transparency for PNG images
Дмитрийb23b8fc2014-10-20 00:36:55 +04001220 if ($this->image_type === 3)
1221 {
1222 imagealphablending($src_img, FALSE);
1223 imagesavealpha($src_img, TRUE);
1224 }
1225
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001226 // Output the image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001227 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 {
1229 $this->image_display_gd($src_img);
1230 }
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001231 elseif ( ! $this->image_save_gd($src_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001233 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 }
1235
1236 imagedestroy($src_img);
1237 imagedestroy($wm_img);
1238
1239 return TRUE;
1240 }
1241
1242 // --------------------------------------------------------------------
1243
1244 /**
1245 * Watermark - Text Version
1246 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 * @return bool
1248 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001249 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 {
1251 if ( ! ($src_img = $this->image_create_gd()))
1252 {
1253 return FALSE;
1254 }
1255
Alex Bilbied261b1e2012-06-02 11:12:16 +01001256 if ($this->wm_use_truetype === TRUE && ! file_exists($this->wm_font_path))
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 {
1258 $this->set_error('imglib_missing_font');
1259 return FALSE;
1260 }
1261
Andrey Andreev8e70b792012-01-12 20:19:24 +02001262 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 $this->get_image_properties();
1264
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 // Reverse the vertical offset
1266 // When the image is positioned at the bottom
1267 // we don't want the vertical offset to push it
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001268 // further down. We want the reverse, so we'll
Andrey Andreev8e70b792012-01-12 20:19:24 +02001269 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 // offset flips itself automatically
1271
Alex Bilbied261b1e2012-06-02 11:12:16 +01001272 if ($this->wm_vrt_alignment === 'B')
Andrey Andreeve52fc262014-02-11 13:27:01 +02001273 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
Andrey Andreeve52fc262014-02-11 13:27:01 +02001275 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001276
Alex Bilbied261b1e2012-06-02 11:12:16 +01001277 if ($this->wm_hor_alignment === 'R')
Andrey Andreeve52fc262014-02-11 13:27:01 +02001278 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 $this->wm_hor_offset = $this->wm_hor_offset * -1;
Andrey Andreeve52fc262014-02-11 13:27:01 +02001280 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001281
1282 // Set font width and height
1283 // These are calculated differently depending on
1284 // whether we are using the true type font or not
Alex Bilbied261b1e2012-06-02 11:12:16 +01001285 if ($this->wm_use_truetype === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 {
Andrey Andreeve52fc262014-02-11 13:27:01 +02001287 if (empty($this->wm_font_size))
Andrey Andreeva92b9032011-12-24 19:05:58 +02001288 {
1289 $this->wm_font_size = 17;
1290 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001291
Andrey Andreeve52fc262014-02-11 13:27:01 +02001292 if (function_exists('imagettfbbox'))
1293 {
1294 $temp = imagettfbbox($this->wm_font_size, 0, $this->wm_font_path, $this->wm_text);
1295 $temp = $temp[2] - $temp[0];
1296
1297 $fontwidth = $temp / strlen($this->wm_text);
1298 }
1299 else
1300 {
1301 $fontwidth = $this->wm_font_size - ($this->wm_font_size / 4);
1302 }
1303
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 $fontheight = $this->wm_font_size;
1305 $this->wm_vrt_offset += $this->wm_font_size;
1306 }
1307 else
1308 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001309 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 $fontheight = imagefontheight($this->wm_font_size);
1311 }
1312
1313 // Set base X and Y axis values
1314 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1315 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1316
Alex Bilbied261b1e2012-06-02 11:12:16 +01001317 if ($this->wm_use_drop_shadow === FALSE)
omar0779e992015-01-31 11:57:07 -07001318 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 $this->wm_shadow_distance = 0;
omar0779e992015-01-31 11:57:07 -07001320 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001321
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001322 $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
1323 $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +00001324
omar0779e992015-01-31 11:57:07 -07001325 // Set vertical alignment
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001326 if ($this->wm_vrt_alignment === 'M')
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001328 $y_axis += ($this->orig_height / 2) + ($fontheight / 2);
1329 }
1330 elseif ($this->wm_vrt_alignment === 'B')
1331 {
1332 $y_axis += $this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight / 2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 }
Andrey Andreev3a197592015-05-21 01:05:06 +03001334
omar0779e992015-01-31 11:57:07 -07001335 // Set horizontal alignment
1336 if ($this->wm_hor_alignment === 'R')
1337 {
omar9c855322015-01-31 11:58:03 -07001338 $x_axis += $this->orig_width - ($fontwidth * strlen($this->wm_text)) - $this->wm_shadow_distance;
omar0779e992015-01-31 11:57:07 -07001339 }
1340 elseif ($this->wm_hor_alignment === 'C')
1341 {
1342 $x_axis += floor(($this->orig_width - ($fontwidth * strlen($this->wm_text))) / 2);
1343 }
Andrey Andreev3a197592015-05-21 01:05:06 +03001344
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001345 if ($this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 {
omar0779e992015-01-31 11:57:07 -07001347 // Offset from text
1348 $x_shad = $x_axis + $this->wm_shadow_distance;
1349 $y_shad = $y_axis + $this->wm_shadow_distance;
Andrey Andreev3a197592015-05-21 01:05:06 +03001350
omar0779e992015-01-31 11:57:07 -07001351 /* Set RGB values for shadow
Andrey Andreev8323ae62011-12-31 18:39:10 +02001352 *
1353 * First character is #, so we don't really need it.
1354 * Get the rest of the string and split it into 2-length
1355 * hex values:
1356 */
Andrey Andreev8323ae62011-12-31 18:39:10 +02001357 $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2);
Ronald Beilsma8f80bc42012-01-12 16:41:49 +01001358 $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[1]), hexdec($drp_color[2]));
Andrey Andreev3a197592015-05-21 01:05:06 +03001359
omar0779e992015-01-31 11:57:07 -07001360 // Add the shadow to the source image
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001361 if ($this->wm_use_truetype)
1362 {
1363 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 +02001364 }
1365 else
1366 {
1367 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001368 }
omar0779e992015-01-31 11:57:07 -07001369 }
Andrey Andreev3a197592015-05-21 01:05:06 +03001370
omar0779e992015-01-31 11:57:07 -07001371 /* Set RGB values for text
1372 *
1373 * First character is #, so we don't really need it.
1374 * Get the rest of the string and split it into 2-length
1375 * hex values:
1376 */
1377 $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2);
1378 $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
Andrey Andreev99ae2262012-10-05 15:14:30 +03001379
omar0779e992015-01-31 11:57:07 -07001380 // Add the text to the source image
1381 if ($this->wm_use_truetype)
1382 {
1383 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1384 }
1385 else
1386 {
1387 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1388 }
Andrey Andreev3a197592015-05-21 01:05:06 +03001389
omar0779e992015-01-31 11:57:07 -07001390 // We can preserve transparency for PNG images
1391 if ($this->image_type === 3)
1392 {
1393 imagealphablending($src_img, FALSE);
1394 imagesavealpha($src_img, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 }
1396
Andrey Andreev8e70b792012-01-12 20:19:24 +02001397 // Output the final image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001398 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 {
1400 $this->image_display_gd($src_img);
1401 }
1402 else
1403 {
1404 $this->image_save_gd($src_img);
1405 }
1406
1407 imagedestroy($src_img);
1408
1409 return TRUE;
1410 }
1411
1412 // --------------------------------------------------------------------
1413
1414 /**
1415 * Create Image - GD
1416 *
1417 * This simply creates an image resource handle
1418 * based on the type of image being processed
1419 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001421 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001422 * @return resource
1423 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001424 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001425 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001426 if ($path === '')
Andrey Andreeve52fc262014-02-11 13:27:01 +02001427 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001428 $path = $this->full_src_path;
Andrey Andreeve52fc262014-02-11 13:27:01 +02001429 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001430
Alex Bilbied261b1e2012-06-02 11:12:16 +01001431 if ($image_type === '')
Andrey Andreeve52fc262014-02-11 13:27:01 +02001432 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001433 $image_type = $this->image_type;
Andrey Andreeve52fc262014-02-11 13:27:01 +02001434 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001435
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 switch ($image_type)
1437 {
Andrey Andreev3a197592015-05-21 01:05:06 +03001438 case 1:
Andrey Andreeve52fc262014-02-11 13:27:01 +02001439 if ( ! function_exists('imagecreatefromgif'))
1440 {
1441 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1442 return FALSE;
1443 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001444
Andrey Andreeve52fc262014-02-11 13:27:01 +02001445 return imagecreatefromgif($path);
Andrey Andreev3a197592015-05-21 01:05:06 +03001446 case 2:
Andrey Andreeve52fc262014-02-11 13:27:01 +02001447 if ( ! function_exists('imagecreatefromjpeg'))
1448 {
1449 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1450 return FALSE;
1451 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001452
Andrey Andreeve52fc262014-02-11 13:27:01 +02001453 return imagecreatefromjpeg($path);
Andrey Andreev3a197592015-05-21 01:05:06 +03001454 case 3:
Andrey Andreeve52fc262014-02-11 13:27:01 +02001455 if ( ! function_exists('imagecreatefrompng'))
1456 {
1457 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1458 return FALSE;
1459 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001460
Andrey Andreeve52fc262014-02-11 13:27:01 +02001461 return imagecreatefrompng($path);
1462 default:
1463 $this->set_error(array('imglib_unsupported_imagecreate'));
1464 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001466 }
1467
1468 // --------------------------------------------------------------------
1469
1470 /**
1471 * Write image file to disk - GD
1472 *
1473 * Takes an image resource as input and writes the file
1474 * to the specified destination
1475 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 * @param resource
1477 * @return bool
1478 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001479 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001480 {
1481 switch ($this->image_type)
1482 {
Andrey Andreev56454792012-05-17 14:32:19 +03001483 case 1:
1484 if ( ! function_exists('imagegif'))
1485 {
1486 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1487 return FALSE;
1488 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001489
Andrey Andreev56454792012-05-17 14:32:19 +03001490 if ( ! @imagegif($resource, $this->full_dst_path))
1491 {
1492 $this->set_error('imglib_save_failed');
1493 return FALSE;
1494 }
1495 break;
1496 case 2:
1497 if ( ! function_exists('imagejpeg'))
1498 {
1499 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1500 return FALSE;
1501 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001502
Andrey Andreev56454792012-05-17 14:32:19 +03001503 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1504 {
1505 $this->set_error('imglib_save_failed');
1506 return FALSE;
1507 }
1508 break;
1509 case 3:
1510 if ( ! function_exists('imagepng'))
1511 {
1512 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1513 return FALSE;
1514 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001515
Andrey Andreev56454792012-05-17 14:32:19 +03001516 if ( ! @imagepng($resource, $this->full_dst_path))
1517 {
1518 $this->set_error('imglib_save_failed');
1519 return FALSE;
1520 }
1521 break;
1522 default:
1523 $this->set_error(array('imglib_unsupported_imagecreate'));
1524 return FALSE;
1525 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 }
1527
1528 return TRUE;
1529 }
1530
1531 // --------------------------------------------------------------------
1532
1533 /**
1534 * Dynamically outputs an image
1535 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 * @param resource
1537 * @return void
1538 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001539 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001540 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001541 header('Content-Disposition: filename='.$this->source_image.';');
1542 header('Content-Type: '.$this->mime_type);
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 header('Content-Transfer-Encoding: binary');
1544 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1545
1546 switch ($this->image_type)
1547 {
Andrey Andreev56454792012-05-17 14:32:19 +03001548 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001549 break;
Ted Wood9cc707b2013-01-08 19:41:45 -08001550 case 2 : imagejpeg($resource, NULL, $this->quality);
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001552 case 3 : imagepng($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001554 default: echo 'Unable to display the image';
Derek Allard2067d1a2008-11-13 22:59:24 +00001555 break;
1556 }
1557 }
1558
1559 // --------------------------------------------------------------------
1560
1561 /**
1562 * Re-proportion Image Width/Height
1563 *
1564 * When creating thumbs, the desired width/height
1565 * can end up warping the image due to an incorrect
1566 * ratio between the full-sized image and the thumb.
1567 *
1568 * This function lets us re-proportion the width/height
1569 * if users choose to maintain the aspect ratio when resizing.
1570 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 * @return void
1572 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001573 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001574 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001575 if (($this->width === 0 && $this->height === 0) OR $this->orig_width === 0 OR $this->orig_height === 0
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001576 OR ( ! ctype_digit((string) $this->width) && ! ctype_digit((string) $this->height))
1577 OR ! ctype_digit((string) $this->orig_width) OR ! ctype_digit((string) $this->orig_height))
Derek Allard2067d1a2008-11-13 22:59:24 +00001578 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001579 return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001580 }
1581
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001582 // Sanitize
Andrey Andreev8e70b792012-01-12 20:19:24 +02001583 $this->width = (int) $this->width;
1584 $this->height = (int) $this->height;
1585
1586 if ($this->master_dim !== 'width' && $this->master_dim !== 'height')
Derek Allard2067d1a2008-11-13 22:59:24 +00001587 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001588 if ($this->width > 0 && $this->height > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001589 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001590 $this->master_dim = ((($this->orig_height/$this->orig_width) - ($this->height/$this->width)) < 0)
1591 ? 'width' : 'height';
Derek Allard2067d1a2008-11-13 22:59:24 +00001592 }
1593 else
1594 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001595 $this->master_dim = ($this->height === 0) ? 'width' : 'height';
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 }
1597 }
Andrey Andreev8e70b792012-01-12 20:19:24 +02001598 elseif (($this->master_dim === 'width' && $this->width === 0)
1599 OR ($this->master_dim === 'height' && $this->height === 0))
1600 {
1601 return;
1602 }
1603
1604 if ($this->master_dim === 'width')
1605 {
1606 $this->height = (int) ceil($this->width*$this->orig_height/$this->orig_width);
1607 }
1608 else
1609 {
1610 $this->width = (int) ceil($this->orig_width*$this->height/$this->orig_height);
1611 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 }
1613
1614 // --------------------------------------------------------------------
1615
1616 /**
1617 * Get image properties
1618 *
1619 * A helper function that gets info about the file
1620 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001622 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001623 * @return mixed
1624 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001625 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001626 {
1627 // For now we require GD but we should
1628 // find a way to determine this using IM or NetPBM
1629
Alex Bilbied261b1e2012-06-02 11:12:16 +01001630 if ($path === '')
Andrey Andreev8e70b792012-01-12 20:19:24 +02001631 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 $path = $this->full_src_path;
Andrey Andreev8e70b792012-01-12 20:19:24 +02001633 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001634
1635 if ( ! file_exists($path))
1636 {
1637 $this->set_error('imglib_invalid_path');
1638 return FALSE;
1639 }
1640
Phil Sturgeon901998a2011-08-26 10:03:33 +01001641 $vals = getimagesize($path);
Andrey Andreev747deff2017-01-06 13:22:16 +02001642 if ($vals === FALSE)
1643 {
1644 $this->set_error('imglib_invalid_image');
1645 return FALSE;
1646 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001647 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
Andrey Andreeva92b9032011-12-24 19:05:58 +02001648 $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001649
Alex Bilbied261b1e2012-06-02 11:12:16 +01001650 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001652 return array(
1653 'width' => $vals[0],
1654 'height' => $vals[1],
1655 'image_type' => $vals[2],
1656 'size_str' => $vals[3],
1657 'mime_type' => $mime
1658 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 }
1660
Andrey Andreeva92b9032011-12-24 19:05:58 +02001661 $this->orig_width = $vals[0];
1662 $this->orig_height = $vals[1];
1663 $this->image_type = $vals[2];
1664 $this->size_str = $vals[3];
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 $this->mime_type = $mime;
1666
1667 return TRUE;
1668 }
1669
1670 // --------------------------------------------------------------------
1671
1672 /**
1673 * Size calculator
1674 *
1675 * This function takes a known width x height and
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001676 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001677 * new variable needs to be known
1678 *
1679 * $props = array(
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001680 * 'width' => $width,
1681 * 'height' => $height,
1682 * 'new_width' => 40,
1683 * 'new_height' => ''
1684 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001686 * @param array
1687 * @return array
1688 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001689 public function size_calculator($vals)
Derek Allard2067d1a2008-11-13 22:59:24 +00001690 {
1691 if ( ! is_array($vals))
1692 {
1693 return;
1694 }
1695
1696 $allowed = array('new_width', 'new_height', 'width', 'height');
1697
1698 foreach ($allowed as $item)
1699 {
Andrey Andreev56454792012-05-17 14:32:19 +03001700 if (empty($vals[$item]))
1701 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001702 $vals[$item] = 0;
Andrey Andreev56454792012-05-17 14:32:19 +03001703 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001704 }
1705
Alex Bilbied261b1e2012-06-02 11:12:16 +01001706 if ($vals['width'] === 0 OR $vals['height'] === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001707 {
1708 return $vals;
1709 }
1710
Alex Bilbied261b1e2012-06-02 11:12:16 +01001711 if ($vals['new_width'] === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001712 {
1713 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1714 }
Alex Bilbied261b1e2012-06-02 11:12:16 +01001715 elseif ($vals['new_height'] === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 {
1717 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1718 }
1719
1720 return $vals;
1721 }
1722
1723 // --------------------------------------------------------------------
1724
1725 /**
1726 * Explode source_image
1727 *
1728 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001729 * from the source_image. This function lets us deal with
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001730 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001732 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 * $array['name'] = 'my.cool';
1734 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 * @param array
1736 * @return array
1737 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001738 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 {
Derek Jones08cae632009-02-10 20:03:29 +00001740 $ext = strrchr($source_image, '.');
1741 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001742
Derek Jones08cae632009-02-10 20:03:29 +00001743 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 }
1745
1746 // --------------------------------------------------------------------
1747
1748 /**
1749 * Is GD Installed?
1750 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 * @return bool
1752 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001753 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001754 {
1755 if ( ! extension_loaded('gd'))
1756 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001757 /* As it is stated in the PHP manual, dl() is not always available
1758 * and even if so - it could generate an E_WARNING message on failure
1759 */
Andrey Andreev8e70b792012-01-12 20:19:24 +02001760 return (function_exists('dl') && @dl('gd.so'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001761 }
1762
1763 return TRUE;
1764 }
1765
1766 // --------------------------------------------------------------------
1767
1768 /**
1769 * Get GD version
1770 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001771 * @return mixed
1772 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001773 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 {
1775 if (function_exists('gd_info'))
1776 {
1777 $gd_version = @gd_info();
Andrey Andreev8e70b792012-01-12 20:19:24 +02001778 return preg_replace('/\D/', '', $gd_version['GD Version']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 }
1780
1781 return FALSE;
1782 }
1783
1784 // --------------------------------------------------------------------
1785
1786 /**
1787 * Set error message
1788 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 * @param string
1790 * @return void
1791 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001792 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 {
1794 $CI =& get_instance();
1795 $CI->lang->load('imglib');
1796
1797 if (is_array($msg))
1798 {
1799 foreach ($msg as $val)
1800 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001801 $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 $this->error_msg[] = $msg;
1803 log_message('error', $msg);
1804 }
1805 }
1806 else
1807 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001808 $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg);
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 $this->error_msg[] = $msg;
1810 log_message('error', $msg);
1811 }
1812 }
1813
1814 // --------------------------------------------------------------------
1815
1816 /**
1817 * Show error messages
1818 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001819 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001820 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001821 * @return string
1822 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001823 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001824 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001825 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001826 }
1827
1828}