blob: 0cb1894455175012a2b5f8a7a33647768bf6c431 [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
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 if ($this->source_image == '')
470 {
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 */
522 if ($this->new_image == '')
523 {
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 */
565 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
566 {
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 */
Andrey Andreev8e70b792012-01-12 20:19:24 +0200584 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 */
594 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
599 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
Andrey Andreev8e70b792012-01-12 20:19:24 +0200607 if ($this->quality == '' OR $this->quality == 0 OR ! preg_match('/^[0-9]+$/', $this->quality))
608 {
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 Andreev8e70b792012-01-12 20:19:24 +0200613 $this->x_axis = ($this->x_axis == '' OR ! preg_match('/^[0-9]+$/', $this->x_axis)) ? 0 : $this->x_axis;
614 $this->y_axis = ($this->y_axis == '' OR ! preg_match('/^[0-9]+$/', $this->y_axis)) ? 0 : $this->y_axis;
Derek Allard2067d1a2008-11-13 22:59:24 +0000615
616 // Watermark-related Stuff...
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 if ($this->wm_overlay_path != '')
618 {
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
622 if ($this->wm_shadow_color != '')
623 {
624 $this->wm_use_drop_shadow = TRUE;
625 }
Andrey Andreev8e70b792012-01-12 20:19:24 +0200626 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
631 if ($this->wm_font_path != '')
632 {
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
Derek Allardd9c7f032008-12-01 20:18:00 +0000686 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
693 if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
694 {
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.
Andrey Andreev8e70b792012-01-12 20:19:24 +0200732 if ($this->dynamic_output === FALSE && $this->orig_width == $this->width && $this->orig_height == $this->height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200734 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
743 if ($action == 'crop')
744 {
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());
753 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
754 }
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 *
775 * if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor') && $v2_override == FALSE)
776 */
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
790 if ($this->image_type == 3) // png we can actually preserve transparency
791 {
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 if ($this->dynamic_output == TRUE)
800 {
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?
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 if ($this->library_path == '')
832 {
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
845 if ($action == 'crop')
846 {
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 }
849 elseif ($action == 'rotate')
850 {
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 {
Andrey Andreeve4636302012-01-12 20:23:27 +0200858 $cmd .= ' -resize '.$this->width.'x'.$this->height.' "'.$this->full_src_path.'" "'.$this->full_dst_path.'" 2>&1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 }
860
861 $retval = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 @exec($cmd, $output, $retval);
863
Andrey Andreev8e70b792012-01-12 20:19:24 +0200864 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 if ($retval > 0)
866 {
867 $this->set_error('imglib_image_process_failed');
868 return FALSE;
869 }
870
871 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000872 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000873
874 return TRUE;
875 }
876
877 // --------------------------------------------------------------------
878
879 /**
880 * Image Process Using NetPBM
881 *
882 * This function will resize, crop or rotate
883 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * @param string
885 * @return bool
886 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200887 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
889 if ($this->library_path == '')
890 {
891 $this->set_error('imglib_libpath_invalid');
892 return FALSE;
893 }
894
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200895 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 switch ($this->image_type)
897 {
898 case 1 :
Andrey Andreev56454792012-05-17 14:32:19 +0300899 $cmd_in = 'giftopnm';
900 $cmd_out = 'ppmtogif';
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 break;
902 case 2 :
Andrey Andreev56454792012-05-17 14:32:19 +0300903 $cmd_in = 'jpegtopnm';
904 $cmd_out = 'ppmtojpeg';
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 break;
906 case 3 :
Andrey Andreev56454792012-05-17 14:32:19 +0300907 $cmd_in = 'pngtopnm';
908 $cmd_out = 'ppmtopng';
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 break;
910 }
911
912 if ($action == 'crop')
913 {
914 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
915 }
916 elseif ($action == 'rotate')
917 {
918 switch ($this->rotation_angle)
919 {
Andrey Andreev56454792012-05-17 14:32:19 +0300920 case 90: $angle = 'r270';
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300922 case 180: $angle = 'r180';
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300924 case 270: $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300926 case 'vrt': $angle = 'tb';
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300928 case 'hor': $angle = 'lr';
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 break;
930 }
931
932 $cmd_inner = 'pnmflip -'.$angle.' ';
933 }
934 else // Resize
935 {
936 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
937 }
938
939 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
940
941 $retval = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 @exec($cmd, $output, $retval);
943
Andrey Andreev8e70b792012-01-12 20:19:24 +0200944 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 if ($retval > 0)
946 {
947 $this->set_error('imglib_image_process_failed');
948 return FALSE;
949 }
950
951 // With NetPBM we have to create a temporary image.
952 // If you try manipulating the original it fails so
953 // we have to rename the temp file.
954 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200955 unlink($this->dest_folder.'netpbm.tmp');
Derek Jones172e1612009-10-13 14:32:48 +0000956 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000957
958 return TRUE;
959 }
960
961 // --------------------------------------------------------------------
962
963 /**
964 * Image Rotate Using GD
965 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 * @return bool
967 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200968 public function image_rotate_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200970 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 if ( ! ($src_img = $this->image_create_gd()))
972 {
973 return FALSE;
974 }
975
976 // Set the background color
977 // This won't work with transparent PNG files so we are
978 // going to have to figure out how to determine the color
979 // of the alpha channel in a future release.
980
981 $white = imagecolorallocate($src_img, 255, 255, 255);
982
Andrey Andreev8e70b792012-01-12 20:19:24 +0200983 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
985
Andrey Andreev8e70b792012-01-12 20:19:24 +0200986 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 if ($this->dynamic_output == TRUE)
988 {
989 $this->image_display_gd($dst_img);
990 }
Andrey Andreev8e70b792012-01-12 20:19:24 +0200991 elseif ( ! $this->image_save_gd($dst_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200993 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 }
995
Andrey Andreev8e70b792012-01-12 20:19:24 +0200996 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 imagedestroy($dst_img);
998 imagedestroy($src_img);
999
1000 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +00001001 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001002
Pascal Kriete8761ef52011-02-14 13:13:52 -05001003 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 }
1005
1006 // --------------------------------------------------------------------
1007
1008 /**
1009 * Create Mirror Image using GD
1010 *
1011 * This function will flip horizontal or vertical
1012 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 * @return bool
1014 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001015 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 {
1017 if ( ! $src_img = $this->image_create_gd())
1018 {
1019 return FALSE;
1020 }
1021
Derek Jones4b9c6292011-07-01 17:40:48 -05001022 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 $height = $this->orig_height;
1024
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001025 if ($this->rotation_angle === 'hor')
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001027 for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 while ($left < $right)
1030 {
1031 $cl = imagecolorat($src_img, $left, $i);
1032 $cr = imagecolorat($src_img, $right, $i);
1033
1034 imagesetpixel($src_img, $left, $i, $cr);
1035 imagesetpixel($src_img, $right, $i, $cl);
1036
1037 $left++;
1038 $right--;
1039 }
1040 }
1041 }
1042 else
1043 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001044 for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 while ($top < $bot)
1047 {
1048 $ct = imagecolorat($src_img, $i, $top);
1049 $cb = imagecolorat($src_img, $i, $bot);
1050
1051 imagesetpixel($src_img, $i, $top, $cb);
1052 imagesetpixel($src_img, $i, $bot, $ct);
1053
1054 $top++;
1055 $bot--;
1056 }
1057 }
1058 }
1059
Andrey Andreev8e70b792012-01-12 20:19:24 +02001060 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 if ($this->dynamic_output == TRUE)
1062 {
1063 $this->image_display_gd($src_img);
1064 }
Andrey Andreev8e70b792012-01-12 20:19:24 +02001065 elseif ( ! $this->image_save_gd($src_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001067 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 }
1069
Andrey Andreev8e70b792012-01-12 20:19:24 +02001070 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 imagedestroy($src_img);
1072
1073 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +00001074 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001075
1076 return TRUE;
1077 }
1078
1079 // --------------------------------------------------------------------
1080
1081 /**
1082 * Image Watermark
1083 *
1084 * This is a wrapper function that chooses the type
1085 * of watermarking based on the specified preference.
1086 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 * @return bool
1088 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001089 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001091 return ($this->wm_type === 'overlay') ? $this->overlay_watermark() : $this->text_watermark();
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 }
1093
1094 // --------------------------------------------------------------------
1095
1096 /**
1097 * Watermark - Graphic Version
1098 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 * @return bool
1100 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001101 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 {
1103 if ( ! function_exists('imagecolortransparent'))
1104 {
1105 $this->set_error('imglib_gd_required');
1106 return FALSE;
1107 }
1108
Andrey Andreev8e70b792012-01-12 20:19:24 +02001109 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 $this->get_image_properties();
1111
Andrey Andreev8e70b792012-01-12 20:19:24 +02001112 // Fetch watermark image properties
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001113 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 $wm_img_type = $props['image_type'];
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001115 $wm_width = $props['width'];
1116 $wm_height = $props['height'];
Derek Allard2067d1a2008-11-13 22:59:24 +00001117
Andrey Andreev8e70b792012-01-12 20:19:24 +02001118 // Create two image resources
Derek Jones4b9c6292011-07-01 17:40:48 -05001119 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 $src_img = $this->image_create_gd($this->full_src_path);
1121
1122 // Reverse the offset if necessary
1123 // When the image is positioned at the bottom
1124 // we don't want the vertical offset to push it
Andrey Andreev8e70b792012-01-12 20:19:24 +02001125 // further down. We want the reverse, so we'll
1126 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 // offset when the image is at the right
1128
Andrey Andreev8e70b792012-01-12 20:19:24 +02001129 $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
1130 $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +00001131
1132 if ($this->wm_vrt_alignment == 'B')
1133 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1134
1135 if ($this->wm_hor_alignment == 'R')
1136 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1137
Andrey Andreev8e70b792012-01-12 20:19:24 +02001138 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1140 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1141
Andrey Andreev8e70b792012-01-12 20:19:24 +02001142 // Set the vertical position
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001143 if ($this->wm_vrt_alignment === 'M')
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001145 $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
1146 }
1147 elseif ($this->wm_vrt_alignment === 'B')
1148 {
1149 $y_axis += $this->orig_height - $wm_height;
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 }
1151
Andrey Andreev8e70b792012-01-12 20:19:24 +02001152 // Set the horizontal position
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001153 if ($this->wm_hor_alignment === 'C')
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001155 $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
1156 }
1157 elseif ($this->wm_hor_alignment === 'R')
1158 {
1159 $x_axis += $this->orig_width - $wm_width;
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 }
1161
Derek Jones4b9c6292011-07-01 17:40:48 -05001162 // Build the finalized image
Andrey Andreev8e70b792012-01-12 20:19:24 +02001163 if ($wm_img_type == 3 && function_exists('imagealphablending'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 {
1165 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +02001166 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001167
1168 // Set RGB values for text and shadow
1169 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
1170 $alpha = ($rgba & 0x7F000000) >> 24;
1171
1172 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
1173 if ($alpha > 0)
1174 {
1175 // copy the image directly, the image's alpha transparency being the sole determinant of blending
1176 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
1177 }
1178 else
1179 {
1180 // set our RGB value from above to be transparent and merge the images with the specified opacity
1181 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
1182 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
1183 }
1184
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001185 // Output the image
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 if ($this->dynamic_output == TRUE)
1187 {
1188 $this->image_display_gd($src_img);
1189 }
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001190 elseif ( ! $this->image_save_gd($src_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001192 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 }
1194
1195 imagedestroy($src_img);
1196 imagedestroy($wm_img);
1197
1198 return TRUE;
1199 }
1200
1201 // --------------------------------------------------------------------
1202
1203 /**
1204 * Watermark - Text Version
1205 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 * @return bool
1207 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001208 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 {
1210 if ( ! ($src_img = $this->image_create_gd()))
1211 {
1212 return FALSE;
1213 }
1214
Andrey Andreev8e70b792012-01-12 20:19:24 +02001215 if ($this->wm_use_truetype == TRUE && ! file_exists($this->wm_font_path))
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 {
1217 $this->set_error('imglib_missing_font');
1218 return FALSE;
1219 }
1220
Andrey Andreev8e70b792012-01-12 20:19:24 +02001221 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 $this->get_image_properties();
1223
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 // Reverse the vertical offset
1225 // When the image is positioned at the bottom
1226 // we don't want the vertical offset to push it
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001227 // further down. We want the reverse, so we'll
Andrey Andreev8e70b792012-01-12 20:19:24 +02001228 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 // offset flips itself automatically
1230
1231 if ($this->wm_vrt_alignment == 'B')
1232 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1233
1234 if ($this->wm_hor_alignment == 'R')
1235 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1236
1237 // Set font width and height
1238 // These are calculated differently depending on
1239 // whether we are using the true type font or not
1240 if ($this->wm_use_truetype == TRUE)
1241 {
1242 if ($this->wm_font_size == '')
Andrey Andreeva92b9032011-12-24 19:05:58 +02001243 {
1244 $this->wm_font_size = 17;
1245 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001246
Derek Jones4b9c6292011-07-01 17:40:48 -05001247 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 $fontheight = $this->wm_font_size;
1249 $this->wm_vrt_offset += $this->wm_font_size;
1250 }
1251 else
1252 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001253 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 $fontheight = imagefontheight($this->wm_font_size);
1255 }
1256
1257 // Set base X and Y axis values
1258 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1259 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1260
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 if ($this->wm_use_drop_shadow == FALSE)
1262 $this->wm_shadow_distance = 0;
1263
1264 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1265 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1266
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001267 // Set verticle alignment
1268 if ($this->wm_vrt_alignment === 'M')
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001270 $y_axis += ($this->orig_height / 2) + ($fontheight / 2);
1271 }
1272 elseif ($this->wm_vrt_alignment === 'B')
1273 {
1274 $y_axis += $this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight / 2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 }
1276
1277 $x_shad = $x_axis + $this->wm_shadow_distance;
1278 $y_shad = $y_axis + $this->wm_shadow_distance;
1279
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001280 if ($this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001282 // Set horizontal alignment
1283 if ($this->wm_hor_alignment === 'R')
1284 {
1285 $x_shad += $this->orig_width - ($fontwidth * strlen($this->wm_text));
1286 $x_axis += $this->orig_width - ($fontwidth * strlen($this->wm_text));
1287 }
1288 elseif ($this->wm_hor_alignment === 'C')
1289 {
1290 $x_shad += floor(($this->orig_width - ($fontwidth * strlen($this->wm_text))) / 2);
1291 $x_axis += floor(($this->orig_width - ($fontwidth * strlen($this->wm_text))) / 2);
1292 }
1293
Andrey Andreev8323ae62011-12-31 18:39:10 +02001294 /* Set RGB values for text and shadow
1295 *
1296 * First character is #, so we don't really need it.
1297 * Get the rest of the string and split it into 2-length
1298 * hex values:
1299 */
1300 $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2);
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001301 $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
Andrey Andreev8323ae62011-12-31 18:39:10 +02001302 $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2);
Ronald Beilsma8f80bc42012-01-12 16:41:49 +01001303 $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[1]), hexdec($drp_color[2]));
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001304
Andrey Andreev8e70b792012-01-12 20:19:24 +02001305 // Add the text to the source image
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001306 if ($this->wm_use_truetype)
1307 {
1308 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1309 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1310 }
1311 else
1312 {
1313 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1314 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1315 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001316 }
1317
Andrey Andreev8e70b792012-01-12 20:19:24 +02001318 // Output the final image
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 if ($this->dynamic_output == TRUE)
1320 {
1321 $this->image_display_gd($src_img);
1322 }
1323 else
1324 {
1325 $this->image_save_gd($src_img);
1326 }
1327
1328 imagedestroy($src_img);
1329
1330 return TRUE;
1331 }
1332
1333 // --------------------------------------------------------------------
1334
1335 /**
1336 * Create Image - GD
1337 *
1338 * This simply creates an image resource handle
1339 * based on the type of image being processed
1340 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001342 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 * @return resource
1344 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001345 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 {
1347 if ($path == '')
1348 $path = $this->full_src_path;
1349
1350 if ($image_type == '')
1351 $image_type = $this->image_type;
1352
1353
1354 switch ($image_type)
1355 {
1356 case 1 :
1357 if ( ! function_exists('imagecreatefromgif'))
1358 {
1359 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1360 return FALSE;
1361 }
1362
1363 return imagecreatefromgif($path);
1364 break;
1365 case 2 :
1366 if ( ! function_exists('imagecreatefromjpeg'))
1367 {
1368 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1369 return FALSE;
1370 }
1371
1372 return imagecreatefromjpeg($path);
1373 break;
1374 case 3 :
1375 if ( ! function_exists('imagecreatefrompng'))
1376 {
1377 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1378 return FALSE;
1379 }
1380
1381 return imagecreatefrompng($path);
1382 break;
1383
1384 }
1385
1386 $this->set_error(array('imglib_unsupported_imagecreate'));
1387 return FALSE;
1388 }
1389
1390 // --------------------------------------------------------------------
1391
1392 /**
1393 * Write image file to disk - GD
1394 *
1395 * Takes an image resource as input and writes the file
1396 * to the specified destination
1397 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 * @param resource
1399 * @return bool
1400 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001401 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001402 {
1403 switch ($this->image_type)
1404 {
Andrey Andreev56454792012-05-17 14:32:19 +03001405 case 1:
1406 if ( ! function_exists('imagegif'))
1407 {
1408 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1409 return FALSE;
1410 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001411
Andrey Andreev56454792012-05-17 14:32:19 +03001412 if ( ! @imagegif($resource, $this->full_dst_path))
1413 {
1414 $this->set_error('imglib_save_failed');
1415 return FALSE;
1416 }
1417 break;
1418 case 2:
1419 if ( ! function_exists('imagejpeg'))
1420 {
1421 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1422 return FALSE;
1423 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001424
Andrey Andreev56454792012-05-17 14:32:19 +03001425 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1426 {
1427 $this->set_error('imglib_save_failed');
1428 return FALSE;
1429 }
1430 break;
1431 case 3:
1432 if ( ! function_exists('imagepng'))
1433 {
1434 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1435 return FALSE;
1436 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001437
Andrey Andreev56454792012-05-17 14:32:19 +03001438 if ( ! @imagepng($resource, $this->full_dst_path))
1439 {
1440 $this->set_error('imglib_save_failed');
1441 return FALSE;
1442 }
1443 break;
1444 default:
1445 $this->set_error(array('imglib_unsupported_imagecreate'));
1446 return FALSE;
1447 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 }
1449
1450 return TRUE;
1451 }
1452
1453 // --------------------------------------------------------------------
1454
1455 /**
1456 * Dynamically outputs an image
1457 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 * @param resource
1459 * @return void
1460 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001461 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001462 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001463 header('Content-Disposition: filename='.$this->source_image.';');
1464 header('Content-Type: '.$this->mime_type);
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 header('Content-Transfer-Encoding: binary');
1466 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1467
1468 switch ($this->image_type)
1469 {
Andrey Andreev56454792012-05-17 14:32:19 +03001470 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001472 case 2 : imagejpeg($resource, '', $this->quality);
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001474 case 3 : imagepng($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001475 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001476 default: echo 'Unable to display the image';
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 break;
1478 }
1479 }
1480
1481 // --------------------------------------------------------------------
1482
1483 /**
1484 * Re-proportion Image Width/Height
1485 *
1486 * When creating thumbs, the desired width/height
1487 * can end up warping the image due to an incorrect
1488 * ratio between the full-sized image and the thumb.
1489 *
1490 * This function lets us re-proportion the width/height
1491 * if users choose to maintain the aspect ratio when resizing.
1492 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 * @return void
1494 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001495 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001497 if (($this->width == 0 && $this->height == 0) OR $this->orig_width == 0 OR $this->orig_height == 0
1498 OR ( ! preg_match('/^[0-9]+$/', $this->width) && ! preg_match('/^[0-9]+$/', $this->height))
1499 OR ! preg_match('/^[0-9]+$/', $this->orig_width) OR ! preg_match('/^[0-9]+$/', $this->orig_height))
Derek Allard2067d1a2008-11-13 22:59:24 +00001500 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001501 return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 }
1503
Andrey Andreev8e70b792012-01-12 20:19:24 +02001504 // Sanitize so we don't call preg_match() anymore
1505 $this->width = (int) $this->width;
1506 $this->height = (int) $this->height;
1507
1508 if ($this->master_dim !== 'width' && $this->master_dim !== 'height')
Derek Allard2067d1a2008-11-13 22:59:24 +00001509 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001510 if ($this->width > 0 && $this->height > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001511 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001512 $this->master_dim = ((($this->orig_height/$this->orig_width) - ($this->height/$this->width)) < 0)
1513 ? 'width' : 'height';
Derek Allard2067d1a2008-11-13 22:59:24 +00001514 }
1515 else
1516 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001517 $this->master_dim = ($this->height === 0) ? 'width' : 'height';
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 }
1519 }
Andrey Andreev8e70b792012-01-12 20:19:24 +02001520 elseif (($this->master_dim === 'width' && $this->width === 0)
1521 OR ($this->master_dim === 'height' && $this->height === 0))
1522 {
1523 return;
1524 }
1525
1526 if ($this->master_dim === 'width')
1527 {
1528 $this->height = (int) ceil($this->width*$this->orig_height/$this->orig_width);
1529 }
1530 else
1531 {
1532 $this->width = (int) ceil($this->orig_width*$this->height/$this->orig_height);
1533 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 }
1535
1536 // --------------------------------------------------------------------
1537
1538 /**
1539 * Get image properties
1540 *
1541 * A helper function that gets info about the file
1542 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001544 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001545 * @return mixed
1546 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001547 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 {
1549 // For now we require GD but we should
1550 // find a way to determine this using IM or NetPBM
1551
1552 if ($path == '')
Andrey Andreev8e70b792012-01-12 20:19:24 +02001553 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001554 $path = $this->full_src_path;
Andrey Andreev8e70b792012-01-12 20:19:24 +02001555 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001556
1557 if ( ! file_exists($path))
1558 {
1559 $this->set_error('imglib_invalid_path');
1560 return FALSE;
1561 }
1562
Phil Sturgeon901998a2011-08-26 10:03:33 +01001563 $vals = getimagesize($path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001564 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
Andrey Andreeva92b9032011-12-24 19:05:58 +02001565 $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001566
1567 if ($return == TRUE)
1568 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001569 return array(
1570 'width' => $vals[0],
1571 'height' => $vals[1],
1572 'image_type' => $vals[2],
1573 'size_str' => $vals[3],
1574 'mime_type' => $mime
1575 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001576 }
1577
Andrey Andreeva92b9032011-12-24 19:05:58 +02001578 $this->orig_width = $vals[0];
1579 $this->orig_height = $vals[1];
1580 $this->image_type = $vals[2];
1581 $this->size_str = $vals[3];
Derek Allard2067d1a2008-11-13 22:59:24 +00001582 $this->mime_type = $mime;
1583
1584 return TRUE;
1585 }
1586
1587 // --------------------------------------------------------------------
1588
1589 /**
1590 * Size calculator
1591 *
1592 * This function takes a known width x height and
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001593 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 * new variable needs to be known
1595 *
1596 * $props = array(
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001597 * 'width' => $width,
1598 * 'height' => $height,
1599 * 'new_width' => 40,
1600 * 'new_height' => ''
1601 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001602 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 * @param array
1604 * @return array
1605 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001606 public function size_calculator($vals)
Derek Allard2067d1a2008-11-13 22:59:24 +00001607 {
1608 if ( ! is_array($vals))
1609 {
1610 return;
1611 }
1612
1613 $allowed = array('new_width', 'new_height', 'width', 'height');
1614
1615 foreach ($allowed as $item)
1616 {
Andrey Andreev56454792012-05-17 14:32:19 +03001617 if (empty($vals[$item]))
1618 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 $vals[$item] = 0;
Andrey Andreev56454792012-05-17 14:32:19 +03001620 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 }
1622
1623 if ($vals['width'] == 0 OR $vals['height'] == 0)
1624 {
1625 return $vals;
1626 }
1627
1628 if ($vals['new_width'] == 0)
1629 {
1630 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1631 }
1632 elseif ($vals['new_height'] == 0)
1633 {
1634 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1635 }
1636
1637 return $vals;
1638 }
1639
1640 // --------------------------------------------------------------------
1641
1642 /**
1643 * Explode source_image
1644 *
1645 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001646 * from the source_image. This function lets us deal with
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001647 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001648 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001649 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 * $array['name'] = 'my.cool';
1651 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 * @param array
1653 * @return array
1654 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001655 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001656 {
Derek Jones08cae632009-02-10 20:03:29 +00001657 $ext = strrchr($source_image, '.');
1658 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001659
Derek Jones08cae632009-02-10 20:03:29 +00001660 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 }
1662
1663 // --------------------------------------------------------------------
1664
1665 /**
1666 * Is GD Installed?
1667 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001668 * @return bool
1669 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001670 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 {
1672 if ( ! extension_loaded('gd'))
1673 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001674 /* As it is stated in the PHP manual, dl() is not always available
1675 * and even if so - it could generate an E_WARNING message on failure
1676 */
Andrey Andreev8e70b792012-01-12 20:19:24 +02001677 return (function_exists('dl') && @dl('gd.so'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 }
1679
1680 return TRUE;
1681 }
1682
1683 // --------------------------------------------------------------------
1684
1685 /**
1686 * Get GD version
1687 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001688 * @return mixed
1689 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001690 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 {
1692 if (function_exists('gd_info'))
1693 {
1694 $gd_version = @gd_info();
Andrey Andreev8e70b792012-01-12 20:19:24 +02001695 return preg_replace('/\D/', '', $gd_version['GD Version']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001696 }
1697
1698 return FALSE;
1699 }
1700
1701 // --------------------------------------------------------------------
1702
1703 /**
1704 * Set error message
1705 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001706 * @param string
1707 * @return void
1708 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001709 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001710 {
1711 $CI =& get_instance();
1712 $CI->lang->load('imglib');
1713
1714 if (is_array($msg))
1715 {
1716 foreach ($msg as $val)
1717 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001718 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1719 $this->error_msg[] = $msg;
1720 log_message('error', $msg);
1721 }
1722 }
1723 else
1724 {
1725 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1726 $this->error_msg[] = $msg;
1727 log_message('error', $msg);
1728 }
1729 }
1730
1731 // --------------------------------------------------------------------
1732
1733 /**
1734 * Show error messages
1735 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001736 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001737 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001738 * @return string
1739 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001740 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001742 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001743 }
1744
1745}
Derek Allard2067d1a2008-11-13 22:59:24 +00001746
1747/* End of file Image_lib.php */
Andrey Andreev1b815532012-04-03 16:06:03 +03001748/* Location: ./system/libraries/Image_lib.php */