blob: c175c67401b50557c2766b25b9105ca8264c6c26 [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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
12 * bundled with this package in the files license.txt / license.rst. It is
13 * 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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
28// ------------------------------------------------------------------------
29
30/**
31 * Image Manipulation class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Image_lib
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/image_lib.html
38 */
39class CI_Image_lib {
40
Andrey Andreev3a459572011-12-21 11:23:11 +020041 public $image_library = 'gd2'; // Can be: imagemagick, netpbm, gd, gd2
42 public $library_path = '';
43 public $dynamic_output = FALSE; // Whether to send to browser or write to disk
44 public $source_image = '';
45 public $new_image = '';
46 public $width = '';
47 public $height = '';
48 public $quality = '90';
49 public $create_thumb = FALSE;
50 public $thumb_marker = '_thumb';
51 public $maintain_ratio = TRUE; // Whether to maintain aspect ratio when resizing or use hard values
52 public $master_dim = 'auto'; // auto, height, or width. Determines what to use as the master dimension
53 public $rotation_angle = '';
54 public $x_axis = '';
55 public $y_axis = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000056
57 // Watermark Vars
Andrey Andreev3a459572011-12-21 11:23:11 +020058 public $wm_text = ''; // Watermark text if graphic is not used
59 public $wm_type = 'text'; // Type of watermarking. Options: text/overlay
60 public $wm_x_transp = 4;
61 public $wm_y_transp = 4;
62 public $wm_overlay_path = ''; // Watermark image path
63 public $wm_font_path = ''; // TT font
64 public $wm_font_size = 17; // Font size (different versions of GD will either use points or pixels)
65 public $wm_vrt_alignment = 'B'; // Vertical alignment: T M B
66 public $wm_hor_alignment = 'C'; // Horizontal alignment: L R C
67 public $wm_padding = 0; // Padding around text
68 public $wm_hor_offset = 0; // Lets you push text to the right
69 public $wm_vrt_offset = 0; // Lets you push text down
Andrey Andreev64dbdfb2011-12-30 14:14:07 +020070 protected $wm_font_color = '#ffffff'; // Text color
71 protected $wm_shadow_color = ''; // Dropshadow color
Andrey Andreev3a459572011-12-21 11:23:11 +020072 public $wm_shadow_distance = 2; // Dropshadow distance
73 public $wm_opacity = 50; // Image opacity: 1 - 100 Only works with image
Derek Allard2067d1a2008-11-13 22:59:24 +000074
75 // Private Vars
Andrey Andreev3a459572011-12-21 11:23:11 +020076 public $source_folder = '';
77 public $dest_folder = '';
78 public $mime_type = '';
79 public $orig_width = '';
80 public $orig_height = '';
81 public $image_type = '';
82 public $size_str = '';
83 public $full_src_path = '';
84 public $full_dst_path = '';
85 public $create_fnc = 'imagecreatetruecolor';
86 public $copy_fnc = 'imagecopyresampled';
87 public $error_msg = array();
Andrey Andreev64dbdfb2011-12-30 14:14:07 +020088 protected $wm_use_drop_shadow = FALSE;
Andrey Andreev3a459572011-12-21 11:23:11 +020089 public $wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000090
91 /**
92 * Constructor
93 *
Derek Allard2067d1a2008-11-13 22:59:24 +000094 * @param string
95 * @return void
96 */
Greg Akera9263282010-11-10 15:26:43 -060097 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
99 if (count($props) > 0)
100 {
101 $this->initialize($props);
102 }
103
104 log_message('debug', "Image Lib Class Initialized");
105 }
106
107 // --------------------------------------------------------------------
108
109 /**
110 * Initialize image properties
111 *
112 * Resets values in case this class is used in a loop
113 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 * @return void
115 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200116 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
Michael Denniscb07a322011-08-20 23:40:59 -0700118 $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 +0000119
120 foreach ($props as $val)
121 {
122 $this->$val = '';
123 }
124
Michael Denniscb07a322011-08-20 23:40:59 -0700125 $this->image_library = 'gd2';
126 $this->dynamic_output = FALSE;
127 $this->quality = '90';
128 $this->create_thumb = FALSE;
129 $this->thumb_marker = '_thumb';
130 $this->maintain_ratio = TRUE;
131 $this->master_dim = 'auto';
132 $this->wm_type = 'text';
133 $this->wm_x_transp = 4;
134 $this->wm_y_transp = 4;
135 $this->wm_font_size = 17;
136 $this->wm_vrt_alignment = 'B';
137 $this->wm_hor_alignment = 'C';
138 $this->wm_padding = 0;
139 $this->wm_hor_offset = 0;
140 $this->wm_vrt_offset = 0;
141 $this->wm_font_color = '#ffffff';
142 $this->wm_shadow_distance = 2;
143 $this->wm_opacity = 50;
144 $this->create_fnc = 'imagecreatetruecolor';
145 $this->copy_fnc = 'imagecopyresampled';
146 $this->error_msg = array();
147 $this->wm_use_drop_shadow = FALSE;
148 $this->wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * initialize image preferences
155 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 * @param array
157 * @return bool
158 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200159 public function initialize($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
161 /*
162 * Convert array elements into class variables
163 */
164 if (count($props) > 0)
165 {
166 foreach ($props as $key => $val)
167 {
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200168 if (property_exists($this, $key))
169 {
170 if (in_array($key, array('wm_font_color', 'wm_shadow_color')))
171 {
Andrey Andreev8323ae62011-12-31 18:39:10 +0200172 if (preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches))
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200173 {
Andrey Andreev8323ae62011-12-31 18:39:10 +0200174 /* This particular line has caused a lengthy discussion
175 * (https://github.com/EllisLab/CodeIgniter/pull/818), so
176 * just to clarify:
177 *
178 * $matches[1] contains our hex color value, but it might be
179 * both in the full 6-length format or the shortened 3-length
180 * value.
181 * We'll later need the full version, so if we keep it if it's
182 * already there and if not - we'll convert to it. We can
183 * access string characters by their index as in an array,
184 * so we'll do that and use concatenation to form the final
185 * value:
186 */
Andrey Andreev665af0c2011-12-30 14:39:29 +0200187 $val = (strlen($matches[1]) === 6)
188 ? '#'.$matches[1]
189 : '#'.$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 +0200190 }
191 else
192 {
193 continue;
194 }
195 }
196
197 $this->$key = $val;
198 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200 }
201
202 /*
203 * Is there a source image?
204 *
205 * If not, there's no reason to continue
206 *
207 */
208 if ($this->source_image == '')
209 {
210 $this->set_error('imglib_source_image_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500211 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
213
214 /*
215 * Is getimagesize() Available?
216 *
217 * We use it to determine the image properties (width/height).
Derek Jones4b9c6292011-07-01 17:40:48 -0500218 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 * properties using ImageMagick and NetPBM
220 *
221 */
222 if ( ! function_exists('getimagesize'))
223 {
224 $this->set_error('imglib_gd_required_for_props');
225 return FALSE;
226 }
227
228 $this->image_library = strtolower($this->image_library);
229
230 /*
231 * Set the full server path
232 *
233 * The source image may or may not contain a path.
234 * Either way, we'll try use realpath to generate the
235 * full server path in order to more reliably read it.
236 *
237 */
238 if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE)
239 {
240 $full_source_path = str_replace("\\", "/", realpath($this->source_image));
241 }
242 else
243 {
244 $full_source_path = $this->source_image;
245 }
246
247 $x = explode('/', $full_source_path);
248 $this->source_image = end($x);
249 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
250
251 // Set the Image Properties
252 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
253 {
Eric Barnesb1673362011-12-05 22:05:38 -0500254 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
256
257 /*
258 * Assign the "new" image name/path
259 *
260 * If the user has set a "new_image" name it means
261 * we are making a copy of the source image. If not
Derek Jones4b9c6292011-07-01 17:40:48 -0500262 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 * set the destination filename and path accordingly.
264 *
265 */
266 if ($this->new_image == '')
267 {
268 $this->dest_image = $this->source_image;
269 $this->dest_folder = $this->source_folder;
270 }
271 else
272 {
273 if (strpos($this->new_image, '/') === FALSE)
274 {
275 $this->dest_folder = $this->source_folder;
276 $this->dest_image = $this->new_image;
277 }
278 else
279 {
280 if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE)
281 {
282 $full_dest_path = str_replace("\\", "/", realpath($this->new_image));
283 }
284 else
285 {
286 $full_dest_path = $this->new_image;
287 }
288
289 // Is there a file name?
290 if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
291 {
292 $this->dest_folder = $full_dest_path.'/';
293 $this->dest_image = $this->source_image;
294 }
295 else
296 {
297 $x = explode('/', $full_dest_path);
298 $this->dest_image = end($x);
299 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
300 }
301 }
302 }
303
304 /*
305 * Compile the finalized filenames/paths
306 *
307 * We'll create two master strings containing the
308 * full server path to the source image and the
309 * full server path to the destination image.
310 * We'll also split the destination image name
311 * so we can insert the thumbnail marker if needed.
312 *
313 */
314 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
315 {
316 $this->thumb_marker = '';
317 }
318
319 $xp = $this->explode_name($this->dest_image);
320
321 $filename = $xp['name'];
322 $file_ext = $xp['ext'];
323
324 $this->full_src_path = $this->source_folder.$this->source_image;
325 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
326
327 /*
328 * Should we maintain image proportions?
329 *
330 * When creating thumbs or copies, the target width/height
331 * might not be in correct proportion with the source
Derek Jones4b9c6292011-07-01 17:40:48 -0500332 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 *
334 */
335 if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
336 {
337 $this->image_reproportion();
338 }
339
340 /*
341 * Was a width and height specified?
342 *
343 * If the destination width/height was
344 * not submitted we will use the values
345 * from the actual file
346 *
347 */
348 if ($this->width == '')
349 $this->width = $this->orig_width;
350
351 if ($this->height == '')
352 $this->height = $this->orig_height;
353
354 // Set the quality
355 $this->quality = trim(str_replace("%", "", $this->quality));
356
357 if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality))
358 $this->quality = 90;
359
360 // Set the x/y coordinates
361 $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis;
362 $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis;
363
364 // Watermark-related Stuff...
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 if ($this->wm_overlay_path != '')
366 {
367 $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path));
368 }
369
370 if ($this->wm_shadow_color != '')
371 {
372 $this->wm_use_drop_shadow = TRUE;
373 }
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200374 elseif ($this->wm_use_drop_shadow == TRUE AND $this->wm_shadow_color == '')
375 {
376 $this->wm_use_drop_shadow = FALSE;
377 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000378
379 if ($this->wm_font_path != '')
380 {
381 $this->wm_use_truetype = TRUE;
382 }
383
384 return TRUE;
385 }
386
387 // --------------------------------------------------------------------
388
389 /**
390 * Image Resize
391 *
392 * This is a wrapper function that chooses the proper
393 * resize function based on the protocol specified
394 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 * @return bool
396 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200397 public function resize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200399 $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 return $this->$protocol('resize');
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Image Crop
407 *
408 * This is a wrapper function that chooses the proper
409 * cropping function based on the protocol specified
410 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @return bool
412 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200413 public function crop()
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200415 $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 return $this->$protocol('crop');
417 }
418
419 // --------------------------------------------------------------------
420
421 /**
422 * Image Rotate
423 *
424 * This is a wrapper function that chooses the proper
425 * rotation function based on the protocol specified
426 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 * @return bool
428 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200429 public function rotate()
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
431 // Allowed rotation values
432 $degs = array(90, 180, 270, 'vrt', 'hor');
433
Derek Allardd9c7f032008-12-01 20:18:00 +0000434 if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
436 $this->set_error('imglib_rotation_angle_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500437 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
439
440 // Reassign the width and height
441 if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
442 {
443 $this->width = $this->orig_height;
444 $this->height = $this->orig_width;
445 }
446 else
447 {
448 $this->width = $this->orig_width;
449 $this->height = $this->orig_height;
450 }
451
452
453 // Choose resizing function
454 if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm')
455 {
456 $protocol = 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 return $this->$protocol('rotate');
458 }
459
460 if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
461 {
462 return $this->image_mirror_gd();
463 }
464 else
465 {
466 return $this->image_rotate_gd();
467 }
468 }
469
470 // --------------------------------------------------------------------
471
472 /**
473 * Image Process Using GD/GD2
474 *
475 * This function will resize or crop
476 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 * @param string
478 * @return bool
479 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200480 public function image_process_gd($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
482 $v2_override = FALSE;
483
484 // If the target width/height match the source, AND if the new file name is not equal to the old file name
485 // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
Andrey Andreeva92b9032011-12-24 19:05:58 +0200486 if ($this->dynamic_output === FALSE AND $this->orig_width == $this->width AND $this->orig_height == $this->height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200488 if ($this->source_image != $this->new_image AND @copy($this->full_src_path, $this->full_dst_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200490 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200492
493 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
495
496 // Let's set up our values based on the action
497 if ($action == 'crop')
498 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500499 // Reassign the source width/height if cropping
500 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 $this->orig_height = $this->height;
502
503 // GD 2.0 has a cropping bug so we'll test for it
504 if ($this->gd_version() !== FALSE)
505 {
506 $gd_version = str_replace('0', '', $this->gd_version());
507 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
508 }
509 }
510 else
511 {
512 // If resizing the x/y axis must be zero
513 $this->x_axis = 0;
514 $this->y_axis = 0;
515 }
516
Derek Jones4b9c6292011-07-01 17:40:48 -0500517 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 if ( ! ($src_img = $this->image_create_gd()))
519 {
520 return FALSE;
521 }
522
Derek Jones4b9c6292011-07-01 17:40:48 -0500523 // Create The Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500525 // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
526 // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
527 // below should that ever prove inaccurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500529 // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200530 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 {
532 $create = 'imagecreatetruecolor';
533 $copy = 'imagecopyresampled';
534 }
535 else
536 {
537 $create = 'imagecreate';
538 $copy = 'imagecopyresized';
539 }
540
541 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500542
543 if ($this->image_type == 3) // png we can actually preserve transparency
544 {
545 imagealphablending($dst_img, FALSE);
546 imagesavealpha($dst_img, TRUE);
547 }
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
550
Derek Jones4b9c6292011-07-01 17:40:48 -0500551 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 if ($this->dynamic_output == TRUE)
553 {
554 $this->image_display_gd($dst_img);
555 }
556 else
557 {
558 // Or save it
559 if ( ! $this->image_save_gd($dst_img))
560 {
561 return FALSE;
562 }
563 }
564
Derek Jones4b9c6292011-07-01 17:40:48 -0500565 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 imagedestroy($dst_img);
567 imagedestroy($src_img);
568
569 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000570 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000571
572 return TRUE;
573 }
574
575 // --------------------------------------------------------------------
576
577 /**
578 * Image Process Using ImageMagick
579 *
580 * This function will resize, crop or rotate
581 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * @param string
583 * @return bool
584 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200585 public function image_process_imagemagick($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500587 // Do we have a vaild library path?
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 if ($this->library_path == '')
589 {
590 $this->set_error('imglib_libpath_invalid');
591 return FALSE;
592 }
593
Derek Jones1322ec52009-03-11 17:01:14 +0000594 if ( ! preg_match("/convert$/i", $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200596 $this->library_path = rtrim($this->library_path, '/').'/convert';
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 }
598
599 // Execute the command
600 $cmd = $this->library_path." -quality ".$this->quality;
601
602 if ($action == 'crop')
603 {
604 $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
605 }
606 elseif ($action == 'rotate')
607 {
608 switch ($this->rotation_angle)
609 {
Barry Mienydd671972010-10-04 16:33:58 +0200610 case 'hor' : $angle = '-flop';
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 break;
Barry Mienydd671972010-10-04 16:33:58 +0200612 case 'vrt' : $angle = '-flip';
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 break;
614 default : $angle = '-rotate '.$this->rotation_angle;
615 break;
616 }
617
618 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
619 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500620 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
622 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
623 }
624
625 $retval = 1;
626
627 @exec($cmd, $output, $retval);
628
629 // Did it work?
630 if ($retval > 0)
631 {
632 $this->set_error('imglib_image_process_failed');
633 return FALSE;
634 }
635
636 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000637 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000638
639 return TRUE;
640 }
641
642 // --------------------------------------------------------------------
643
644 /**
645 * Image Process Using NetPBM
646 *
647 * This function will resize, crop or rotate
648 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 * @param string
650 * @return bool
651 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200652 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 {
654 if ($this->library_path == '')
655 {
656 $this->set_error('imglib_libpath_invalid');
657 return FALSE;
658 }
659
Derek Jones4b9c6292011-07-01 17:40:48 -0500660 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 switch ($this->image_type)
662 {
663 case 1 :
664 $cmd_in = 'giftopnm';
665 $cmd_out = 'ppmtogif';
666 break;
667 case 2 :
668 $cmd_in = 'jpegtopnm';
669 $cmd_out = 'ppmtojpeg';
670 break;
671 case 3 :
672 $cmd_in = 'pngtopnm';
673 $cmd_out = 'ppmtopng';
674 break;
675 }
676
677 if ($action == 'crop')
678 {
679 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
680 }
681 elseif ($action == 'rotate')
682 {
683 switch ($this->rotation_angle)
684 {
685 case 90 : $angle = 'r270';
686 break;
687 case 180 : $angle = 'r180';
688 break;
Barry Mienydd671972010-10-04 16:33:58 +0200689 case 270 : $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 break;
691 case 'vrt' : $angle = 'tb';
692 break;
693 case 'hor' : $angle = 'lr';
694 break;
695 }
696
697 $cmd_inner = 'pnmflip -'.$angle.' ';
698 }
699 else // Resize
700 {
701 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
702 }
703
704 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
705
706 $retval = 1;
707
708 @exec($cmd, $output, $retval);
709
Derek Jones4b9c6292011-07-01 17:40:48 -0500710 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 if ($retval > 0)
712 {
713 $this->set_error('imglib_image_process_failed');
714 return FALSE;
715 }
716
717 // With NetPBM we have to create a temporary image.
718 // If you try manipulating the original it fails so
719 // we have to rename the temp file.
720 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
721 unlink ($this->dest_folder.'netpbm.tmp');
Derek Jones172e1612009-10-13 14:32:48 +0000722 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000723
724 return TRUE;
725 }
726
727 // --------------------------------------------------------------------
728
729 /**
730 * Image Rotate Using GD
731 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 * @return bool
733 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200734 public function image_rotate_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500736 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 if ( ! ($src_img = $this->image_create_gd()))
738 {
739 return FALSE;
740 }
741
742 // Set the background color
743 // This won't work with transparent PNG files so we are
744 // going to have to figure out how to determine the color
745 // of the alpha channel in a future release.
746
747 $white = imagecolorallocate($src_img, 255, 255, 255);
748
Derek Jones4b9c6292011-07-01 17:40:48 -0500749 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
751
Derek Jones4b9c6292011-07-01 17:40:48 -0500752 // Save the Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 if ($this->dynamic_output == TRUE)
754 {
755 $this->image_display_gd($dst_img);
756 }
757 else
758 {
759 // Or save it
760 if ( ! $this->image_save_gd($dst_img))
761 {
762 return FALSE;
763 }
764 }
765
Derek Jones4b9c6292011-07-01 17:40:48 -0500766 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 imagedestroy($dst_img);
768 imagedestroy($src_img);
769
770 // Set the file to 777
771
Derek Jones172e1612009-10-13 14:32:48 +0000772 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000773
Pascal Kriete8761ef52011-02-14 13:13:52 -0500774 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 }
776
777 // --------------------------------------------------------------------
778
779 /**
780 * Create Mirror Image using GD
781 *
782 * This function will flip horizontal or vertical
783 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 * @return bool
785 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200786 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 {
788 if ( ! $src_img = $this->image_create_gd())
789 {
790 return FALSE;
791 }
792
Derek Jones4b9c6292011-07-01 17:40:48 -0500793 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 $height = $this->orig_height;
795
796 if ($this->rotation_angle == 'hor')
797 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200798 for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 while ($left < $right)
801 {
802 $cl = imagecolorat($src_img, $left, $i);
803 $cr = imagecolorat($src_img, $right, $i);
804
805 imagesetpixel($src_img, $left, $i, $cr);
806 imagesetpixel($src_img, $right, $i, $cl);
807
808 $left++;
809 $right--;
810 }
811 }
812 }
813 else
814 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200815 for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 while ($top < $bot)
818 {
819 $ct = imagecolorat($src_img, $i, $top);
820 $cb = imagecolorat($src_img, $i, $bot);
821
822 imagesetpixel($src_img, $i, $top, $cb);
823 imagesetpixel($src_img, $i, $bot, $ct);
824
825 $top++;
826 $bot--;
827 }
828 }
829 }
830
Derek Jones4b9c6292011-07-01 17:40:48 -0500831 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 if ($this->dynamic_output == TRUE)
833 {
834 $this->image_display_gd($src_img);
835 }
836 else
837 {
838 // Or save it
839 if ( ! $this->image_save_gd($src_img))
840 {
841 return FALSE;
842 }
843 }
844
Derek Jones4b9c6292011-07-01 17:40:48 -0500845 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 imagedestroy($src_img);
847
848 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000849 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000850
851 return TRUE;
852 }
853
854 // --------------------------------------------------------------------
855
856 /**
857 * Image Watermark
858 *
859 * This is a wrapper function that chooses the type
860 * of watermarking based on the specified preference.
861 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 * @param string
863 * @return bool
864 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200865 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 {
867 if ($this->wm_type == 'overlay')
868 {
869 return $this->overlay_watermark();
870 }
871 else
872 {
873 return $this->text_watermark();
874 }
875 }
876
877 // --------------------------------------------------------------------
878
879 /**
880 * Watermark - Graphic Version
881 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 * @return bool
883 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200884 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
886 if ( ! function_exists('imagecolortransparent'))
887 {
888 $this->set_error('imglib_gd_required');
889 return FALSE;
890 }
891
Derek Jones4b9c6292011-07-01 17:40:48 -0500892 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 $this->get_image_properties();
894
Derek Jones4b9c6292011-07-01 17:40:48 -0500895 // Fetch watermark image properties
Barry Mienydd671972010-10-04 16:33:58 +0200896 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 $wm_img_type = $props['image_type'];
898 $wm_width = $props['width'];
899 $wm_height = $props['height'];
900
Derek Jones4b9c6292011-07-01 17:40:48 -0500901 // Create two image resources
902 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 $src_img = $this->image_create_gd($this->full_src_path);
904
905 // Reverse the offset if necessary
906 // When the image is positioned at the bottom
907 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -0500908 // further down. We want the reverse, so we'll
909 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 // offset when the image is at the right
911
912 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
913 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
914
915 if ($this->wm_vrt_alignment == 'B')
916 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
917
918 if ($this->wm_hor_alignment == 'R')
919 $this->wm_hor_offset = $this->wm_hor_offset * -1;
920
Derek Jones4b9c6292011-07-01 17:40:48 -0500921 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 $x_axis = $this->wm_hor_offset + $this->wm_padding;
923 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
924
Derek Jones4b9c6292011-07-01 17:40:48 -0500925 // Set the vertical position
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 switch ($this->wm_vrt_alignment)
927 {
928 case 'T':
929 break;
930 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
931 break;
932 case 'B': $y_axis += $this->orig_height - $wm_height;
933 break;
934 }
935
Derek Jones4b9c6292011-07-01 17:40:48 -0500936 // Set the horizontal position
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 switch ($this->wm_hor_alignment)
938 {
939 case 'L':
940 break;
941 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
942 break;
943 case 'R': $x_axis += $this->orig_width - $wm_width;
944 break;
945 }
946
Derek Jones4b9c6292011-07-01 17:40:48 -0500947 // Build the finalized image
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
949 {
950 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200951 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000952
953 // Set RGB values for text and shadow
954 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
955 $alpha = ($rgba & 0x7F000000) >> 24;
956
957 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
958 if ($alpha > 0)
959 {
960 // copy the image directly, the image's alpha transparency being the sole determinant of blending
961 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
962 }
963 else
964 {
965 // set our RGB value from above to be transparent and merge the images with the specified opacity
966 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
967 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
968 }
969
Derek Jones4b9c6292011-07-01 17:40:48 -0500970 // Output the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 if ($this->dynamic_output == TRUE)
972 {
973 $this->image_display_gd($src_img);
974 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200975 elseif ( ! $this->image_save_gd($src_img))
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200977 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 }
979
980 imagedestroy($src_img);
981 imagedestroy($wm_img);
982
983 return TRUE;
984 }
985
986 // --------------------------------------------------------------------
987
988 /**
989 * Watermark - Text Version
990 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 * @return bool
992 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200993 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
995 if ( ! ($src_img = $this->image_create_gd()))
996 {
997 return FALSE;
998 }
999
1000 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
1001 {
1002 $this->set_error('imglib_missing_font');
1003 return FALSE;
1004 }
1005
Derek Jones4b9c6292011-07-01 17:40:48 -05001006 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 $this->get_image_properties();
1008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 // Reverse the vertical offset
1010 // When the image is positioned at the bottom
1011 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -05001012 // further down. We want the reverse, so we'll
1013 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 // offset flips itself automatically
1015
1016 if ($this->wm_vrt_alignment == 'B')
1017 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1018
1019 if ($this->wm_hor_alignment == 'R')
1020 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1021
1022 // Set font width and height
1023 // These are calculated differently depending on
1024 // whether we are using the true type font or not
1025 if ($this->wm_use_truetype == TRUE)
1026 {
1027 if ($this->wm_font_size == '')
Andrey Andreeva92b9032011-12-24 19:05:58 +02001028 {
1029 $this->wm_font_size = 17;
1030 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001031
Derek Jones4b9c6292011-07-01 17:40:48 -05001032 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 $fontheight = $this->wm_font_size;
1034 $this->wm_vrt_offset += $this->wm_font_size;
1035 }
1036 else
1037 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001038 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 $fontheight = imagefontheight($this->wm_font_size);
1040 }
1041
1042 // Set base X and Y axis values
1043 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1044 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1045
1046 // Set verticle alignment
1047 if ($this->wm_use_drop_shadow == FALSE)
1048 $this->wm_shadow_distance = 0;
1049
1050 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1051 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1052
1053 switch ($this->wm_vrt_alignment)
1054 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001055 case 'T':
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001057 case 'M': $y_axis += ($this->orig_height/2)+($fontheight/2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001059 case 'B': $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 break;
1061 }
1062
1063 $x_shad = $x_axis + $this->wm_shadow_distance;
1064 $y_shad = $y_axis + $this->wm_shadow_distance;
1065
1066 // Set horizontal alignment
1067 switch ($this->wm_hor_alignment)
1068 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001069 case 'L':
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001071 case 'R':
1072 if ($this->wm_use_drop_shadow)
1073 {
1074 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1075 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1076 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001078 case 'C':
1079 if ($this->wm_use_drop_shadow)
1080 {
1081 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1082 $x_axis += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1083 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 break;
1085 }
1086
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001087 if ($this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 {
Andrey Andreev8323ae62011-12-31 18:39:10 +02001089 /* Set RGB values for text and shadow
1090 *
1091 * First character is #, so we don't really need it.
1092 * Get the rest of the string and split it into 2-length
1093 * hex values:
1094 */
1095 $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2);
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001096 $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
Andrey Andreev8323ae62011-12-31 18:39:10 +02001097 $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2);
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001098 $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[2]), hexdec($drp_color[3]));
1099
1100 // Add the text to the source image
1101 if ($this->wm_use_truetype)
1102 {
1103 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1104 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1105 }
1106 else
1107 {
1108 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1109 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1110 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 }
1112
Derek Jones4b9c6292011-07-01 17:40:48 -05001113 // Output the final image
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 if ($this->dynamic_output == TRUE)
1115 {
1116 $this->image_display_gd($src_img);
1117 }
1118 else
1119 {
1120 $this->image_save_gd($src_img);
1121 }
1122
1123 imagedestroy($src_img);
1124
1125 return TRUE;
1126 }
1127
1128 // --------------------------------------------------------------------
1129
1130 /**
1131 * Create Image - GD
1132 *
1133 * This simply creates an image resource handle
1134 * based on the type of image being processed
1135 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 * @param string
1137 * @return resource
1138 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001139 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
1141 if ($path == '')
1142 $path = $this->full_src_path;
1143
1144 if ($image_type == '')
1145 $image_type = $this->image_type;
1146
1147
1148 switch ($image_type)
1149 {
1150 case 1 :
1151 if ( ! function_exists('imagecreatefromgif'))
1152 {
1153 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1154 return FALSE;
1155 }
1156
1157 return imagecreatefromgif($path);
1158 break;
1159 case 2 :
1160 if ( ! function_exists('imagecreatefromjpeg'))
1161 {
1162 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1163 return FALSE;
1164 }
1165
1166 return imagecreatefromjpeg($path);
1167 break;
1168 case 3 :
1169 if ( ! function_exists('imagecreatefrompng'))
1170 {
1171 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1172 return FALSE;
1173 }
1174
1175 return imagecreatefrompng($path);
1176 break;
1177
1178 }
1179
1180 $this->set_error(array('imglib_unsupported_imagecreate'));
1181 return FALSE;
1182 }
1183
1184 // --------------------------------------------------------------------
1185
1186 /**
1187 * Write image file to disk - GD
1188 *
1189 * Takes an image resource as input and writes the file
1190 * to the specified destination
1191 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 * @param resource
1193 * @return bool
1194 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001195 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 {
1197 switch ($this->image_type)
1198 {
1199 case 1 :
1200 if ( ! function_exists('imagegif'))
1201 {
1202 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1203 return FALSE;
1204 }
1205
Derek Jones541ddbd2008-12-09 15:25:31 +00001206 if ( ! @imagegif($resource, $this->full_dst_path))
1207 {
1208 $this->set_error('imglib_save_failed');
1209 return FALSE;
1210 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 break;
1212 case 2 :
1213 if ( ! function_exists('imagejpeg'))
1214 {
1215 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1216 return FALSE;
1217 }
1218
Derek Jones541ddbd2008-12-09 15:25:31 +00001219 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1220 {
1221 $this->set_error('imglib_save_failed');
1222 return FALSE;
1223 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 break;
1225 case 3 :
1226 if ( ! function_exists('imagepng'))
1227 {
1228 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1229 return FALSE;
1230 }
1231
Derek Jones541ddbd2008-12-09 15:25:31 +00001232 if ( ! @imagepng($resource, $this->full_dst_path))
1233 {
1234 $this->set_error('imglib_save_failed');
1235 return FALSE;
1236 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 break;
1238 default :
1239 $this->set_error(array('imglib_unsupported_imagecreate'));
1240 return FALSE;
1241 break;
1242 }
1243
1244 return TRUE;
1245 }
1246
1247 // --------------------------------------------------------------------
1248
1249 /**
1250 * Dynamically outputs an image
1251 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 * @param resource
1253 * @return void
1254 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001255 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 {
1257 header("Content-Disposition: filename={$this->source_image};");
1258 header("Content-Type: {$this->mime_type}");
1259 header('Content-Transfer-Encoding: binary');
1260 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1261
1262 switch ($this->image_type)
1263 {
Barry Mienydd671972010-10-04 16:33:58 +02001264 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 break;
1266 case 2 : imagejpeg($resource, '', $this->quality);
1267 break;
1268 case 3 : imagepng($resource);
1269 break;
1270 default : echo 'Unable to display the image';
1271 break;
1272 }
1273 }
1274
1275 // --------------------------------------------------------------------
1276
1277 /**
1278 * Re-proportion Image Width/Height
1279 *
1280 * When creating thumbs, the desired width/height
1281 * can end up warping the image due to an incorrect
1282 * ratio between the full-sized image and the thumb.
1283 *
1284 * This function lets us re-proportion the width/height
1285 * if users choose to maintain the aspect ratio when resizing.
1286 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 * @return void
1288 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001289 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 {
1291 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
1292 return;
1293
1294 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
1295 return;
1296
1297 $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
1298 $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
1299
1300 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
1301
1302 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
1303 {
1304 $this->master_dim = ($ratio < 0) ? 'width' : 'height';
1305 }
1306
1307 if (($this->width != $new_width) AND ($this->height != $new_height))
1308 {
1309 if ($this->master_dim == 'height')
1310 {
1311 $this->width = $new_width;
1312 }
1313 else
1314 {
1315 $this->height = $new_height;
1316 }
1317 }
1318 }
1319
1320 // --------------------------------------------------------------------
1321
1322 /**
1323 * Get image properties
1324 *
1325 * A helper function that gets info about the file
1326 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 * @param string
1328 * @return mixed
1329 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001330 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 {
1332 // For now we require GD but we should
1333 // find a way to determine this using IM or NetPBM
1334
1335 if ($path == '')
1336 $path = $this->full_src_path;
1337
1338 if ( ! file_exists($path))
1339 {
1340 $this->set_error('imglib_invalid_path');
1341 return FALSE;
1342 }
1343
Phil Sturgeon901998a2011-08-26 10:03:33 +01001344 $vals = getimagesize($path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
Andrey Andreeva92b9032011-12-24 19:05:58 +02001346 $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001347
1348 if ($return == TRUE)
1349 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001350 return array(
1351 'width' => $vals[0],
1352 'height' => $vals[1],
1353 'image_type' => $vals[2],
1354 'size_str' => $vals[3],
1355 'mime_type' => $mime
1356 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 }
1358
Andrey Andreeva92b9032011-12-24 19:05:58 +02001359 $this->orig_width = $vals[0];
1360 $this->orig_height = $vals[1];
1361 $this->image_type = $vals[2];
1362 $this->size_str = $vals[3];
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 $this->mime_type = $mime;
1364
1365 return TRUE;
1366 }
1367
1368 // --------------------------------------------------------------------
1369
1370 /**
1371 * Size calculator
1372 *
1373 * This function takes a known width x height and
Derek Jones4b9c6292011-07-01 17:40:48 -05001374 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 * new variable needs to be known
1376 *
1377 * $props = array(
Barry Mienydd671972010-10-04 16:33:58 +02001378 * 'width' => $width,
1379 * 'height' => $height,
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 * 'new_width' => 40,
1381 * 'new_height' => ''
Derek Jones4b9c6292011-07-01 17:40:48 -05001382 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 * @param array
1385 * @return array
1386 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001387 public function size_calculator($vals)
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 {
1389 if ( ! is_array($vals))
1390 {
1391 return;
1392 }
1393
1394 $allowed = array('new_width', 'new_height', 'width', 'height');
1395
1396 foreach ($allowed as $item)
1397 {
1398 if ( ! isset($vals[$item]) OR $vals[$item] == '')
1399 $vals[$item] = 0;
1400 }
1401
1402 if ($vals['width'] == 0 OR $vals['height'] == 0)
1403 {
1404 return $vals;
1405 }
1406
1407 if ($vals['new_width'] == 0)
1408 {
1409 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1410 }
1411 elseif ($vals['new_height'] == 0)
1412 {
1413 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1414 }
1415
1416 return $vals;
1417 }
1418
1419 // --------------------------------------------------------------------
1420
1421 /**
1422 * Explode source_image
1423 *
1424 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001425 * from the source_image. This function lets us deal with
1426 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001428 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001429 * $array['name'] = 'my.cool';
1430 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001431 * @param array
1432 * @return array
1433 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001434 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001435 {
Derek Jones08cae632009-02-10 20:03:29 +00001436 $ext = strrchr($source_image, '.');
1437 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001438
Derek Jones08cae632009-02-10 20:03:29 +00001439 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 }
1441
1442 // --------------------------------------------------------------------
1443
1444 /**
1445 * Is GD Installed?
1446 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 * @return bool
1448 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001449 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001450 {
1451 if ( ! extension_loaded('gd'))
1452 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001453 /* As it is stated in the PHP manual, dl() is not always available
1454 * and even if so - it could generate an E_WARNING message on failure
1455 */
1456 return (function_exists('dl') AND @dl('gd.so'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 }
1458
1459 return TRUE;
1460 }
1461
1462 // --------------------------------------------------------------------
1463
1464 /**
1465 * Get GD version
1466 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001467 * @return mixed
1468 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001469 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001470 {
1471 if (function_exists('gd_info'))
1472 {
1473 $gd_version = @gd_info();
1474 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
1475
1476 return $gd_version;
1477 }
1478
1479 return FALSE;
1480 }
1481
1482 // --------------------------------------------------------------------
1483
1484 /**
1485 * Set error message
1486 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001487 * @param string
1488 * @return void
1489 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001490 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001491 {
1492 $CI =& get_instance();
1493 $CI->lang->load('imglib');
1494
1495 if (is_array($msg))
1496 {
1497 foreach ($msg as $val)
1498 {
1499
1500 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1501 $this->error_msg[] = $msg;
1502 log_message('error', $msg);
1503 }
1504 }
1505 else
1506 {
1507 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1508 $this->error_msg[] = $msg;
1509 log_message('error', $msg);
1510 }
1511 }
1512
1513 // --------------------------------------------------------------------
1514
1515 /**
1516 * Show error messages
1517 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 * @param string
1519 * @return string
1520 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001521 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 {
Andrey Andreev4eea9892011-12-19 12:05:41 +02001523 return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 }
1525
1526}
1527// END Image_lib Class
1528
1529/* End of file Image_lib.php */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001530/* Location: ./system/libraries/Image_lib.php */