blob: 3b453be47f39fcc8378ab5d09cf2b0019d05befc [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Image Manipulation class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Image_lib
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/libraries/image_lib.html
37 */
38class CI_Image_lib {
39
Timothy Warrenb8e62852012-04-26 18:40:54 -040040 /**
41 * PHP extension/library to use for image manipulation
Andrey Andreev56454792012-05-17 14:32:19 +030042 * Can be: imagemagick, netpbm, gd, gd2
Timothy Warrenb8e62852012-04-26 18:40:54 -040043 *
44 * @var string
45 */
46 public $image_library = 'gd2';
Andrey Andreev56454792012-05-17 14:32:19 +030047
Timothy Warrenb8e62852012-04-26 18:40:54 -040048 /**
49 * Path to the graphic library (if applicable)
50 *
51 * @var string
52 */
Andrey Andreev3a459572011-12-21 11:23:11 +020053 public $library_path = '';
Andrey Andreev56454792012-05-17 14:32:19 +030054
Timothy Warrenb8e62852012-04-26 18:40:54 -040055 /**
56 * Whether to send to browser or write to disk
57 *
58 * @var bool
59 */
60 public $dynamic_output = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +030061
Timothy Warrenb8e62852012-04-26 18:40:54 -040062 /**
63 * Path to original image
64 *
65 * @var string
66 */
Andrey Andreev3a459572011-12-21 11:23:11 +020067 public $source_image = '';
Andrey Andreev56454792012-05-17 14:32:19 +030068
Timothy Warrenb8e62852012-04-26 18:40:54 -040069 /**
70 * Path to the modified image
71 *
72 * @var string
73 */
Andrey Andreev56454792012-05-17 14:32:19 +030074 public $new_image = '';
75
Timothy Warrenb8e62852012-04-26 18:40:54 -040076 /**
77 * Image width
78 *
79 * @var int
80 */
Andrey Andreev56454792012-05-17 14:32:19 +030081 public $width = '';
82
Timothy Warrenb8e62852012-04-26 18:40:54 -040083 /**
84 * Image height
85 *
86 * @var int
87 */
Andrey Andreev56454792012-05-17 14:32:19 +030088 public $height = '';
89
Timothy Warrenb8e62852012-04-26 18:40:54 -040090 /**
91 * Quality percentage of new image
92 *
93 * @var int
94 */
Andrey Andreev56454792012-05-17 14:32:19 +030095 public $quality = 90;
96
Timothy Warrenb8e62852012-04-26 18:40:54 -040097 /**
98 * Whether to create a thumbnail
99 *
100 * @var bool
101 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200102 public $create_thumb = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300103
Timothy Warrenb8e62852012-04-26 18:40:54 -0400104 /**
105 * String to add to thumbnail version of image
106 *
107 * @var string
108 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200109 public $thumb_marker = '_thumb';
Andrey Andreev56454792012-05-17 14:32:19 +0300110
Timothy Warrenb8e62852012-04-26 18:40:54 -0400111 /**
112 * Whether to maintain aspect ratio when resizing or use hard values
113 *
114 * @var bool
115 */
116 public $maintain_ratio = TRUE;
Andrey Andreev56454792012-05-17 14:32:19 +0300117
Timothy Warrenb8e62852012-04-26 18:40:54 -0400118 /**
119 * auto, height, or width. Determines what to use as the master dimension
120 *
121 * @var string
122 */
Andrey Andreev56454792012-05-17 14:32:19 +0300123 public $master_dim = 'auto';
124
Timothy Warrenb8e62852012-04-26 18:40:54 -0400125 /**
126 * Angle at to rotate image
127 *
128 * @var string
129 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200130 public $rotation_angle = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300131
Timothy Warrenb8e62852012-04-26 18:40:54 -0400132 /**
133 * X Coordinate for manipulation of the current image
134 *
135 * @var int
136 */
Andrey Andreev56454792012-05-17 14:32:19 +0300137 public $x_axis = '';
138
Timothy Warrenb8e62852012-04-26 18:40:54 -0400139 /**
140 * Y Coordinate for manipulation of the current image
141 *
142 * @var int
143 */
Andrey Andreev56454792012-05-17 14:32:19 +0300144 public $y_axis = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000145
Timothy Warrenb8e62852012-04-26 18:40:54 -0400146 // --------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // Watermark Vars
Timothy Warrenb8e62852012-04-26 18:40:54 -0400148 // --------------------------------------------------------------------------
Andrey Andreev56454792012-05-17 14:32:19 +0300149
Timothy Warrenb8e62852012-04-26 18:40:54 -0400150 /**
151 * Watermark text if graphic is not used
152 *
153 * @var string
154 */
155 public $wm_text = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300156
Timothy Warrenb8e62852012-04-26 18:40:54 -0400157 /**
158 * Type of watermarking. Options: text/overlay
159 *
160 * @var string
161 */
162 public $wm_type = 'text';
Andrey Andreev56454792012-05-17 14:32:19 +0300163
Timothy Warrenb8e62852012-04-26 18:40:54 -0400164 /**
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400165 * Default transparency for watermark
Andrey Andreev56454792012-05-17 14:32:19 +0300166 *
Timothy Warrenb8e62852012-04-26 18:40:54 -0400167 * @var int
168 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200169 public $wm_x_transp = 4;
Andrey Andreev56454792012-05-17 14:32:19 +0300170
Timothy Warrenb8e62852012-04-26 18:40:54 -0400171 /**
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400172 * Default transparency for watermark
173 *
Timothy Warrenb8e62852012-04-26 18:40:54 -0400174 * @var int
175 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200176 public $wm_y_transp = 4;
Andrey Andreev56454792012-05-17 14:32:19 +0300177
Timothy Warrenb8e62852012-04-26 18:40:54 -0400178 /**
179 * Watermark image path
Andrey Andreev56454792012-05-17 14:32:19 +0300180 *
Timothy Warrenb8e62852012-04-26 18:40:54 -0400181 * @var string
182 */
Andrey Andreev56454792012-05-17 14:32:19 +0300183 public $wm_overlay_path = '';
184
Timothy Warrenb8e62852012-04-26 18:40:54 -0400185 /**
186 * TT font
187 *
188 * @var string
189 */
190 public $wm_font_path = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300191
Timothy Warrenb8e62852012-04-26 18:40:54 -0400192 /**
193 * Font size (different versions of GD will either use points or pixels)
194 *
195 * @var int
196 */
197 public $wm_font_size = 17;
Andrey Andreev56454792012-05-17 14:32:19 +0300198
Timothy Warrenb8e62852012-04-26 18:40:54 -0400199 /**
200 * Vertical alignment: T M B
201 *
202 * @var string
203 */
204 public $wm_vrt_alignment = 'B';
Andrey Andreev56454792012-05-17 14:32:19 +0300205
Timothy Warrenb8e62852012-04-26 18:40:54 -0400206 /**
207 * Horizontal alignment: L R C
208 *
209 * @var string
210 */
211 public $wm_hor_alignment = 'C';
Andrey Andreev56454792012-05-17 14:32:19 +0300212
Timothy Warrenb8e62852012-04-26 18:40:54 -0400213 /**
214 * Padding around text
215 *
216 * @var int
217 */
218 public $wm_padding = 0;
Andrey Andreev56454792012-05-17 14:32:19 +0300219
Timothy Warrenb8e62852012-04-26 18:40:54 -0400220 /**
221 * Lets you push text to the right
222 *
223 * @var int
224 */
225 public $wm_hor_offset = 0;
Andrey Andreev56454792012-05-17 14:32:19 +0300226
Timothy Warrenb8e62852012-04-26 18:40:54 -0400227 /**
228 * Lets you push text down
229 *
230 * @var int
231 */
232 public $wm_vrt_offset = 0;
Andrey Andreev56454792012-05-17 14:32:19 +0300233
Timothy Warrenb8e62852012-04-26 18:40:54 -0400234 /**
235 * Text color
236 *
237 * @var string
238 */
Andrey Andreev56454792012-05-17 14:32:19 +0300239 protected $wm_font_color = '#ffffff';
240
Timothy Warrenb8e62852012-04-26 18:40:54 -0400241 /**
242 * Dropshadow color
243 *
244 * @var string
245 */
Andrey Andreev56454792012-05-17 14:32:19 +0300246 protected $wm_shadow_color = '';
247
Timothy Warrenb8e62852012-04-26 18:40:54 -0400248 /**
249 * Dropshadow distance
250 *
251 * @var int
252 */
253 public $wm_shadow_distance = 2;
Andrey Andreev56454792012-05-17 14:32:19 +0300254
Timothy Warrenb8e62852012-04-26 18:40:54 -0400255 /**
256 * Image opacity: 1 - 100 Only works with image
257 *
258 * @var int
259 */
Andrey Andreev56454792012-05-17 14:32:19 +0300260 public $wm_opacity = 50;
Derek Allard2067d1a2008-11-13 22:59:24 +0000261
Timothy Warrenb8e62852012-04-26 18:40:54 -0400262 // --------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 // Private Vars
Timothy Warrenb8e62852012-04-26 18:40:54 -0400264 // --------------------------------------------------------------------------
Andrey Andreev56454792012-05-17 14:32:19 +0300265
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400266 /**
267 * Source image folder
268 *
269 * @var string
270 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200271 public $source_folder = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300272
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400273 /**
274 * Destination image folder
275 *
276 * @var string
277 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200278 public $dest_folder = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300279
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400280 /**
281 * Image mime-type
282 *
283 * @var string
284 */
Andrey Andreev56454792012-05-17 14:32:19 +0300285 public $mime_type = '';
286
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400287 /**
Andrey Andreev56454792012-05-17 14:32:19 +0300288 * Original image width
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400289 *
290 * @var int
291 */
Andrey Andreev56454792012-05-17 14:32:19 +0300292 public $orig_width = '';
293
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400294 /**
295 * Original image height
296 *
297 * @var int
298 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200299 public $orig_height = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300300
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400301 /**
302 * Image format
Andrey Andreev56454792012-05-17 14:32:19 +0300303 *
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400304 * @var string
305 */
Andrey Andreev56454792012-05-17 14:32:19 +0300306 public $image_type = '';
307
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400308 /**
309 * Size of current image
310 *
311 * @var string
312 */
Andrey Andreev56454792012-05-17 14:32:19 +0300313 public $size_str = '';
314
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400315 /**
316 * Full path to source image
317 *
318 * @var string
319 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200320 public $full_src_path = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300321
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400322 /**
323 * Full path to destination image
324 *
325 * @var string
326 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200327 public $full_dst_path = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300328
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400329 /**
330 * Name of function to create image
331 *
332 * @var string
333 */
Andrey Andreev56454792012-05-17 14:32:19 +0300334 public $create_fnc = 'imagecreatetruecolor';
335
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400336 /**
337 * Name of function to copy image
338 *
339 * @var string
340 */
Andrey Andreev56454792012-05-17 14:32:19 +0300341 public $copy_fnc = 'imagecopyresampled';
342
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400343 /**
344 * Error messages
345 *
346 * @var array
347 */
Andrey Andreev56454792012-05-17 14:32:19 +0300348 public $error_msg = array();
349
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400350 /**
351 * Whether to have a drop shadow on watermark
352 *
353 * @var bool
354 */
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200355 protected $wm_use_drop_shadow = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300356
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400357 /**
358 * Whether to use truetype fonts
359 *
360 * @var bool
361 */
Andrey Andreev3a459572011-12-21 11:23:11 +0200362 public $wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000363
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400364 /**
365 * Initialize Image Library
366 *
367 * @param array $props
Andrey Andreev56454792012-05-17 14:32:19 +0300368 * @return void
Timothy Warrenb82bc3a2012-04-27 09:12:58 -0400369 */
Greg Akera9263282010-11-10 15:26:43 -0600370 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
372 if (count($props) > 0)
373 {
374 $this->initialize($props);
375 }
376
Andrey Andreev8e70b792012-01-12 20:19:24 +0200377 log_message('debug', 'Image Lib Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * Initialize image properties
384 *
385 * Resets values in case this class is used in a loop
386 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 * @return void
388 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200389 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
Michael Denniscb07a322011-08-20 23:40:59 -0700391 $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 +0000392
393 foreach ($props as $val)
394 {
395 $this->$val = '';
396 }
397
Michael Denniscb07a322011-08-20 23:40:59 -0700398 $this->image_library = 'gd2';
399 $this->dynamic_output = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300400 $this->quality = 90;
Michael Denniscb07a322011-08-20 23:40:59 -0700401 $this->create_thumb = FALSE;
402 $this->thumb_marker = '_thumb';
403 $this->maintain_ratio = TRUE;
404 $this->master_dim = 'auto';
405 $this->wm_type = 'text';
406 $this->wm_x_transp = 4;
407 $this->wm_y_transp = 4;
408 $this->wm_font_size = 17;
409 $this->wm_vrt_alignment = 'B';
410 $this->wm_hor_alignment = 'C';
411 $this->wm_padding = 0;
412 $this->wm_hor_offset = 0;
413 $this->wm_vrt_offset = 0;
414 $this->wm_font_color = '#ffffff';
415 $this->wm_shadow_distance = 2;
416 $this->wm_opacity = 50;
417 $this->create_fnc = 'imagecreatetruecolor';
418 $this->copy_fnc = 'imagecopyresampled';
419 $this->error_msg = array();
420 $this->wm_use_drop_shadow = FALSE;
421 $this->wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * initialize image preferences
428 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 * @param array
430 * @return bool
431 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200432 public function initialize($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200434 // Convert array elements into class variables
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 if (count($props) > 0)
436 {
437 foreach ($props as $key => $val)
438 {
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200439 if (property_exists($this, $key))
440 {
441 if (in_array($key, array('wm_font_color', 'wm_shadow_color')))
442 {
Andrey Andreev8323ae62011-12-31 18:39:10 +0200443 if (preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches))
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200444 {
Andrey Andreevcf2ba9e2012-01-01 21:57:13 +0200445 /* $matches[1] contains our hex color value, but it might be
Andrey Andreev8323ae62011-12-31 18:39:10 +0200446 * both in the full 6-length format or the shortened 3-length
447 * value.
Andrey Andreevbb96c8b2011-12-31 18:48:39 +0200448 * We'll later need the full version, so we keep it if it's
Andrey Andreev8323ae62011-12-31 18:39:10 +0200449 * already there and if not - we'll convert to it. We can
450 * access string characters by their index as in an array,
451 * so we'll do that and use concatenation to form the final
452 * value:
453 */
Andrey Andreev665af0c2011-12-30 14:39:29 +0200454 $val = (strlen($matches[1]) === 6)
455 ? '#'.$matches[1]
456 : '#'.$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 +0200457 }
458 else
459 {
460 continue;
461 }
462 }
463
464 $this->$key = $val;
465 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
467 }
468
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200469 // Is there a source image? If not, there's no reason to continue
Alex Bilbied261b1e2012-06-02 11:12:16 +0100470 if ($this->source_image === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
472 $this->set_error('imglib_source_image_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500473 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
475
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200476 /* Is getimagesize() available?
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 *
478 * We use it to determine the image properties (width/height).
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200479 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * properties using ImageMagick and NetPBM
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 */
482 if ( ! function_exists('getimagesize'))
483 {
484 $this->set_error('imglib_gd_required_for_props');
485 return FALSE;
486 }
487
488 $this->image_library = strtolower($this->image_library);
489
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200490 /* Set the full server path
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 *
492 * The source image may or may not contain a path.
493 * Either way, we'll try use realpath to generate the
494 * full server path in order to more reliably read it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 */
Andrey Andreev8e70b792012-01-12 20:19:24 +0200496 if (function_exists('realpath') && @realpath($this->source_image) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200498 $full_source_path = str_replace('\\', '/', realpath($this->source_image));
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
500 else
501 {
502 $full_source_path = $this->source_image;
503 }
504
505 $x = explode('/', $full_source_path);
506 $this->source_image = end($x);
507 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
508
509 // Set the Image Properties
510 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
511 {
Eric Barnesb1673362011-12-05 22:05:38 -0500512 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
514
515 /*
516 * Assign the "new" image name/path
517 *
518 * If the user has set a "new_image" name it means
519 * we are making a copy of the source image. If not
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200520 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 * set the destination filename and path accordingly.
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100523 if ($this->new_image === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 {
525 $this->dest_image = $this->source_image;
526 $this->dest_folder = $this->source_folder;
527 }
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200528 elseif (strpos($this->new_image, '/') === FALSE)
529 {
530 $this->dest_folder = $this->source_folder;
531 $this->dest_image = $this->new_image;
532 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 else
534 {
Andrey Andreev1b815532012-04-03 16:06:03 +0300535 if (strpos($this->new_image, '/') === FALSE && strpos($this->new_image, '\\') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200537 $full_dest_path = str_replace('\\', '/', realpath($this->new_image));
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
539 else
540 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200541 $full_dest_path = $this->new_image;
542 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000543
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200544 // Is there a file name?
545 if ( ! preg_match('#\.(jpg|jpeg|gif|png)$#i', $full_dest_path))
546 {
547 $this->dest_folder = $full_dest_path.'/';
548 $this->dest_image = $this->source_image;
549 }
550 else
551 {
552 $x = explode('/', $full_dest_path);
553 $this->dest_image = end($x);
554 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 }
556 }
557
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200558 /* Compile the finalized filenames/paths
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 *
560 * We'll create two master strings containing the
561 * full server path to the source image and the
562 * full server path to the destination image.
563 * We'll also split the destination image name
564 * so we can insert the thumbnail marker if needed.
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100566 if ($this->create_thumb === FALSE OR $this->thumb_marker === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
568 $this->thumb_marker = '';
569 }
570
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200571 $xp = $this->explode_name($this->dest_image);
Derek Allard2067d1a2008-11-13 22:59:24 +0000572
573 $filename = $xp['name'];
574 $file_ext = $xp['ext'];
575
576 $this->full_src_path = $this->source_folder.$this->source_image;
577 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
578
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200579 /* Should we maintain image proportions?
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 *
581 * When creating thumbs or copies, the target width/height
582 * might not be in correct proportion with the source
Andrey Andreev8e70b792012-01-12 20:19:24 +0200583 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100585 if ($this->maintain_ratio === TRUE && ($this->width !== 0 OR $this->height !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
587 $this->image_reproportion();
588 }
589
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200590 /* Was a width and height specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 *
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200592 * If the destination width/height was not submitted we
593 * will use the values from the actual file
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 */
Alex Bilbied261b1e2012-06-02 11:12:16 +0100595 if ($this->width === '')
Andrey Andreev8e70b792012-01-12 20:19:24 +0200596 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 $this->width = $this->orig_width;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200598 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000599
Alex Bilbied261b1e2012-06-02 11:12:16 +0100600 if ($this->height === '')
Andrey Andreev8e70b792012-01-12 20:19:24 +0200601 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 $this->height = $this->orig_height;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200603 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000604
605 // Set the quality
Andrey Andreev8e70b792012-01-12 20:19:24 +0200606 $this->quality = trim(str_replace('%', '', $this->quality));
Derek Allard2067d1a2008-11-13 22:59:24 +0000607
Alex Bilbied261b1e2012-06-02 11:12:16 +0100608 if ($this->quality === '' OR $this->quality === 0 OR ! preg_match('/^[0-9]+$/', $this->quality))
Andrey Andreev8e70b792012-01-12 20:19:24 +0200609 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 $this->quality = 90;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200611 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000612
613 // Set the x/y coordinates
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300614 is_numeric($this->x_axis) OR $this->x_axis = 0;
615 is_numeric($this->y_axis) OR $this->y_axis = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000616
617 // Watermark-related Stuff...
Alex Bilbied261b1e2012-06-02 11:12:16 +0100618 if ($this->wm_overlay_path !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200620 $this->wm_overlay_path = str_replace('\\', '/', realpath($this->wm_overlay_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 }
622
Alex Bilbied261b1e2012-06-02 11:12:16 +0100623 if ($this->wm_shadow_color !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
625 $this->wm_use_drop_shadow = TRUE;
626 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100627 elseif ($this->wm_use_drop_shadow === TRUE && $this->wm_shadow_color === '')
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200628 {
629 $this->wm_use_drop_shadow = FALSE;
630 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000631
Alex Bilbied261b1e2012-06-02 11:12:16 +0100632 if ($this->wm_font_path !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
634 $this->wm_use_truetype = TRUE;
635 }
636
637 return TRUE;
638 }
639
640 // --------------------------------------------------------------------
641
642 /**
643 * Image Resize
644 *
645 * This is a wrapper function that chooses the proper
646 * resize function based on the protocol specified
647 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 * @return bool
649 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200650 public function resize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
Andrey Andreev1a9f52c2012-01-07 01:06:34 +0200652 $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 return $this->$protocol('resize');
654 }
655
656 // --------------------------------------------------------------------
657
658 /**
659 * Image Crop
660 *
661 * This is a wrapper function that chooses the proper
662 * cropping function based on the protocol specified
663 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 * @return bool
665 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200666 public function crop()
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 {
Andrey Andreev5a67e3d2012-01-07 01:21:09 +0200668 $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 return $this->$protocol('crop');
670 }
671
672 // --------------------------------------------------------------------
673
674 /**
675 * Image Rotate
676 *
677 * This is a wrapper function that chooses the proper
678 * rotation function based on the protocol specified
679 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 * @return bool
681 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200682 public function rotate()
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
684 // Allowed rotation values
685 $degs = array(90, 180, 270, 'vrt', 'hor');
686
Alex Bilbied261b1e2012-06-02 11:12:16 +0100687 if ($this->rotation_angle === '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 {
689 $this->set_error('imglib_rotation_angle_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500690 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 }
692
693 // Reassign the width and height
Alex Bilbied261b1e2012-06-02 11:12:16 +0100694 if ($this->rotation_angle === 90 OR $this->rotation_angle === 270)
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 {
696 $this->width = $this->orig_height;
697 $this->height = $this->orig_width;
698 }
699 else
700 {
701 $this->width = $this->orig_width;
702 $this->height = $this->orig_height;
703 }
704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 // Choose resizing function
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200706 if ($this->image_library === 'imagemagick' OR $this->image_library === 'netpbm')
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 {
708 $protocol = 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 return $this->$protocol('rotate');
710 }
711
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200712 return ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
713 ? $this->image_mirror_gd()
714 : $this->image_rotate_gd();
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 }
716
717 // --------------------------------------------------------------------
718
719 /**
720 * Image Process Using GD/GD2
721 *
722 * This function will resize or crop
723 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 * @param string
725 * @return bool
726 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200727 public function image_process_gd($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 {
729 $v2_override = FALSE;
730
731 // If the target width/height match the source, AND if the new file name is not equal to the old file name
732 // 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 +0100733 if ($this->dynamic_output === FALSE && $this->orig_width === $this->width && $this->orig_height === $this->height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100735 if ($this->source_image !== $this->new_image && @copy($this->full_src_path, $this->full_dst_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200737 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200739
740 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 }
742
743 // Let's set up our values based on the action
Alex Bilbied261b1e2012-06-02 11:12:16 +0100744 if ($action === 'crop')
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200746 // Reassign the source width/height if cropping
Derek Jones4b9c6292011-07-01 17:40:48 -0500747 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 $this->orig_height = $this->height;
749
750 // GD 2.0 has a cropping bug so we'll test for it
751 if ($this->gd_version() !== FALSE)
752 {
753 $gd_version = str_replace('0', '', $this->gd_version());
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300754 $v2_override = ($gd_version === 2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 }
756 }
757 else
758 {
759 // If resizing the x/y axis must be zero
760 $this->x_axis = 0;
761 $this->y_axis = 0;
762 }
763
Derek Jones4b9c6292011-07-01 17:40:48 -0500764 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 if ( ! ($src_img = $this->image_create_gd()))
766 {
767 return FALSE;
768 }
769
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200770 /* Create the image
771 *
772 * Old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
773 * it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
774 * below should that ever prove inaccurate.
775 *
Alex Bilbied261b1e2012-06-02 11:12:16 +0100776 * if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor') && $v2_override === FALSE)
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200777 */
Andrey Andreev8e70b792012-01-12 20:19:24 +0200778 if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 {
780 $create = 'imagecreatetruecolor';
781 $copy = 'imagecopyresampled';
782 }
783 else
784 {
785 $create = 'imagecreate';
786 $copy = 'imagecopyresized';
787 }
788
789 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500790
Alex Bilbied261b1e2012-06-02 11:12:16 +0100791 if ($this->image_type === 3) // png we can actually preserve transparency
Derek Jones595bfd12010-08-20 10:28:22 -0500792 {
793 imagealphablending($dst_img, FALSE);
794 imagesavealpha($dst_img, TRUE);
795 }
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
798
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200799 // Show the image
Alex Bilbied261b1e2012-06-02 11:12:16 +0100800 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
802 $this->image_display_gd($dst_img);
803 }
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200804 elseif ( ! $this->image_save_gd($dst_img)) // Or save it
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200806 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 }
808
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200809 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 imagedestroy($dst_img);
811 imagedestroy($src_img);
812
813 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000814 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000815
816 return TRUE;
817 }
818
819 // --------------------------------------------------------------------
820
821 /**
822 * Image Process Using ImageMagick
823 *
824 * This function will resize, crop or rotate
825 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 * @param string
827 * @return bool
828 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200829 public function image_process_imagemagick($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500831 // Do we have a vaild library path?
Alex Bilbied261b1e2012-06-02 11:12:16 +0100832 if ($this->library_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 {
834 $this->set_error('imglib_libpath_invalid');
835 return FALSE;
836 }
837
Andrey Andreev8e70b792012-01-12 20:19:24 +0200838 if ( ! preg_match('/convert$/i', $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200840 $this->library_path = rtrim($this->library_path, '/').'/convert';
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 }
842
843 // Execute the command
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200844 $cmd = $this->library_path.' -quality '.$this->quality;
Derek Allard2067d1a2008-11-13 22:59:24 +0000845
Alex Bilbied261b1e2012-06-02 11:12:16 +0100846 if ($action === 'crop')
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200848 $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 +0000849 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100850 elseif ($action === 'rotate')
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200852 $angle = ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
853 ? '-flop' : '-rotate '.$this->rotation_angle;
Derek Allard2067d1a2008-11-13 22:59:24 +0000854
Andrey Andreev8e70b792012-01-12 20:19:24 +0200855 $cmd .= ' '.$angle.' "'.$this->full_src_path.'" "'.$this->full_dst_path.'" 2>&1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 }
Andrey Andreev8e70b792012-01-12 20:19:24 +0200857 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 {
Omar24063af2012-07-02 13:50:17 -0300859 if($this->maintain_ratio === TRUE)
860 {
861 $cmd .= ' -resize '.$this->width.'x'.$this->height.' "'.$this->full_src_path.'" "'.$this->full_dst_path.'" 2>&1';
862 }
Omarbb531d62012-06-29 10:48:28 -0300863 else
Omar24063af2012-07-02 13:50:17 -0300864 {
865 $cmd .= ' -resize '.$this->width.'x'.$this->height.'\! "'.$this->full_src_path.'" "'.$this->full_dst_path.'" 2>&1';
866 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 }
868
869 $retval = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 @exec($cmd, $output, $retval);
871
Andrey Andreev8e70b792012-01-12 20:19:24 +0200872 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 if ($retval > 0)
874 {
875 $this->set_error('imglib_image_process_failed');
876 return FALSE;
877 }
878
879 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000880 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000881
882 return TRUE;
883 }
884
885 // --------------------------------------------------------------------
886
887 /**
888 * Image Process Using NetPBM
889 *
890 * This function will resize, crop or rotate
891 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 * @param string
893 * @return bool
894 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200895 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100897 if ($this->library_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
899 $this->set_error('imglib_libpath_invalid');
900 return FALSE;
901 }
902
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200903 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 switch ($this->image_type)
905 {
906 case 1 :
Andrey Andreev56454792012-05-17 14:32:19 +0300907 $cmd_in = 'giftopnm';
908 $cmd_out = 'ppmtogif';
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 break;
910 case 2 :
Andrey Andreev56454792012-05-17 14:32:19 +0300911 $cmd_in = 'jpegtopnm';
912 $cmd_out = 'ppmtojpeg';
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 break;
914 case 3 :
Andrey Andreev56454792012-05-17 14:32:19 +0300915 $cmd_in = 'pngtopnm';
916 $cmd_out = 'ppmtopng';
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 break;
918 }
919
Alex Bilbied261b1e2012-06-02 11:12:16 +0100920 if ($action === 'crop')
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 {
922 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
923 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100924 elseif ($action === 'rotate')
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 {
926 switch ($this->rotation_angle)
927 {
Andrey Andreev56454792012-05-17 14:32:19 +0300928 case 90: $angle = 'r270';
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300930 case 180: $angle = 'r180';
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300932 case 270: $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300934 case 'vrt': $angle = 'tb';
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 break;
Andrey Andreev56454792012-05-17 14:32:19 +0300936 case 'hor': $angle = 'lr';
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 break;
938 }
939
940 $cmd_inner = 'pnmflip -'.$angle.' ';
941 }
942 else // Resize
943 {
944 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
945 }
946
947 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
948
949 $retval = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 @exec($cmd, $output, $retval);
951
Andrey Andreev8e70b792012-01-12 20:19:24 +0200952 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 if ($retval > 0)
954 {
955 $this->set_error('imglib_image_process_failed');
956 return FALSE;
957 }
958
959 // With NetPBM we have to create a temporary image.
960 // If you try manipulating the original it fails so
961 // we have to rename the temp file.
962 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200963 unlink($this->dest_folder.'netpbm.tmp');
Derek Jones172e1612009-10-13 14:32:48 +0000964 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000965
966 return TRUE;
967 }
968
969 // --------------------------------------------------------------------
970
971 /**
972 * Image Rotate Using GD
973 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 * @return bool
975 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200976 public function image_rotate_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +0200978 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 if ( ! ($src_img = $this->image_create_gd()))
980 {
981 return FALSE;
982 }
983
984 // Set the background color
985 // This won't work with transparent PNG files so we are
986 // going to have to figure out how to determine the color
987 // of the alpha channel in a future release.
988
989 $white = imagecolorallocate($src_img, 255, 255, 255);
990
Andrey Andreev8e70b792012-01-12 20:19:24 +0200991 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
993
Andrey Andreev8e70b792012-01-12 20:19:24 +0200994 // Show the image
Alex Bilbied261b1e2012-06-02 11:12:16 +0100995 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
997 $this->image_display_gd($dst_img);
998 }
Andrey Andreev8e70b792012-01-12 20:19:24 +0200999 elseif ( ! $this->image_save_gd($dst_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001001 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 }
1003
Andrey Andreev8e70b792012-01-12 20:19:24 +02001004 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 imagedestroy($dst_img);
1006 imagedestroy($src_img);
1007
1008 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +00001009 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001010
Pascal Kriete8761ef52011-02-14 13:13:52 -05001011 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 }
1013
1014 // --------------------------------------------------------------------
1015
1016 /**
1017 * Create Mirror Image using GD
1018 *
1019 * This function will flip horizontal or vertical
1020 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 * @return bool
1022 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001023 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 {
1025 if ( ! $src_img = $this->image_create_gd())
1026 {
1027 return FALSE;
1028 }
1029
Derek Jones4b9c6292011-07-01 17:40:48 -05001030 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 $height = $this->orig_height;
1032
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001033 if ($this->rotation_angle === 'hor')
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001035 for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 while ($left < $right)
1038 {
1039 $cl = imagecolorat($src_img, $left, $i);
1040 $cr = imagecolorat($src_img, $right, $i);
1041
1042 imagesetpixel($src_img, $left, $i, $cr);
1043 imagesetpixel($src_img, $right, $i, $cl);
1044
1045 $left++;
1046 $right--;
1047 }
1048 }
1049 }
1050 else
1051 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001052 for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1)
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 while ($top < $bot)
1055 {
1056 $ct = imagecolorat($src_img, $i, $top);
1057 $cb = imagecolorat($src_img, $i, $bot);
1058
1059 imagesetpixel($src_img, $i, $top, $cb);
1060 imagesetpixel($src_img, $i, $bot, $ct);
1061
1062 $top++;
1063 $bot--;
1064 }
1065 }
1066 }
1067
Andrey Andreev8e70b792012-01-12 20:19:24 +02001068 // Show the image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001069 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 {
1071 $this->image_display_gd($src_img);
1072 }
Andrey Andreev8e70b792012-01-12 20:19:24 +02001073 elseif ( ! $this->image_save_gd($src_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001075 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 }
1077
Andrey Andreev8e70b792012-01-12 20:19:24 +02001078 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 imagedestroy($src_img);
1080
1081 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +00001082 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001083
1084 return TRUE;
1085 }
1086
1087 // --------------------------------------------------------------------
1088
1089 /**
1090 * Image Watermark
1091 *
1092 * This is a wrapper function that chooses the type
1093 * of watermarking based on the specified preference.
1094 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 * @return bool
1096 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001097 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001099 return ($this->wm_type === 'overlay') ? $this->overlay_watermark() : $this->text_watermark();
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 }
1101
1102 // --------------------------------------------------------------------
1103
1104 /**
1105 * Watermark - Graphic Version
1106 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 * @return bool
1108 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001109 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 {
1111 if ( ! function_exists('imagecolortransparent'))
1112 {
1113 $this->set_error('imglib_gd_required');
1114 return FALSE;
1115 }
1116
Andrey Andreev8e70b792012-01-12 20:19:24 +02001117 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 $this->get_image_properties();
1119
Andrey Andreev8e70b792012-01-12 20:19:24 +02001120 // Fetch watermark image properties
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001121 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 $wm_img_type = $props['image_type'];
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001123 $wm_width = $props['width'];
1124 $wm_height = $props['height'];
Derek Allard2067d1a2008-11-13 22:59:24 +00001125
Andrey Andreev8e70b792012-01-12 20:19:24 +02001126 // Create two image resources
Derek Jones4b9c6292011-07-01 17:40:48 -05001127 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 $src_img = $this->image_create_gd($this->full_src_path);
1129
1130 // Reverse the offset if necessary
1131 // When the image is positioned at the bottom
1132 // we don't want the vertical offset to push it
Andrey Andreev8e70b792012-01-12 20:19:24 +02001133 // further down. We want the reverse, so we'll
1134 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 // offset when the image is at the right
1136
Andrey Andreev8e70b792012-01-12 20:19:24 +02001137 $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
1138 $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +00001139
Alex Bilbied261b1e2012-06-02 11:12:16 +01001140 if ($this->wm_vrt_alignment === 'B')
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1142
Alex Bilbied261b1e2012-06-02 11:12:16 +01001143 if ($this->wm_hor_alignment === 'R')
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1145
Andrey Andreev8e70b792012-01-12 20:19:24 +02001146 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1148 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1149
Andrey Andreev8e70b792012-01-12 20:19:24 +02001150 // Set the vertical position
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001151 if ($this->wm_vrt_alignment === 'M')
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001153 $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
1154 }
1155 elseif ($this->wm_vrt_alignment === 'B')
1156 {
1157 $y_axis += $this->orig_height - $wm_height;
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 }
1159
Andrey Andreev8e70b792012-01-12 20:19:24 +02001160 // Set the horizontal position
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001161 if ($this->wm_hor_alignment === 'C')
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001163 $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
1164 }
1165 elseif ($this->wm_hor_alignment === 'R')
1166 {
1167 $x_axis += $this->orig_width - $wm_width;
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 }
1169
Derek Jones4b9c6292011-07-01 17:40:48 -05001170 // Build the finalized image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001171 if ($wm_img_type === 3 && function_exists('imagealphablending'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 {
1173 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +02001174 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001175
1176 // Set RGB values for text and shadow
1177 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
1178 $alpha = ($rgba & 0x7F000000) >> 24;
1179
1180 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
1181 if ($alpha > 0)
1182 {
1183 // copy the image directly, the image's alpha transparency being the sole determinant of blending
1184 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
1185 }
1186 else
1187 {
1188 // set our RGB value from above to be transparent and merge the images with the specified opacity
1189 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
1190 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
1191 }
1192
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001193 // Output the image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001194 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 {
1196 $this->image_display_gd($src_img);
1197 }
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001198 elseif ( ! $this->image_save_gd($src_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001200 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 }
1202
1203 imagedestroy($src_img);
1204 imagedestroy($wm_img);
1205
1206 return TRUE;
1207 }
1208
1209 // --------------------------------------------------------------------
1210
1211 /**
1212 * Watermark - Text Version
1213 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 * @return bool
1215 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001216 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 {
1218 if ( ! ($src_img = $this->image_create_gd()))
1219 {
1220 return FALSE;
1221 }
1222
Alex Bilbied261b1e2012-06-02 11:12:16 +01001223 if ($this->wm_use_truetype === TRUE && ! file_exists($this->wm_font_path))
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 {
1225 $this->set_error('imglib_missing_font');
1226 return FALSE;
1227 }
1228
Andrey Andreev8e70b792012-01-12 20:19:24 +02001229 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 $this->get_image_properties();
1231
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 // Reverse the vertical offset
1233 // When the image is positioned at the bottom
1234 // we don't want the vertical offset to push it
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001235 // further down. We want the reverse, so we'll
Andrey Andreev8e70b792012-01-12 20:19:24 +02001236 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 // offset flips itself automatically
1238
Alex Bilbied261b1e2012-06-02 11:12:16 +01001239 if ($this->wm_vrt_alignment === 'B')
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1241
Alex Bilbied261b1e2012-06-02 11:12:16 +01001242 if ($this->wm_hor_alignment === 'R')
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1244
1245 // Set font width and height
1246 // These are calculated differently depending on
1247 // whether we are using the true type font or not
Alex Bilbied261b1e2012-06-02 11:12:16 +01001248 if ($this->wm_use_truetype === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001250 if ($this->wm_font_size === '')
Andrey Andreeva92b9032011-12-24 19:05:58 +02001251 {
1252 $this->wm_font_size = 17;
1253 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001254
Derek Jones4b9c6292011-07-01 17:40:48 -05001255 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 $fontheight = $this->wm_font_size;
1257 $this->wm_vrt_offset += $this->wm_font_size;
1258 }
1259 else
1260 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001261 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 $fontheight = imagefontheight($this->wm_font_size);
1263 }
1264
1265 // Set base X and Y axis values
1266 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1267 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1268
Alex Bilbied261b1e2012-06-02 11:12:16 +01001269 if ($this->wm_use_drop_shadow === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 $this->wm_shadow_distance = 0;
1271
1272 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1273 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1274
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001275 // Set verticle alignment
1276 if ($this->wm_vrt_alignment === 'M')
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001278 $y_axis += ($this->orig_height / 2) + ($fontheight / 2);
1279 }
1280 elseif ($this->wm_vrt_alignment === 'B')
1281 {
1282 $y_axis += $this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight / 2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 }
1284
1285 $x_shad = $x_axis + $this->wm_shadow_distance;
1286 $y_shad = $y_axis + $this->wm_shadow_distance;
1287
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001288 if ($this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001290 // Set horizontal alignment
1291 if ($this->wm_hor_alignment === 'R')
1292 {
1293 $x_shad += $this->orig_width - ($fontwidth * strlen($this->wm_text));
1294 $x_axis += $this->orig_width - ($fontwidth * strlen($this->wm_text));
1295 }
1296 elseif ($this->wm_hor_alignment === 'C')
1297 {
1298 $x_shad += floor(($this->orig_width - ($fontwidth * strlen($this->wm_text))) / 2);
1299 $x_axis += floor(($this->orig_width - ($fontwidth * strlen($this->wm_text))) / 2);
1300 }
1301
Andrey Andreev8323ae62011-12-31 18:39:10 +02001302 /* Set RGB values for text and shadow
1303 *
1304 * First character is #, so we don't really need it.
1305 * Get the rest of the string and split it into 2-length
1306 * hex values:
1307 */
1308 $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2);
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001309 $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
Andrey Andreev8323ae62011-12-31 18:39:10 +02001310 $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2);
Ronald Beilsma8f80bc42012-01-12 16:41:49 +01001311 $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[1]), hexdec($drp_color[2]));
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001312
Andrey Andreev8e70b792012-01-12 20:19:24 +02001313 // Add the text to the source image
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001314 if ($this->wm_use_truetype)
1315 {
1316 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1317 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1318 }
1319 else
1320 {
1321 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1322 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1323 }
Andrey Andreev99ae2262012-10-05 15:14:30 +03001324
1325 // We can preserve transparency for PNG images
1326 if ($this->image_type === 3)
1327 {
1328 imagealphablending($src_img, FALSE);
1329 imagesavealpha($src_img, TRUE);
1330 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 }
1332
Andrey Andreev8e70b792012-01-12 20:19:24 +02001333 // Output the final image
Alex Bilbied261b1e2012-06-02 11:12:16 +01001334 if ($this->dynamic_output === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 {
1336 $this->image_display_gd($src_img);
1337 }
1338 else
1339 {
1340 $this->image_save_gd($src_img);
1341 }
1342
1343 imagedestroy($src_img);
1344
1345 return TRUE;
1346 }
1347
1348 // --------------------------------------------------------------------
1349
1350 /**
1351 * Create Image - GD
1352 *
1353 * This simply creates an image resource handle
1354 * based on the type of image being processed
1355 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001357 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 * @return resource
1359 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001360 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001362 if ($path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 $path = $this->full_src_path;
1364
Alex Bilbied261b1e2012-06-02 11:12:16 +01001365 if ($image_type === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 $image_type = $this->image_type;
1367
1368
1369 switch ($image_type)
1370 {
1371 case 1 :
1372 if ( ! function_exists('imagecreatefromgif'))
1373 {
1374 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1375 return FALSE;
1376 }
1377
1378 return imagecreatefromgif($path);
1379 break;
1380 case 2 :
1381 if ( ! function_exists('imagecreatefromjpeg'))
1382 {
1383 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1384 return FALSE;
1385 }
1386
1387 return imagecreatefromjpeg($path);
1388 break;
1389 case 3 :
1390 if ( ! function_exists('imagecreatefrompng'))
1391 {
1392 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1393 return FALSE;
1394 }
1395
1396 return imagecreatefrompng($path);
1397 break;
1398
1399 }
1400
1401 $this->set_error(array('imglib_unsupported_imagecreate'));
1402 return FALSE;
1403 }
1404
1405 // --------------------------------------------------------------------
1406
1407 /**
1408 * Write image file to disk - GD
1409 *
1410 * Takes an image resource as input and writes the file
1411 * to the specified destination
1412 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 * @param resource
1414 * @return bool
1415 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001416 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 {
1418 switch ($this->image_type)
1419 {
Andrey Andreev56454792012-05-17 14:32:19 +03001420 case 1:
1421 if ( ! function_exists('imagegif'))
1422 {
1423 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1424 return FALSE;
1425 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001426
Andrey Andreev56454792012-05-17 14:32:19 +03001427 if ( ! @imagegif($resource, $this->full_dst_path))
1428 {
1429 $this->set_error('imglib_save_failed');
1430 return FALSE;
1431 }
1432 break;
1433 case 2:
1434 if ( ! function_exists('imagejpeg'))
1435 {
1436 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1437 return FALSE;
1438 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001439
Andrey Andreev56454792012-05-17 14:32:19 +03001440 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1441 {
1442 $this->set_error('imglib_save_failed');
1443 return FALSE;
1444 }
1445 break;
1446 case 3:
1447 if ( ! function_exists('imagepng'))
1448 {
1449 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1450 return FALSE;
1451 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001452
Andrey Andreev56454792012-05-17 14:32:19 +03001453 if ( ! @imagepng($resource, $this->full_dst_path))
1454 {
1455 $this->set_error('imglib_save_failed');
1456 return FALSE;
1457 }
1458 break;
1459 default:
1460 $this->set_error(array('imglib_unsupported_imagecreate'));
1461 return FALSE;
1462 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001463 }
1464
1465 return TRUE;
1466 }
1467
1468 // --------------------------------------------------------------------
1469
1470 /**
1471 * Dynamically outputs an image
1472 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 * @param resource
1474 * @return void
1475 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001476 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001478 header('Content-Disposition: filename='.$this->source_image.';');
1479 header('Content-Type: '.$this->mime_type);
Derek Allard2067d1a2008-11-13 22:59:24 +00001480 header('Content-Transfer-Encoding: binary');
1481 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1482
1483 switch ($this->image_type)
1484 {
Andrey Andreev56454792012-05-17 14:32:19 +03001485 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001487 case 2 : imagejpeg($resource, '', $this->quality);
Derek Allard2067d1a2008-11-13 22:59:24 +00001488 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001489 case 3 : imagepng($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001490 break;
Andrey Andreev56454792012-05-17 14:32:19 +03001491 default: echo 'Unable to display the image';
Derek Allard2067d1a2008-11-13 22:59:24 +00001492 break;
1493 }
1494 }
1495
1496 // --------------------------------------------------------------------
1497
1498 /**
1499 * Re-proportion Image Width/Height
1500 *
1501 * When creating thumbs, the desired width/height
1502 * can end up warping the image due to an incorrect
1503 * ratio between the full-sized image and the thumb.
1504 *
1505 * This function lets us re-proportion the width/height
1506 * if users choose to maintain the aspect ratio when resizing.
1507 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 * @return void
1509 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001510 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001511 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001512 if (($this->width === 0 && $this->height === 0) OR $this->orig_width === 0 OR $this->orig_height === 0
Andrey Andreev8e70b792012-01-12 20:19:24 +02001513 OR ( ! preg_match('/^[0-9]+$/', $this->width) && ! preg_match('/^[0-9]+$/', $this->height))
1514 OR ! preg_match('/^[0-9]+$/', $this->orig_width) OR ! preg_match('/^[0-9]+$/', $this->orig_height))
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001516 return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001517 }
1518
Andrey Andreev8e70b792012-01-12 20:19:24 +02001519 // Sanitize so we don't call preg_match() anymore
1520 $this->width = (int) $this->width;
1521 $this->height = (int) $this->height;
1522
1523 if ($this->master_dim !== 'width' && $this->master_dim !== 'height')
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001525 if ($this->width > 0 && $this->height > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001527 $this->master_dim = ((($this->orig_height/$this->orig_width) - ($this->height/$this->width)) < 0)
1528 ? 'width' : 'height';
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 }
1530 else
1531 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001532 $this->master_dim = ($this->height === 0) ? 'width' : 'height';
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 }
1534 }
Andrey Andreev8e70b792012-01-12 20:19:24 +02001535 elseif (($this->master_dim === 'width' && $this->width === 0)
1536 OR ($this->master_dim === 'height' && $this->height === 0))
1537 {
1538 return;
1539 }
1540
1541 if ($this->master_dim === 'width')
1542 {
1543 $this->height = (int) ceil($this->width*$this->orig_height/$this->orig_width);
1544 }
1545 else
1546 {
1547 $this->width = (int) ceil($this->orig_width*$this->height/$this->orig_height);
1548 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001549 }
1550
1551 // --------------------------------------------------------------------
1552
1553 /**
1554 * Get image properties
1555 *
1556 * A helper function that gets info about the file
1557 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001558 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001559 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 * @return mixed
1561 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001562 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001563 {
1564 // For now we require GD but we should
1565 // find a way to determine this using IM or NetPBM
1566
Alex Bilbied261b1e2012-06-02 11:12:16 +01001567 if ($path === '')
Andrey Andreev8e70b792012-01-12 20:19:24 +02001568 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001569 $path = $this->full_src_path;
Andrey Andreev8e70b792012-01-12 20:19:24 +02001570 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001571
1572 if ( ! file_exists($path))
1573 {
1574 $this->set_error('imglib_invalid_path');
1575 return FALSE;
1576 }
1577
Phil Sturgeon901998a2011-08-26 10:03:33 +01001578 $vals = getimagesize($path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001579 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
Andrey Andreeva92b9032011-12-24 19:05:58 +02001580 $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001581
Alex Bilbied261b1e2012-06-02 11:12:16 +01001582 if ($return === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001583 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001584 return array(
1585 'width' => $vals[0],
1586 'height' => $vals[1],
1587 'image_type' => $vals[2],
1588 'size_str' => $vals[3],
1589 'mime_type' => $mime
1590 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001591 }
1592
Andrey Andreeva92b9032011-12-24 19:05:58 +02001593 $this->orig_width = $vals[0];
1594 $this->orig_height = $vals[1];
1595 $this->image_type = $vals[2];
1596 $this->size_str = $vals[3];
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 $this->mime_type = $mime;
1598
1599 return TRUE;
1600 }
1601
1602 // --------------------------------------------------------------------
1603
1604 /**
1605 * Size calculator
1606 *
1607 * This function takes a known width x height and
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001608 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001609 * new variable needs to be known
1610 *
1611 * $props = array(
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001612 * 'width' => $width,
1613 * 'height' => $height,
1614 * 'new_width' => 40,
1615 * 'new_height' => ''
1616 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001617 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001618 * @param array
1619 * @return array
1620 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001621 public function size_calculator($vals)
Derek Allard2067d1a2008-11-13 22:59:24 +00001622 {
1623 if ( ! is_array($vals))
1624 {
1625 return;
1626 }
1627
1628 $allowed = array('new_width', 'new_height', 'width', 'height');
1629
1630 foreach ($allowed as $item)
1631 {
Andrey Andreev56454792012-05-17 14:32:19 +03001632 if (empty($vals[$item]))
1633 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001634 $vals[$item] = 0;
Andrey Andreev56454792012-05-17 14:32:19 +03001635 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 }
1637
Alex Bilbied261b1e2012-06-02 11:12:16 +01001638 if ($vals['width'] === 0 OR $vals['height'] === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001639 {
1640 return $vals;
1641 }
1642
Alex Bilbied261b1e2012-06-02 11:12:16 +01001643 if ($vals['new_width'] === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001644 {
1645 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1646 }
Alex Bilbied261b1e2012-06-02 11:12:16 +01001647 elseif ($vals['new_height'] === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001648 {
1649 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1650 }
1651
1652 return $vals;
1653 }
1654
1655 // --------------------------------------------------------------------
1656
1657 /**
1658 * Explode source_image
1659 *
1660 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001661 * from the source_image. This function lets us deal with
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001662 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001664 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 * $array['name'] = 'my.cool';
1666 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001667 * @param array
1668 * @return array
1669 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001670 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 {
Derek Jones08cae632009-02-10 20:03:29 +00001672 $ext = strrchr($source_image, '.');
1673 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001674
Derek Jones08cae632009-02-10 20:03:29 +00001675 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001676 }
1677
1678 // --------------------------------------------------------------------
1679
1680 /**
1681 * Is GD Installed?
1682 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 * @return bool
1684 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001685 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001686 {
1687 if ( ! extension_loaded('gd'))
1688 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001689 /* As it is stated in the PHP manual, dl() is not always available
1690 * and even if so - it could generate an E_WARNING message on failure
1691 */
Andrey Andreev8e70b792012-01-12 20:19:24 +02001692 return (function_exists('dl') && @dl('gd.so'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 }
1694
1695 return TRUE;
1696 }
1697
1698 // --------------------------------------------------------------------
1699
1700 /**
1701 * Get GD version
1702 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001703 * @return mixed
1704 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001705 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001706 {
1707 if (function_exists('gd_info'))
1708 {
1709 $gd_version = @gd_info();
Andrey Andreev8e70b792012-01-12 20:19:24 +02001710 return preg_replace('/\D/', '', $gd_version['GD Version']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 }
1712
1713 return FALSE;
1714 }
1715
1716 // --------------------------------------------------------------------
1717
1718 /**
1719 * Set error message
1720 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001721 * @param string
1722 * @return void
1723 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001724 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 {
1726 $CI =& get_instance();
1727 $CI->lang->load('imglib');
1728
1729 if (is_array($msg))
1730 {
1731 foreach ($msg as $val)
1732 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001733 $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 $this->error_msg[] = $msg;
1735 log_message('error', $msg);
1736 }
1737 }
1738 else
1739 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001740 $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg);
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 $this->error_msg[] = $msg;
1742 log_message('error', $msg);
1743 }
1744 }
1745
1746 // --------------------------------------------------------------------
1747
1748 /**
1749 * Show error messages
1750 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 * @param string
Timothy Warrenb82bc3a2012-04-27 09:12:58 -04001752 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001753 * @return string
1754 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001755 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001756 {
Andrey Andreev2c8baeb2012-01-19 15:45:28 +02001757 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 }
1759
1760}
Derek Allard2067d1a2008-11-13 22:59:24 +00001761
1762/* End of file Image_lib.php */
Andrey Andreev1b815532012-04-03 16:06:03 +03001763/* Location: ./system/libraries/Image_lib.php */