blob: ebd214ef07fbad50fb6ef2c51278e506db898bc4 [file] [log] [blame]
Andrey Andreeva92b9032011-12-24 19:05:58 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnesb1673362011-12-05 22:05:38 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnesb1673362011-12-05 22:05:38 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreeva3d19c42012-01-24 15:29:29 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Image Manipulation class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Image_lib
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/image_lib.html
36 */
37class CI_Image_lib {
38
Timothy Warrenb8e62852012-04-26 18:40:54 -040039 /**
40 * PHP extension/library to use for image manipulation
Andrey Andreev56454792012-05-17 14:32:19 +030041 * Can be: imagemagick, netpbm, gd, gd2
Timothy Warrenb8e62852012-04-26 18:40:54 -040042 *
43 * @var string
44 */
45 public $image_library = 'gd2';
Andrey Andreev56454792012-05-17 14:32:19 +030046
Timothy Warrenb8e62852012-04-26 18:40:54 -040047 /**
48 * Path to the graphic library (if applicable)
49 *
50 * @var string
51 */
Andrey Andreev3a459572011-12-21 11:23:11 +020052 public $library_path = '';
Andrey Andreev56454792012-05-17 14:32:19 +030053
Timothy Warrenb8e62852012-04-26 18:40:54 -040054 /**
55 * Whether to send to browser or write to disk
56 *
57 * @var bool
58 */
59 public $dynamic_output = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +030060
Timothy Warrenb8e62852012-04-26 18:40:54 -040061 /**
62 * Path to original image
63 *
64 * @var string
65 */
Andrey Andreev3a459572011-12-21 11:23:11 +020066 public $source_image = '';
Andrey Andreev56454792012-05-17 14:32:19 +030067
Timothy Warrenb8e62852012-04-26 18:40:54 -040068 /**
69 * Path to the modified image
70 *
71 * @var string
72 */
Andrey Andreev56454792012-05-17 14:32:19 +030073 public $new_image = '';
74
Timothy Warrenb8e62852012-04-26 18:40:54 -040075 /**
76 * Image width
77 *
78 * @var int
79 */
Andrey Andreev56454792012-05-17 14:32:19 +030080 public $width = '';
81
Timothy Warrenb8e62852012-04-26 18:40:54 -040082 /**
83 * Image height
84 *
85 * @var int
86 */
Andrey Andreev56454792012-05-17 14:32:19 +030087 public $height = '';
88
Timothy Warrenb8e62852012-04-26 18:40:54 -040089 /**
90 * Quality percentage of new image
91 *
92 * @var int
93 */
Andrey Andreev56454792012-05-17 14:32:19 +030094 public $quality = 90;
95
Timothy Warrenb8e62852012-04-26 18:40:54 -040096 /**
97 * Whether to create a thumbnail
98 *
99 * @var bool
100 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200101 public $create_thumb = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300102
Timothy Warrenb8e62852012-04-26 18:40:54 -0400103 /**
104 * String to add to thumbnail version of image
105 *
106 * @var string
107 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200108 public $thumb_marker = '_thumb';
Andrey Andreev56454792012-05-17 14:32:19 +0300109
Timothy Warrenb8e62852012-04-26 18:40:54 -0400110 /**
111 * Whether to maintain aspect ratio when resizing or use hard values
112 *
113 * @var bool
114 */
115 public $maintain_ratio = TRUE;
Andrey Andreev56454792012-05-17 14:32:19 +0300116
Timothy Warrenb8e62852012-04-26 18:40:54 -0400117 /**
118 * auto, height, or width. Determines what to use as the master dimension
119 *
120 * @var string
121 */
Andrey Andreev56454792012-05-17 14:32:19 +0300122 public $master_dim = 'auto';
123
Timothy Warrenb8e62852012-04-26 18:40:54 -0400124 /**
125 * Angle at to rotate image
126 *
127 * @var string
128 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200129 public $rotation_angle = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300130
Timothy Warrenb8e62852012-04-26 18:40:54 -0400131 /**
132 * X Coordinate for manipulation of the current image
133 *
134 * @var int
135 */
Andrey Andreev56454792012-05-17 14:32:19 +0300136 public $x_axis = '';
137
Timothy Warrenb8e62852012-04-26 18:40:54 -0400138 /**
139 * Y Coordinate for manipulation of the current image
140 *
141 * @var int
142 */
Andrey Andreev56454792012-05-17 14:32:19 +0300143 public $y_axis = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000144
Timothy Warrenb8e62852012-04-26 18:40:54 -0400145 // --------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // Watermark Vars
Timothy Warrenb8e62852012-04-26 18:40:54 -0400147 // --------------------------------------------------------------------------
Andrey Andreev56454792012-05-17 14:32:19 +0300148
Timothy Warrenb8e62852012-04-26 18:40:54 -0400149 /**
150 * Watermark text if graphic is not used
151 *
152 * @var string
153 */
154 public $wm_text = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300155
Timothy Warrenb8e62852012-04-26 18:40:54 -0400156 /**
157 * Type of watermarking. Options: text/overlay
158 *
159 * @var string
160 */
161 public $wm_type = 'text';
Andrey Andreev56454792012-05-17 14:32:19 +0300162
Timothy Warrenb8e62852012-04-26 18:40:54 -0400163 /**
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400164 * Default transparency for watermark
Andrey Andreev56454792012-05-17 14:32:19 +0300165 *
Timothy Warrenb8e62852012-04-26 18:40:54 -0400166 * @var int
167 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200168 public $wm_x_transp = 4;
Andrey Andreev56454792012-05-17 14:32:19 +0300169
Timothy Warrenb8e62852012-04-26 18:40:54 -0400170 /**
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400171 * Default transparency for watermark
172 *
Timothy Warrenb8e62852012-04-26 18:40:54 -0400173 * @var int
174 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200175 public $wm_y_transp = 4;
Andrey Andreev56454792012-05-17 14:32:19 +0300176
Timothy Warrenb8e62852012-04-26 18:40:54 -0400177 /**
178 * Watermark image path
Andrey Andreev56454792012-05-17 14:32:19 +0300179 *
Timothy Warrenb8e62852012-04-26 18:40:54 -0400180 * @var string
181 */
Andrey Andreev56454792012-05-17 14:32:19 +0300182 public $wm_overlay_path = '';
183
Timothy Warrenb8e62852012-04-26 18:40:54 -0400184 /**
185 * TT font
186 *
187 * @var string
188 */
189 public $wm_font_path = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300190
Timothy Warrenb8e62852012-04-26 18:40:54 -0400191 /**
192 * Font size (different versions of GD will either use points or pixels)
193 *
194 * @var int
195 */
196 public $wm_font_size = 17;
Andrey Andreev56454792012-05-17 14:32:19 +0300197
Timothy Warrenb8e62852012-04-26 18:40:54 -0400198 /**
199 * Vertical alignment: T M B
200 *
201 * @var string
202 */
203 public $wm_vrt_alignment = 'B';
Andrey Andreev56454792012-05-17 14:32:19 +0300204
Timothy Warrenb8e62852012-04-26 18:40:54 -0400205 /**
206 * Horizontal alignment: L R C
207 *
208 * @var string
209 */
210 public $wm_hor_alignment = 'C';
Andrey Andreev56454792012-05-17 14:32:19 +0300211
Timothy Warrenb8e62852012-04-26 18:40:54 -0400212 /**
213 * Padding around text
214 *
215 * @var int
216 */
217 public $wm_padding = 0;
Andrey Andreev56454792012-05-17 14:32:19 +0300218
Timothy Warrenb8e62852012-04-26 18:40:54 -0400219 /**
220 * Lets you push text to the right
221 *
222 * @var int
223 */
224 public $wm_hor_offset = 0;
Andrey Andreev56454792012-05-17 14:32:19 +0300225
Timothy Warrenb8e62852012-04-26 18:40:54 -0400226 /**
227 * Lets you push text down
228 *
229 * @var int
230 */
231 public $wm_vrt_offset = 0;
Andrey Andreev56454792012-05-17 14:32:19 +0300232
Timothy Warrenb8e62852012-04-26 18:40:54 -0400233 /**
234 * Text color
235 *
236 * @var string
237 */
Andrey Andreev56454792012-05-17 14:32:19 +0300238 protected $wm_font_color = '#ffffff';
239
Timothy Warrenb8e62852012-04-26 18:40:54 -0400240 /**
241 * Dropshadow color
242 *
243 * @var string
244 */
Andrey Andreev56454792012-05-17 14:32:19 +0300245 protected $wm_shadow_color = '';
246
Timothy Warrenb8e62852012-04-26 18:40:54 -0400247 /**
248 * Dropshadow distance
249 *
250 * @var int
251 */
252 public $wm_shadow_distance = 2;
Andrey Andreev56454792012-05-17 14:32:19 +0300253
Timothy Warrenb8e62852012-04-26 18:40:54 -0400254 /**
255 * Image opacity: 1 - 100 Only works with image
256 *
257 * @var int
258 */
Andrey Andreev56454792012-05-17 14:32:19 +0300259 public $wm_opacity = 50;
Derek Allard2067d1a2008-11-13 22:59:24 +0000260
Timothy Warrenb8e62852012-04-26 18:40:54 -0400261 // --------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // Private Vars
Timothy Warrenb8e62852012-04-26 18:40:54 -0400263 // --------------------------------------------------------------------------
Andrey Andreev56454792012-05-17 14:32:19 +0300264
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400265 /**
266 * Source image folder
267 *
268 * @var string
269 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200270 public $source_folder = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300271
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400272 /**
273 * Destination image folder
274 *
275 * @var string
276 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200277 public $dest_folder = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300278
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400279 /**
280 * Image mime-type
281 *
282 * @var string
283 */
Andrey Andreev56454792012-05-17 14:32:19 +0300284 public $mime_type = '';
285
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400286 /**
Andrey Andreev56454792012-05-17 14:32:19 +0300287 * Original image width
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400288 *
289 * @var int
290 */
Andrey Andreev56454792012-05-17 14:32:19 +0300291 public $orig_width = '';
292
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400293 /**
294 * Original image height
295 *
296 * @var int
297 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200298 public $orig_height = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300299
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400300 /**
301 * Image format
Andrey Andreev56454792012-05-17 14:32:19 +0300302 *
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400303 * @var string
304 */
Andrey Andreev56454792012-05-17 14:32:19 +0300305 public $image_type = '';
306
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400307 /**
308 * Size of current image
309 *
310 * @var string
311 */
Andrey Andreev56454792012-05-17 14:32:19 +0300312 public $size_str = '';
313
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400314 /**
315 * Full path to source image
316 *
317 * @var string
318 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200319 public $full_src_path = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300320
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400321 /**
322 * Full path to destination image
323 *
324 * @var string
325 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200326 public $full_dst_path = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300327
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400328 /**
329 * Name of function to create image
330 *
331 * @var string
332 */
Andrey Andreev56454792012-05-17 14:32:19 +0300333 public $create_fnc = 'imagecreatetruecolor';
334
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400335 /**
336 * Name of function to copy image
337 *
338 * @var string
339 */
Andrey Andreev56454792012-05-17 14:32:19 +0300340 public $copy_fnc = 'imagecopyresampled';
341
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400342 /**
343 * Error messages
344 *
345 * @var array
346 */
Andrey Andreev56454792012-05-17 14:32:19 +0300347 public $error_msg = array();
348
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400349 /**
350 * Whether to have a drop shadow on watermark
351 *
352 * @var bool
353 */
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200354 protected $wm_use_drop_shadow = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300355
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400356 /**
357 * Whether to use truetype fonts
358 *
359 * @var bool
360 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200361 public $wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000362
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400363 /**
364 * Initialize Image Library
365 *
366 * @param array $props
Andrey Andreev56454792012-05-17 14:32:19 +0300367 * @return void
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400368 */
Greg Akera9263282010-11-10 15:26:43 -0600369 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
371 if (count($props) > 0)
372 {
373 $this->initialize($props);
374 }
375
Andrey Andreev8e70b792012-01-12 20:19:24 +0200376 log_message('debug', 'Image Lib Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
378
379 // --------------------------------------------------------------------
380
381 /**
382 * Initialize image properties
383 *
384 * Resets values in case this class is used in a loop
385 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 * @return void
387 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200388 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
Michael Denniscb07a322011-08-20 23:40:59 -0700390 $props = array('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 +0000391
392 foreach ($props as $val)
393 {
394 $this->$val = '';
395 }
396
Michael Denniscb07a322011-08-20 23:40:59 -0700397 $this->image_library = 'gd2';
398 $this->dynamic_output = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300399 $this->quality = 90;
Michael Denniscb07a322011-08-20 23:40:59 -0700400 $this->create_thumb = FALSE;
401 $this->thumb_marker = '_thumb';
402 $this->maintain_ratio = TRUE;
403 $this->master_dim = 'auto';
404 $this->wm_type = 'text';
405 $this->wm_x_transp = 4;
406 $this->wm_y_transp = 4;
407 $this->wm_font_size = 17;
408 $this->wm_vrt_alignment = 'B';
409 $this->wm_hor_alignment = 'C';
410 $this->wm_padding = 0;
411 $this->wm_hor_offset = 0;
412 $this->wm_vrt_offset = 0;
413 $this->wm_font_color = '#ffffff';
414 $this->wm_shadow_distance = 2;
415 $this->wm_opacity = 50;
416 $this->create_fnc = 'imagecreatetruecolor';
417 $this->copy_fnc = 'imagecopyresampled';
418 $this->error_msg = array();
419 $this->wm_use_drop_shadow = FALSE;
420 $this->wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
422
423 // --------------------------------------------------------------------
424
425 /**
426 * initialize image preferences
427 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @param array
429 * @return bool
430 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200431 public function initialize($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200433 // Convert array elements into class variables
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 if (count($props) > 0)
435 {
436 foreach ($props as $key => $val)
437 {
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200438 if (property_exists($this, $key))
439 {
440 if (in_array($key, array('wm_font_color', 'wm_shadow_color')))
441 {
Andrey Andreev8323ae62011-12-31 18:39:10 +0200442 if (preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches))
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200443 {
Andrey Andreevcf2ba9e2012-01-01 21:57:13 +0200444 /* $matches[1] contains our hex color value, but it might be
Andrey Andreev8323ae62011-12-31 18:39:10 +0200445 * both in the full 6-length format or the shortened 3-length
446 * value.
Andrey Andreevbb96c8b2011-12-31 18:48:39 +0200447 * We'll later need the full version, so we keep it if it's
Andrey Andreev8323ae62011-12-31 18:39:10 +0200448 * already there and if not - we'll convert to it. We can
449 * access string characters by their index as in an array,
450 * so we'll do that and use concatenation to form the final
451 * value:
452 */
Andrey Andreev665af0c2011-12-30 14:39:29 +0200453 $val = (strlen($matches[1]) === 6)
454 ? '#'.$matches[1]
455 : '#'.$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 +0200456 }
457 else
458 {
459 continue;
460 }
461 }
462
463 $this->$key = $val;
464 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
466 }
467
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200468 // Is there a source image? If not, there's no reason to continue
Alex Bilbied261b1e2012-06-02 11:12:16 +0100469 if ($this->source_image === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
471 $this->set_error('imglib_source_image_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500472 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
474
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200475 /* Is getimagesize() available?
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 *
477 * We use it to determine the image properties (width/height).
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200478 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * properties using ImageMagick and NetPBM
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 */
481 if ( ! function_exists('getimagesize'))
482 {
483 $this->set_error('imglib_gd_required_for_props');
484 return FALSE;
485 }
486
487 $this->image_library = strtolower($this->image_library);
488
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200489 /* Set the full server path
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 *
491 * The source image may or may not contain a path.
492 * Either way, we'll try use realpath to generate the
493 * full server path in order to more reliably read it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 */
Andrey Andreev8e70b792012-01-12 20:19:24 +0200495 if (function_exists('realpath') && @realpath($this->source_image) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200497 $full_source_path = str_replace('\\', '/', realpath($this->source_image));
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
499 else
500 {
501 $full_source_path = $this->source_image;
502 }
503
504 $x = explode('/', $full_source_path);
505 $this->source_image = end($x);
506 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
507
508 // Set the Image Properties
509 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
510 {
Eric Barnesb1673362011-12-05 22:05:38 -0500511 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
513
514 /*
515 * Assign the "new" image name/path
516 *
517 * If the user has set a "new_image" name it means
518 * we are making a copy of the source image. If not
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200519 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * set the destination filename and path accordingly.
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100522 if ($this->new_image === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 $this->dest_image = $this->source_image;
525 $this->dest_folder = $this->source_folder;
526 }
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200527 elseif (strpos($this->new_image, '/') === FALSE)
528 {
529 $this->dest_folder = $this->source_folder;
530 $this->dest_image = $this->new_image;
531 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 else
533 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300534 if (strpos($this->new_image, '/') === FALSE && strpos($this->new_image, '\\') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200536 $full_dest_path = str_replace('\\', '/', realpath($this->new_image));
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
538 else
539 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200540 $full_dest_path = $this->new_image;
541 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000542
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200543 // Is there a file name?
544 if ( ! preg_match('#\.(jpg|jpeg|gif|png)$#i', $full_dest_path))
545 {
546 $this->dest_folder = $full_dest_path.'/';
547 $this->dest_image = $this->source_image;
548 }
549 else
550 {
551 $x = explode('/', $full_dest_path);
552 $this->dest_image = end($x);
553 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
555 }
556
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200557 /* Compile the finalized filenames/paths
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 *
559 * We'll create two master strings containing the
560 * full server path to the source image and the
561 * full server path to the destination image.
562 * We'll also split the destination image name
563 * so we can insert the thumbnail marker if needed.
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100565 if ($this->create_thumb === FALSE OR $this->thumb_marker === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 {
567 $this->thumb_marker = '';
568 }
569
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200570 $xp = $this->explode_name($this->dest_image);
Derek Allard2067d1a2008-11-13 22:59:24 +0000571
572 $filename = $xp['name'];
573 $file_ext = $xp['ext'];
574
575 $this->full_src_path = $this->source_folder.$this->source_image;
576 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
577
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200578 /* Should we maintain image proportions?
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 *
580 * When creating thumbs or copies, the target width/height
581 * might not be in correct proportion with the source
Andrey Andreev8e70b792012-01-12 20:19:24 +0200582 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100584 if ($this->maintain_ratio === TRUE && ($this->width !== 0 OR $this->height !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
586 $this->image_reproportion();
587 }
588
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200589 /* Was a width and height specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 *
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200591 * If the destination width/height was not submitted we
592 * will use the values from the actual file
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100594 if ($this->width === '')
Andrey Andreev8e70b792012-01-12 20:19:24 +0200595 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 $this->width = $this->orig_width;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200597 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000598
Alex Bilbied261b1e2012-06-02 11:12:16 +0100599 if ($this->height === '')
Andrey Andreev8e70b792012-01-12 20:19:24 +0200600 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 $this->height = $this->orig_height;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200602 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000603
604 // Set the quality
Andrey Andreev8e70b792012-01-12 20:19:24 +0200605 $this->quality = trim(str_replace('%', '', $this->quality));
Derek Allard2067d1a2008-11-13 22:59:24 +0000606
Alex Bilbied261b1e2012-06-02 11:12:16 +0100607 if ($this->quality === '' OR $this->quality === 0 OR ! preg_match('/^[0-9]+$/', $this->quality))
Andrey Andreev8e70b792012-01-12 20:19:24 +0200608 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 $this->quality = 90;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200610 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000611
612 // Set the x/y coordinates
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300613 is_numeric($this->x_axis) OR $this->x_axis = 0;
614 is_numeric($this->y_axis) OR $this->y_axis = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000615
616 // Watermark-related Stuff...
Alex Bilbied261b1e2012-06-02 11:12:16 +0100617 if ($this->wm_overlay_path !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200619 $this->wm_overlay_path = str_replace('\\', '/', realpath($this->wm_overlay_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 }
621
Alex Bilbied261b1e2012-06-02 11:12:16 +0100622 if ($this->wm_shadow_color !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 {
624 $this->wm_use_drop_shadow = TRUE;
625 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100626 elseif ($this->wm_use_drop_shadow === TRUE && $this->wm_shadow_color === '')
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200627 {
628 $this->wm_use_drop_shadow = FALSE;
629 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000630
Alex Bilbied261b1e2012-06-02 11:12:16 +0100631 if ($this->wm_font_path !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
633 $this->wm_use_truetype = TRUE;
634 }
635
636 return TRUE;
637 }
638
639 // --------------------------------------------------------------------
640
641 /**
642 * Image Resize
643 *
644 * This is a wrapper function that chooses the proper
645 * resize function based on the protocol specified
646 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 * @return bool
648 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200649 public function resize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 {
Andrey Andreev1a9f52c2012-01-07 01:06:34 +0200651 $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 return $this->$protocol('resize');
653 }
654
655 // --------------------------------------------------------------------
656
657 /**
658 * Image Crop
659 *
660 * This is a wrapper function that chooses the proper
661 * cropping 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 crop()
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 {
Andrey Andreev5a67e3d2012-01-07 01:21:09 +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('crop');
669 }
670
671 // --------------------------------------------------------------------
672
673 /**
674 * Image Rotate
675 *
676 * This is a wrapper function that chooses the proper
677 * rotation 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 rotate()
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
683 // Allowed rotation values
684 $degs = array(90, 180, 270, 'vrt', 'hor');
685
Alex Bilbied261b1e2012-06-02 11:12:16 +0100686 if ($this->rotation_angle === '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 {
688 $this->set_error('imglib_rotation_angle_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500689 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 }
691
692 // Reassign the width and height
Alex Bilbied261b1e2012-06-02 11:12:16 +0100693 if ($this->rotation_angle === 90 OR $this->rotation_angle === 270)
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 {
695 $this->width = $this->orig_height;
696 $this->height = $this->orig_width;
697 }
698 else
699 {
700 $this->width = $this->orig_width;
701 $this->height = $this->orig_height;
702 }
703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 // Choose resizing function
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200705 if ($this->image_library === 'imagemagick' OR $this->image_library === 'netpbm')
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 {
707 $protocol = 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 return $this->$protocol('rotate');
709 }
710
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200711 return ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
712 ? $this->image_mirror_gd()
713 : $this->image_rotate_gd();
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 }
715
716 // --------------------------------------------------------------------
717
718 /**
719 * Image Process Using GD/GD2
720 *
721 * This function will resize or crop
722 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 * @param string
724 * @return bool
725 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200726 public function image_process_gd($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 {
728 $v2_override = FALSE;
729
730 // If the target width/height match the source, AND if the new file name is not equal to the old file name
731 // 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 +0100732 if ($this->dynamic_output === FALSE && $this->orig_width === $this->width && $this->orig_height === $this->height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100734 if ($this->source_image !== $this->new_image && @copy($this->full_src_path, $this->full_dst_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200736 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200738
739 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 }
741
742 // Let's set up our values based on the action
Alex Bilbied261b1e2012-06-02 11:12:16 +0100743 if ($action === 'crop')
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200745 // Reassign the source width/height if cropping
Derek Jones4b9c6292011-07-01 17:40:48 -0500746 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 $this->orig_height = $this->height;
748
749 // GD 2.0 has a cropping bug so we'll test for it
750 if ($this->gd_version() !== FALSE)
751 {
752 $gd_version = str_replace('0', '', $this->gd_version());
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300753 $v2_override = ($gd_version === 2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 }
755 }
756 else
757 {
758 // If resizing the x/y axis must be zero
759 $this->x_axis = 0;
760 $this->y_axis = 0;
761 }
762
Derek Jones4b9c6292011-07-01 17:40:48 -0500763 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 if ( ! ($src_img = $this->image_create_gd()))
765 {
766 return FALSE;
767 }
768
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200769 /* Create the image
770 *
771 * Old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
772 * it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
773 * below should that ever prove inaccurate.
774 *
Alex Bilbied261b1e2012-06-02 11:12:16 +0100775 * if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor') && $v2_override === FALSE)
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200776 */
Andrey Andreev8e70b792012-01-12 20:19:24 +0200777 if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 {
779 $create = 'imagecreatetruecolor';
780 $copy = 'imagecopyresampled';
781 }
782 else
783 {
784 $create = 'imagecreate';
785 $copy = 'imagecopyresized';
786 }
787
788 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500789
Alex Bilbied261b1e2012-06-02 11:12:16 +0100790 if ($this->image_type === 3) // png we can actually preserve transparency
Derek Jones595bfd12010-08-20 10:28:22 -0500791 {
792 imagealphablending($dst_img, FALSE);
793 imagesavealpha($dst_img, TRUE);
794 }
Barry Mienydd671972010-10-04 16:33:58 +0200795
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
797
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200798 // Show the image
Alex Bilbied261b1e2012-06-02 11:12:16 +0100799 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 {
801 $this->image_display_gd($dst_img);
802 }
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200803 elseif ( ! $this->image_save_gd($dst_img)) // Or save it
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200805 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 }
807
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200808 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 imagedestroy($dst_img);
810 imagedestroy($src_img);
811
812 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000813 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000814
815 return TRUE;
816 }
817
818 // --------------------------------------------------------------------
819
820 /**
821 * Image Process Using ImageMagick
822 *
823 * This function will resize, crop or rotate
824 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 * @param string
826 * @return bool
827 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200828 public function image_process_imagemagick($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500830 // Do we have a vaild library path?
Alex Bilbied261b1e2012-06-02 11:12:16 +0100831 if ($this->library_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 {
833 $this->set_error('imglib_libpath_invalid');
834 return FALSE;
835 }
836
Andrey Andreev8e70b792012-01-12 20:19:24 +0200837 if ( ! preg_match('/convert$/i', $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200839 $this->library_path = rtrim($this->library_path, '/').'/convert';
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 }
841
842 // Execute the command
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200843 $cmd = $this->library_path.' -quality '.$this->quality;
Derek Allard2067d1a2008-11-13 22:59:24 +0000844
Alex Bilbied261b1e2012-06-02 11:12:16 +0100845 if ($action === 'crop')
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200847 $cmd .= ' -crop '.$this->width.'x'.$this->height.'+'.$this->x_axis.'+'.$this->y_axis.' "'.$this->full_src_path.'" "'.$this->full_dst_path .'" 2>&1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100849 elseif ($action === 'rotate')
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200851 $angle = ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
852 ? '-flop' : '-rotate '.$this->rotation_angle;
Derek Allard2067d1a2008-11-13 22:59:24 +0000853
Andrey Andreev8e70b792012-01-12 20:19:24 +0200854 $cmd .= ' '.$angle.' "'.$this->full_src_path.'" "'.$this->full_dst_path.'" 2>&1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 }
Andrey Andreev8e70b792012-01-12 20:19:24 +0200856 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 {
Omarbb531d62012-06-29 10:48:28 -0300858 if($this->maintain_ratio)
859 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
860 else
861 $cmd .= " -resize ".$this->width."x".$this->height."\\! \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 }
863
864 $retval = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 @exec($cmd, $output, $retval);
866
Andrey Andreev8e70b792012-01-12 20:19:24 +0200867 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 if ($retval > 0)
869 {
870 $this->set_error('imglib_image_process_failed');
871 return FALSE;
872 }
873
874 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000875 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000876
877 return TRUE;
878 }
879
880 // --------------------------------------------------------------------
881
882 /**
883 * Image Process Using NetPBM
884 *
885 * This function will resize, crop or rotate
886 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 * @param string
888 * @return bool
889 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200890 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100892 if ($this->library_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 {
894 $this->set_error('imglib_libpath_invalid');
895 return FALSE;
896 }
897
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200898 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 switch ($this->image_type)
900 {
901 case 1 :
Andrey Andreev56454792012-05-17 14:32:19 +0300902 $cmd_in = 'giftopnm';
903 $cmd_out = 'ppmtogif';
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 break;
905 case 2 :
Andrey Andreev56454792012-05-17 14:32:19 +0300906 $cmd_in = 'jpegtopnm';
907 $cmd_out = 'ppmtojpeg';
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 break;
909 case 3 :
Andrey Andreev56454792012-05-17 14:32:19 +0300910 $cmd_in = 'pngtopnm';
911 $cmd_out = 'ppmtopng';
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 break;
913 }
914
Alex Bilbied261b1e2012-06-02 11:12:16 +0100915 if ($action === 'crop')
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
917 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
918 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100919 elseif ($action === 'rotate')
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 {
921 switch ($this->rotation_angle)
922 {
Andrey Andreev56454792012-05-17 14:32:19 +0300923 case 90: $angle = 'r270';
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300925 case 180: $angle = 'r180';
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300927 case 270: $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300929 case 'vrt': $angle = 'tb';
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300931 case 'hor': $angle = 'lr';
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 break;
933 }
934
935 $cmd_inner = 'pnmflip -'.$angle.' ';
936 }
937 else // Resize
938 {
939 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
940 }
941
942 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
943
944 $retval = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 @exec($cmd, $output, $retval);
946
Andrey Andreev8e70b792012-01-12 20:19:24 +0200947 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 if ($retval > 0)
949 {
950 $this->set_error('imglib_image_process_failed');
951 return FALSE;
952 }
953
954 // With NetPBM we have to create a temporary image.
955 // If you try manipulating the original it fails so
956 // we have to rename the temp file.
957 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200958 unlink($this->dest_folder.'netpbm.tmp');
Derek Jones172e1612009-10-13 14:32:48 +0000959 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000960
961 return TRUE;
962 }
963
964 // --------------------------------------------------------------------
965
966 /**
967 * Image Rotate Using GD
968 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 * @return bool
970 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200971 public function image_rotate_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200973 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 if ( ! ($src_img = $this->image_create_gd()))
975 {
976 return FALSE;
977 }
978
979 // Set the background color
980 // This won't work with transparent PNG files so we are
981 // going to have to figure out how to determine the color
982 // of the alpha channel in a future release.
983
984 $white = imagecolorallocate($src_img, 255, 255, 255);
985
Andrey Andreev8e70b792012-01-12 20:19:24 +0200986 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
988
Andrey Andreev8e70b792012-01-12 20:19:24 +0200989 // Show the image
Alex Bilbied261b1e2012-06-02 11:12:16 +0100990 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 {
992 $this->image_display_gd($dst_img);
993 }
Andrey Andreev8e70b792012-01-12 20:19:24 +0200994 elseif ( ! $this->image_save_gd($dst_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200996 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
998
Andrey Andreev8e70b792012-01-12 20:19:24 +0200999 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 imagedestroy($dst_img);
1001 imagedestroy($src_img);
1002
1003 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +00001004 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001005
Pascal Kriete8761ef52011-02-14 13:13:52 -05001006 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 }
1008
1009 // --------------------------------------------------------------------
1010
1011 /**
1012 * Create Mirror Image using GD
1013 *
1014 * This function will flip horizontal or vertical
1015 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 * @return bool
1017 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001018 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 {
1020 if ( ! $src_img = $this->image_create_gd())
1021 {
1022 return FALSE;
1023 }
1024
Derek Jones4b9c6292011-07-01 17:40:48 -05001025 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 $height = $this->orig_height;
1027
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001028 if ($this->rotation_angle === 'hor')
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001030 for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 while ($left < $right)
1033 {
1034 $cl = imagecolorat($src_img, $left, $i);
1035 $cr = imagecolorat($src_img, $right, $i);
1036
1037 imagesetpixel($src_img, $left, $i, $cr);
1038 imagesetpixel($src_img, $right, $i, $cl);
1039
1040 $left++;
1041 $right--;
1042 }
1043 }
1044 }
1045 else
1046 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001047 for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 while ($top < $bot)
1050 {
1051 $ct = imagecolorat($src_img, $i, $top);
1052 $cb = imagecolorat($src_img, $i, $bot);
1053
1054 imagesetpixel($src_img, $i, $top, $cb);
1055 imagesetpixel($src_img, $i, $bot, $ct);
1056
1057 $top++;
1058 $bot--;
1059 }
1060 }
1061 }
1062
Andrey Andreev8e70b792012-01-12 20:19:24 +02001063 // Show the image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001064 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 {
1066 $this->image_display_gd($src_img);
1067 }
Andrey Andreev8e70b792012-01-12 20:19:24 +02001068 elseif ( ! $this->image_save_gd($src_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001070 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 }
1072
Andrey Andreev8e70b792012-01-12 20:19:24 +02001073 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 imagedestroy($src_img);
1075
1076 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +00001077 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001078
1079 return TRUE;
1080 }
1081
1082 // --------------------------------------------------------------------
1083
1084 /**
1085 * Image Watermark
1086 *
1087 * This is a wrapper function that chooses the type
1088 * of watermarking based on the specified preference.
1089 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 * @return bool
1091 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001092 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001094 return ($this->wm_type === 'overlay') ? $this->overlay_watermark() : $this->text_watermark();
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 }
1096
1097 // --------------------------------------------------------------------
1098
1099 /**
1100 * Watermark - Graphic Version
1101 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 * @return bool
1103 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001104 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 {
1106 if ( ! function_exists('imagecolortransparent'))
1107 {
1108 $this->set_error('imglib_gd_required');
1109 return FALSE;
1110 }
1111
Andrey Andreev8e70b792012-01-12 20:19:24 +02001112 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 $this->get_image_properties();
1114
Andrey Andreev8e70b792012-01-12 20:19:24 +02001115 // Fetch watermark image properties
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001116 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 $wm_img_type = $props['image_type'];
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001118 $wm_width = $props['width'];
1119 $wm_height = $props['height'];
Derek Allard2067d1a2008-11-13 22:59:24 +00001120
Andrey Andreev8e70b792012-01-12 20:19:24 +02001121 // Create two image resources
Derek Jones4b9c6292011-07-01 17:40:48 -05001122 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 $src_img = $this->image_create_gd($this->full_src_path);
1124
1125 // Reverse the offset if necessary
1126 // When the image is positioned at the bottom
1127 // we don't want the vertical offset to push it
Andrey Andreev8e70b792012-01-12 20:19:24 +02001128 // further down. We want the reverse, so we'll
1129 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 // offset when the image is at the right
1131
Andrey Andreev8e70b792012-01-12 20:19:24 +02001132 $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
1133 $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +00001134
Alex Bilbied261b1e2012-06-02 11:12:16 +01001135 if ($this->wm_vrt_alignment === 'B')
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1137
Alex Bilbied261b1e2012-06-02 11:12:16 +01001138 if ($this->wm_hor_alignment === 'R')
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1140
Andrey Andreev8e70b792012-01-12 20:19:24 +02001141 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1143 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1144
Andrey Andreev8e70b792012-01-12 20:19:24 +02001145 // Set the vertical position
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001146 if ($this->wm_vrt_alignment === 'M')
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001148 $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
1149 }
1150 elseif ($this->wm_vrt_alignment === 'B')
1151 {
1152 $y_axis += $this->orig_height - $wm_height;
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 }
1154
Andrey Andreev8e70b792012-01-12 20:19:24 +02001155 // Set the horizontal position
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001156 if ($this->wm_hor_alignment === 'C')
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001158 $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
1159 }
1160 elseif ($this->wm_hor_alignment === 'R')
1161 {
1162 $x_axis += $this->orig_width - $wm_width;
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 }
1164
Derek Jones4b9c6292011-07-01 17:40:48 -05001165 // Build the finalized image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001166 if ($wm_img_type === 3 && function_exists('imagealphablending'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 {
1168 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +02001169 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001170
1171 // Set RGB values for text and shadow
1172 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
1173 $alpha = ($rgba & 0x7F000000) >> 24;
1174
1175 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
1176 if ($alpha > 0)
1177 {
1178 // copy the image directly, the image's alpha transparency being the sole determinant of blending
1179 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
1180 }
1181 else
1182 {
1183 // set our RGB value from above to be transparent and merge the images with the specified opacity
1184 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
1185 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
1186 }
1187
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001188 // Output the image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001189 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 {
1191 $this->image_display_gd($src_img);
1192 }
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001193 elseif ( ! $this->image_save_gd($src_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001195 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 }
1197
1198 imagedestroy($src_img);
1199 imagedestroy($wm_img);
1200
1201 return TRUE;
1202 }
1203
1204 // --------------------------------------------------------------------
1205
1206 /**
1207 * Watermark - Text Version
1208 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 * @return bool
1210 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001211 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 {
1213 if ( ! ($src_img = $this->image_create_gd()))
1214 {
1215 return FALSE;
1216 }
1217
Alex Bilbied261b1e2012-06-02 11:12:16 +01001218 if ($this->wm_use_truetype === TRUE && ! file_exists($this->wm_font_path))
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 {
1220 $this->set_error('imglib_missing_font');
1221 return FALSE;
1222 }
1223
Andrey Andreev8e70b792012-01-12 20:19:24 +02001224 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 $this->get_image_properties();
1226
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 // Reverse the vertical offset
1228 // When the image is positioned at the bottom
1229 // we don't want the vertical offset to push it
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001230 // further down. We want the reverse, so we'll
Andrey Andreev8e70b792012-01-12 20:19:24 +02001231 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 // offset flips itself automatically
1233
Alex Bilbied261b1e2012-06-02 11:12:16 +01001234 if ($this->wm_vrt_alignment === 'B')
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1236
Alex Bilbied261b1e2012-06-02 11:12:16 +01001237 if ($this->wm_hor_alignment === 'R')
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1239
1240 // Set font width and height
1241 // These are calculated differently depending on
1242 // whether we are using the true type font or not
Alex Bilbied261b1e2012-06-02 11:12:16 +01001243 if ($this->wm_use_truetype === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001245 if ($this->wm_font_size === '')
Andrey Andreeva92b9032011-12-24 19:05:58 +02001246 {
1247 $this->wm_font_size = 17;
1248 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001249
Derek Jones4b9c6292011-07-01 17:40:48 -05001250 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 $fontheight = $this->wm_font_size;
1252 $this->wm_vrt_offset += $this->wm_font_size;
1253 }
1254 else
1255 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001256 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 $fontheight = imagefontheight($this->wm_font_size);
1258 }
1259
1260 // Set base X and Y axis values
1261 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1262 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1263
Alex Bilbied261b1e2012-06-02 11:12:16 +01001264 if ($this->wm_use_drop_shadow === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 $this->wm_shadow_distance = 0;
1266
1267 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1268 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1269
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001270 // Set verticle alignment
1271 if ($this->wm_vrt_alignment === 'M')
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001273 $y_axis += ($this->orig_height / 2) + ($fontheight / 2);
1274 }
1275 elseif ($this->wm_vrt_alignment === 'B')
1276 {
1277 $y_axis += $this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight / 2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 }
1279
1280 $x_shad = $x_axis + $this->wm_shadow_distance;
1281 $y_shad = $y_axis + $this->wm_shadow_distance;
1282
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001283 if ($this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001285 // Set horizontal alignment
1286 if ($this->wm_hor_alignment === 'R')
1287 {
1288 $x_shad += $this->orig_width - ($fontwidth * strlen($this->wm_text));
1289 $x_axis += $this->orig_width - ($fontwidth * strlen($this->wm_text));
1290 }
1291 elseif ($this->wm_hor_alignment === 'C')
1292 {
1293 $x_shad += floor(($this->orig_width - ($fontwidth * strlen($this->wm_text))) / 2);
1294 $x_axis += floor(($this->orig_width - ($fontwidth * strlen($this->wm_text))) / 2);
1295 }
1296
Andrey Andreev8323ae62011-12-31 18:39:10 +02001297 /* Set RGB values for text and shadow
1298 *
1299 * First character is #, so we don't really need it.
1300 * Get the rest of the string and split it into 2-length
1301 * hex values:
1302 */
1303 $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2);
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001304 $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
Andrey Andreev8323ae62011-12-31 18:39:10 +02001305 $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2);
Ronald Beilsma8f80bc42012-01-12 16:41:49 +01001306 $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[1]), hexdec($drp_color[2]));
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001307
Andrey Andreev8e70b792012-01-12 20:19:24 +02001308 // Add the text to the source image
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001309 if ($this->wm_use_truetype)
1310 {
1311 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1312 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1313 }
1314 else
1315 {
1316 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1317 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1318 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 }
1320
Andrey Andreev8e70b792012-01-12 20:19:24 +02001321 // Output the final image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001322 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 {
1324 $this->image_display_gd($src_img);
1325 }
1326 else
1327 {
1328 $this->image_save_gd($src_img);
1329 }
1330
1331 imagedestroy($src_img);
1332
1333 return TRUE;
1334 }
1335
1336 // --------------------------------------------------------------------
1337
1338 /**
1339 * Create Image - GD
1340 *
1341 * This simply creates an image resource handle
1342 * based on the type of image being processed
1343 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001345 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 * @return resource
1347 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001348 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001350 if ($path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 $path = $this->full_src_path;
1352
Alex Bilbied261b1e2012-06-02 11:12:16 +01001353 if ($image_type === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 $image_type = $this->image_type;
1355
1356
1357 switch ($image_type)
1358 {
1359 case 1 :
1360 if ( ! function_exists('imagecreatefromgif'))
1361 {
1362 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1363 return FALSE;
1364 }
1365
1366 return imagecreatefromgif($path);
1367 break;
1368 case 2 :
1369 if ( ! function_exists('imagecreatefromjpeg'))
1370 {
1371 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1372 return FALSE;
1373 }
1374
1375 return imagecreatefromjpeg($path);
1376 break;
1377 case 3 :
1378 if ( ! function_exists('imagecreatefrompng'))
1379 {
1380 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1381 return FALSE;
1382 }
1383
1384 return imagecreatefrompng($path);
1385 break;
1386
1387 }
1388
1389 $this->set_error(array('imglib_unsupported_imagecreate'));
1390 return FALSE;
1391 }
1392
1393 // --------------------------------------------------------------------
1394
1395 /**
1396 * Write image file to disk - GD
1397 *
1398 * Takes an image resource as input and writes the file
1399 * to the specified destination
1400 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 * @param resource
1402 * @return bool
1403 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001404 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 {
1406 switch ($this->image_type)
1407 {
Andrey Andreev56454792012-05-17 14:32:19 +03001408 case 1:
1409 if ( ! function_exists('imagegif'))
1410 {
1411 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1412 return FALSE;
1413 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001414
Andrey Andreev56454792012-05-17 14:32:19 +03001415 if ( ! @imagegif($resource, $this->full_dst_path))
1416 {
1417 $this->set_error('imglib_save_failed');
1418 return FALSE;
1419 }
1420 break;
1421 case 2:
1422 if ( ! function_exists('imagejpeg'))
1423 {
1424 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1425 return FALSE;
1426 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001427
Andrey Andreev56454792012-05-17 14:32:19 +03001428 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1429 {
1430 $this->set_error('imglib_save_failed');
1431 return FALSE;
1432 }
1433 break;
1434 case 3:
1435 if ( ! function_exists('imagepng'))
1436 {
1437 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1438 return FALSE;
1439 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001440
Andrey Andreev56454792012-05-17 14:32:19 +03001441 if ( ! @imagepng($resource, $this->full_dst_path))
1442 {
1443 $this->set_error('imglib_save_failed');
1444 return FALSE;
1445 }
1446 break;
1447 default:
1448 $this->set_error(array('imglib_unsupported_imagecreate'));
1449 return FALSE;
1450 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001451 }
1452
1453 return TRUE;
1454 }
1455
1456 // --------------------------------------------------------------------
1457
1458 /**
1459 * Dynamically outputs an image
1460 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001461 * @param resource
1462 * @return void
1463 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001464 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001466 header('Content-Disposition: filename='.$this->source_image.';');
1467 header('Content-Type: '.$this->mime_type);
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 header('Content-Transfer-Encoding: binary');
1469 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1470
1471 switch ($this->image_type)
1472 {
Andrey Andreev56454792012-05-17 14:32:19 +03001473 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001475 case 2 : imagejpeg($resource, '', $this->quality);
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001477 case 3 : imagepng($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001478 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001479 default: echo 'Unable to display the image';
Derek Allard2067d1a2008-11-13 22:59:24 +00001480 break;
1481 }
1482 }
1483
1484 // --------------------------------------------------------------------
1485
1486 /**
1487 * Re-proportion Image Width/Height
1488 *
1489 * When creating thumbs, the desired width/height
1490 * can end up warping the image due to an incorrect
1491 * ratio between the full-sized image and the thumb.
1492 *
1493 * This function lets us re-proportion the width/height
1494 * if users choose to maintain the aspect ratio when resizing.
1495 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 * @return void
1497 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001498 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001500 if (($this->width === 0 && $this->height === 0) OR $this->orig_width === 0 OR $this->orig_height === 0
Andrey Andreev8e70b792012-01-12 20:19:24 +02001501 OR ( ! preg_match('/^[0-9]+$/', $this->width) && ! preg_match('/^[0-9]+$/', $this->height))
1502 OR ! preg_match('/^[0-9]+$/', $this->orig_width) OR ! preg_match('/^[0-9]+$/', $this->orig_height))
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001504 return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001505 }
1506
Andrey Andreev8e70b792012-01-12 20:19:24 +02001507 // Sanitize so we don't call preg_match() anymore
1508 $this->width = (int) $this->width;
1509 $this->height = (int) $this->height;
1510
1511 if ($this->master_dim !== 'width' && $this->master_dim !== 'height')
Derek Allard2067d1a2008-11-13 22:59:24 +00001512 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001513 if ($this->width > 0 && $this->height > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001514 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001515 $this->master_dim = ((($this->orig_height/$this->orig_width) - ($this->height/$this->width)) < 0)
1516 ? 'width' : 'height';
Derek Allard2067d1a2008-11-13 22:59:24 +00001517 }
1518 else
1519 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001520 $this->master_dim = ($this->height === 0) ? 'width' : 'height';
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 }
1522 }
Andrey Andreev8e70b792012-01-12 20:19:24 +02001523 elseif (($this->master_dim === 'width' && $this->width === 0)
1524 OR ($this->master_dim === 'height' && $this->height === 0))
1525 {
1526 return;
1527 }
1528
1529 if ($this->master_dim === 'width')
1530 {
1531 $this->height = (int) ceil($this->width*$this->orig_height/$this->orig_width);
1532 }
1533 else
1534 {
1535 $this->width = (int) ceil($this->orig_width*$this->height/$this->orig_height);
1536 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 }
1538
1539 // --------------------------------------------------------------------
1540
1541 /**
1542 * Get image properties
1543 *
1544 * A helper function that gets info about the file
1545 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001547 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 * @return mixed
1549 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001550 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 {
1552 // For now we require GD but we should
1553 // find a way to determine this using IM or NetPBM
1554
Alex Bilbied261b1e2012-06-02 11:12:16 +01001555 if ($path === '')
Andrey Andreev8e70b792012-01-12 20:19:24 +02001556 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001557 $path = $this->full_src_path;
Andrey Andreev8e70b792012-01-12 20:19:24 +02001558 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001559
1560 if ( ! file_exists($path))
1561 {
1562 $this->set_error('imglib_invalid_path');
1563 return FALSE;
1564 }
1565
Phil Sturgeon901998a2011-08-26 10:03:33 +01001566 $vals = getimagesize($path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001567 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
Andrey Andreeva92b9032011-12-24 19:05:58 +02001568 $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001569
Alex Bilbied261b1e2012-06-02 11:12:16 +01001570 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001572 return array(
1573 'width' => $vals[0],
1574 'height' => $vals[1],
1575 'image_type' => $vals[2],
1576 'size_str' => $vals[3],
1577 'mime_type' => $mime
1578 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001579 }
1580
Andrey Andreeva92b9032011-12-24 19:05:58 +02001581 $this->orig_width = $vals[0];
1582 $this->orig_height = $vals[1];
1583 $this->image_type = $vals[2];
1584 $this->size_str = $vals[3];
Derek Allard2067d1a2008-11-13 22:59:24 +00001585 $this->mime_type = $mime;
1586
1587 return TRUE;
1588 }
1589
1590 // --------------------------------------------------------------------
1591
1592 /**
1593 * Size calculator
1594 *
1595 * This function takes a known width x height and
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001596 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 * new variable needs to be known
1598 *
1599 * $props = array(
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001600 * 'width' => $width,
1601 * 'height' => $height,
1602 * 'new_width' => 40,
1603 * 'new_height' => ''
1604 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 * @param array
1607 * @return array
1608 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001609 public function size_calculator($vals)
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 {
1611 if ( ! is_array($vals))
1612 {
1613 return;
1614 }
1615
1616 $allowed = array('new_width', 'new_height', 'width', 'height');
1617
1618 foreach ($allowed as $item)
1619 {
Andrey Andreev56454792012-05-17 14:32:19 +03001620 if (empty($vals[$item]))
1621 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001622 $vals[$item] = 0;
Andrey Andreev56454792012-05-17 14:32:19 +03001623 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001624 }
1625
Alex Bilbied261b1e2012-06-02 11:12:16 +01001626 if ($vals['width'] === 0 OR $vals['height'] === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001627 {
1628 return $vals;
1629 }
1630
Alex Bilbied261b1e2012-06-02 11:12:16 +01001631 if ($vals['new_width'] === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 {
1633 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1634 }
Alex Bilbied261b1e2012-06-02 11:12:16 +01001635 elseif ($vals['new_height'] === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 {
1637 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1638 }
1639
1640 return $vals;
1641 }
1642
1643 // --------------------------------------------------------------------
1644
1645 /**
1646 * Explode source_image
1647 *
1648 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001649 * from the source_image. This function lets us deal with
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001650 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001652 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001653 * $array['name'] = 'my.cool';
1654 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001655 * @param array
1656 * @return array
1657 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001658 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 {
Derek Jones08cae632009-02-10 20:03:29 +00001660 $ext = strrchr($source_image, '.');
1661 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001662
Derek Jones08cae632009-02-10 20:03:29 +00001663 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001664 }
1665
1666 // --------------------------------------------------------------------
1667
1668 /**
1669 * Is GD Installed?
1670 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 * @return bool
1672 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001673 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 {
1675 if ( ! extension_loaded('gd'))
1676 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001677 /* As it is stated in the PHP manual, dl() is not always available
1678 * and even if so - it could generate an E_WARNING message on failure
1679 */
Andrey Andreev8e70b792012-01-12 20:19:24 +02001680 return (function_exists('dl') && @dl('gd.so'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001681 }
1682
1683 return TRUE;
1684 }
1685
1686 // --------------------------------------------------------------------
1687
1688 /**
1689 * Get GD version
1690 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 * @return mixed
1692 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001693 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001694 {
1695 if (function_exists('gd_info'))
1696 {
1697 $gd_version = @gd_info();
Andrey Andreev8e70b792012-01-12 20:19:24 +02001698 return preg_replace('/\D/', '', $gd_version['GD Version']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 }
1700
1701 return FALSE;
1702 }
1703
1704 // --------------------------------------------------------------------
1705
1706 /**
1707 * Set error message
1708 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001709 * @param string
1710 * @return void
1711 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001712 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001713 {
1714 $CI =& get_instance();
1715 $CI->lang->load('imglib');
1716
1717 if (is_array($msg))
1718 {
1719 foreach ($msg as $val)
1720 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001721 $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001722 $this->error_msg[] = $msg;
1723 log_message('error', $msg);
1724 }
1725 }
1726 else
1727 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001728 $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg);
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 $this->error_msg[] = $msg;
1730 log_message('error', $msg);
1731 }
1732 }
1733
1734 // --------------------------------------------------------------------
1735
1736 /**
1737 * Show error messages
1738 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001740 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 * @return string
1742 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001743 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001745 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 }
1747
1748}
Derek Allard2067d1a2008-11-13 22:59:24 +00001749
1750/* End of file Image_lib.php */
Andrey Andreev1b815532012-04-03 16:06:03 +03001751/* Location: ./system/libraries/Image_lib.php */