blob: d4fb51923f3d0b4f681f55d4bb9e91aa1f4c1ab4 [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 {
172 if ($val != '' AND preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches))
173 {
174 if (strlen($matches[1]) === 6)
175 {
176 $val = '#'.$val;
177 }
178 else
179 {
180 $val = str_split($val, 1);
181 $val = '#'.$val[0].$val[0].$val[1].$val[1].$val[2].$val[2];
182 }
183 }
184 else
185 {
186 continue;
187 }
188 }
189
190 $this->$key = $val;
191 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
193 }
194
195 /*
196 * Is there a source image?
197 *
198 * If not, there's no reason to continue
199 *
200 */
201 if ($this->source_image == '')
202 {
203 $this->set_error('imglib_source_image_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500204 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
206
207 /*
208 * Is getimagesize() Available?
209 *
210 * We use it to determine the image properties (width/height).
Derek Jones4b9c6292011-07-01 17:40:48 -0500211 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 * properties using ImageMagick and NetPBM
213 *
214 */
215 if ( ! function_exists('getimagesize'))
216 {
217 $this->set_error('imglib_gd_required_for_props');
218 return FALSE;
219 }
220
221 $this->image_library = strtolower($this->image_library);
222
223 /*
224 * Set the full server path
225 *
226 * The source image may or may not contain a path.
227 * Either way, we'll try use realpath to generate the
228 * full server path in order to more reliably read it.
229 *
230 */
231 if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE)
232 {
233 $full_source_path = str_replace("\\", "/", realpath($this->source_image));
234 }
235 else
236 {
237 $full_source_path = $this->source_image;
238 }
239
240 $x = explode('/', $full_source_path);
241 $this->source_image = end($x);
242 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
243
244 // Set the Image Properties
245 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
246 {
Eric Barnesb1673362011-12-05 22:05:38 -0500247 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249
250 /*
251 * Assign the "new" image name/path
252 *
253 * If the user has set a "new_image" name it means
254 * we are making a copy of the source image. If not
Derek Jones4b9c6292011-07-01 17:40:48 -0500255 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 * set the destination filename and path accordingly.
257 *
258 */
259 if ($this->new_image == '')
260 {
261 $this->dest_image = $this->source_image;
262 $this->dest_folder = $this->source_folder;
263 }
264 else
265 {
266 if (strpos($this->new_image, '/') === FALSE)
267 {
268 $this->dest_folder = $this->source_folder;
269 $this->dest_image = $this->new_image;
270 }
271 else
272 {
273 if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE)
274 {
275 $full_dest_path = str_replace("\\", "/", realpath($this->new_image));
276 }
277 else
278 {
279 $full_dest_path = $this->new_image;
280 }
281
282 // Is there a file name?
283 if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
284 {
285 $this->dest_folder = $full_dest_path.'/';
286 $this->dest_image = $this->source_image;
287 }
288 else
289 {
290 $x = explode('/', $full_dest_path);
291 $this->dest_image = end($x);
292 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
293 }
294 }
295 }
296
297 /*
298 * Compile the finalized filenames/paths
299 *
300 * We'll create two master strings containing the
301 * full server path to the source image and the
302 * full server path to the destination image.
303 * We'll also split the destination image name
304 * so we can insert the thumbnail marker if needed.
305 *
306 */
307 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
308 {
309 $this->thumb_marker = '';
310 }
311
312 $xp = $this->explode_name($this->dest_image);
313
314 $filename = $xp['name'];
315 $file_ext = $xp['ext'];
316
317 $this->full_src_path = $this->source_folder.$this->source_image;
318 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
319
320 /*
321 * Should we maintain image proportions?
322 *
323 * When creating thumbs or copies, the target width/height
324 * might not be in correct proportion with the source
Derek Jones4b9c6292011-07-01 17:40:48 -0500325 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 *
327 */
328 if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
329 {
330 $this->image_reproportion();
331 }
332
333 /*
334 * Was a width and height specified?
335 *
336 * If the destination width/height was
337 * not submitted we will use the values
338 * from the actual file
339 *
340 */
341 if ($this->width == '')
342 $this->width = $this->orig_width;
343
344 if ($this->height == '')
345 $this->height = $this->orig_height;
346
347 // Set the quality
348 $this->quality = trim(str_replace("%", "", $this->quality));
349
350 if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality))
351 $this->quality = 90;
352
353 // Set the x/y coordinates
354 $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis;
355 $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis;
356
357 // Watermark-related Stuff...
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 if ($this->wm_overlay_path != '')
359 {
360 $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path));
361 }
362
363 if ($this->wm_shadow_color != '')
364 {
365 $this->wm_use_drop_shadow = TRUE;
366 }
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200367 elseif ($this->wm_use_drop_shadow == TRUE AND $this->wm_shadow_color == '')
368 {
369 $this->wm_use_drop_shadow = FALSE;
370 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000371
372 if ($this->wm_font_path != '')
373 {
374 $this->wm_use_truetype = TRUE;
375 }
376
377 return TRUE;
378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * Image Resize
384 *
385 * This is a wrapper function that chooses the proper
386 * resize function based on the protocol specified
387 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 * @return bool
389 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200390 public function resize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200392 $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 return $this->$protocol('resize');
394 }
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Image Crop
400 *
401 * This is a wrapper function that chooses the proper
402 * cropping function based on the protocol specified
403 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * @return bool
405 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200406 public function crop()
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200408 $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 return $this->$protocol('crop');
410 }
411
412 // --------------------------------------------------------------------
413
414 /**
415 * Image Rotate
416 *
417 * This is a wrapper function that chooses the proper
418 * rotation function based on the protocol specified
419 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 * @return bool
421 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200422 public function rotate()
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 {
424 // Allowed rotation values
425 $degs = array(90, 180, 270, 'vrt', 'hor');
426
Derek Allardd9c7f032008-12-01 20:18:00 +0000427 if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
429 $this->set_error('imglib_rotation_angle_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500430 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432
433 // Reassign the width and height
434 if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
435 {
436 $this->width = $this->orig_height;
437 $this->height = $this->orig_width;
438 }
439 else
440 {
441 $this->width = $this->orig_width;
442 $this->height = $this->orig_height;
443 }
444
445
446 // Choose resizing function
447 if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm')
448 {
449 $protocol = 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 return $this->$protocol('rotate');
451 }
452
453 if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
454 {
455 return $this->image_mirror_gd();
456 }
457 else
458 {
459 return $this->image_rotate_gd();
460 }
461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * Image Process Using GD/GD2
467 *
468 * This function will resize or crop
469 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @param string
471 * @return bool
472 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200473 public function image_process_gd($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 $v2_override = FALSE;
476
477 // If the target width/height match the source, AND if the new file name is not equal to the old file name
478 // 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 +0200479 if ($this->dynamic_output === FALSE AND $this->orig_width == $this->width AND $this->orig_height == $this->height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200481 if ($this->source_image != $this->new_image AND @copy($this->full_src_path, $this->full_dst_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200483 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200485
486 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488
489 // Let's set up our values based on the action
490 if ($action == 'crop')
491 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500492 // Reassign the source width/height if cropping
493 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 $this->orig_height = $this->height;
495
496 // GD 2.0 has a cropping bug so we'll test for it
497 if ($this->gd_version() !== FALSE)
498 {
499 $gd_version = str_replace('0', '', $this->gd_version());
500 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
501 }
502 }
503 else
504 {
505 // If resizing the x/y axis must be zero
506 $this->x_axis = 0;
507 $this->y_axis = 0;
508 }
509
Derek Jones4b9c6292011-07-01 17:40:48 -0500510 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 if ( ! ($src_img = $this->image_create_gd()))
512 {
513 return FALSE;
514 }
515
Derek Jones4b9c6292011-07-01 17:40:48 -0500516 // Create The Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500518 // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
519 // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
520 // below should that ever prove inaccurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500522 // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200523 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 {
525 $create = 'imagecreatetruecolor';
526 $copy = 'imagecopyresampled';
527 }
528 else
529 {
530 $create = 'imagecreate';
531 $copy = 'imagecopyresized';
532 }
533
534 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500535
536 if ($this->image_type == 3) // png we can actually preserve transparency
537 {
538 imagealphablending($dst_img, FALSE);
539 imagesavealpha($dst_img, TRUE);
540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
543
Derek Jones4b9c6292011-07-01 17:40:48 -0500544 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 if ($this->dynamic_output == TRUE)
546 {
547 $this->image_display_gd($dst_img);
548 }
549 else
550 {
551 // Or save it
552 if ( ! $this->image_save_gd($dst_img))
553 {
554 return FALSE;
555 }
556 }
557
Derek Jones4b9c6292011-07-01 17:40:48 -0500558 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 imagedestroy($dst_img);
560 imagedestroy($src_img);
561
562 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000563 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000564
565 return TRUE;
566 }
567
568 // --------------------------------------------------------------------
569
570 /**
571 * Image Process Using ImageMagick
572 *
573 * This function will resize, crop or rotate
574 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 * @param string
576 * @return bool
577 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200578 public function image_process_imagemagick($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500580 // Do we have a vaild library path?
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 if ($this->library_path == '')
582 {
583 $this->set_error('imglib_libpath_invalid');
584 return FALSE;
585 }
586
Derek Jones1322ec52009-03-11 17:01:14 +0000587 if ( ! preg_match("/convert$/i", $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200589 $this->library_path = rtrim($this->library_path, '/').'/convert';
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
591
592 // Execute the command
593 $cmd = $this->library_path." -quality ".$this->quality;
594
595 if ($action == 'crop')
596 {
597 $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
598 }
599 elseif ($action == 'rotate')
600 {
601 switch ($this->rotation_angle)
602 {
Barry Mienydd671972010-10-04 16:33:58 +0200603 case 'hor' : $angle = '-flop';
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 break;
Barry Mienydd671972010-10-04 16:33:58 +0200605 case 'vrt' : $angle = '-flip';
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 break;
607 default : $angle = '-rotate '.$this->rotation_angle;
608 break;
609 }
610
611 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
612 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500613 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
615 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
616 }
617
618 $retval = 1;
619
620 @exec($cmd, $output, $retval);
621
622 // Did it work?
623 if ($retval > 0)
624 {
625 $this->set_error('imglib_image_process_failed');
626 return FALSE;
627 }
628
629 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000630 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000631
632 return TRUE;
633 }
634
635 // --------------------------------------------------------------------
636
637 /**
638 * Image Process Using NetPBM
639 *
640 * This function will resize, crop or rotate
641 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 * @param string
643 * @return bool
644 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200645 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 {
647 if ($this->library_path == '')
648 {
649 $this->set_error('imglib_libpath_invalid');
650 return FALSE;
651 }
652
Derek Jones4b9c6292011-07-01 17:40:48 -0500653 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 switch ($this->image_type)
655 {
656 case 1 :
657 $cmd_in = 'giftopnm';
658 $cmd_out = 'ppmtogif';
659 break;
660 case 2 :
661 $cmd_in = 'jpegtopnm';
662 $cmd_out = 'ppmtojpeg';
663 break;
664 case 3 :
665 $cmd_in = 'pngtopnm';
666 $cmd_out = 'ppmtopng';
667 break;
668 }
669
670 if ($action == 'crop')
671 {
672 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
673 }
674 elseif ($action == 'rotate')
675 {
676 switch ($this->rotation_angle)
677 {
678 case 90 : $angle = 'r270';
679 break;
680 case 180 : $angle = 'r180';
681 break;
Barry Mienydd671972010-10-04 16:33:58 +0200682 case 270 : $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 break;
684 case 'vrt' : $angle = 'tb';
685 break;
686 case 'hor' : $angle = 'lr';
687 break;
688 }
689
690 $cmd_inner = 'pnmflip -'.$angle.' ';
691 }
692 else // Resize
693 {
694 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
695 }
696
697 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
698
699 $retval = 1;
700
701 @exec($cmd, $output, $retval);
702
Derek Jones4b9c6292011-07-01 17:40:48 -0500703 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 if ($retval > 0)
705 {
706 $this->set_error('imglib_image_process_failed');
707 return FALSE;
708 }
709
710 // With NetPBM we have to create a temporary image.
711 // If you try manipulating the original it fails so
712 // we have to rename the temp file.
713 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
714 unlink ($this->dest_folder.'netpbm.tmp');
Derek Jones172e1612009-10-13 14:32:48 +0000715 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000716
717 return TRUE;
718 }
719
720 // --------------------------------------------------------------------
721
722 /**
723 * Image Rotate Using GD
724 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 * @return bool
726 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200727 public function image_rotate_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500729 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 if ( ! ($src_img = $this->image_create_gd()))
731 {
732 return FALSE;
733 }
734
735 // Set the background color
736 // This won't work with transparent PNG files so we are
737 // going to have to figure out how to determine the color
738 // of the alpha channel in a future release.
739
740 $white = imagecolorallocate($src_img, 255, 255, 255);
741
Derek Jones4b9c6292011-07-01 17:40:48 -0500742 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
744
Derek Jones4b9c6292011-07-01 17:40:48 -0500745 // Save the Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 if ($this->dynamic_output == TRUE)
747 {
748 $this->image_display_gd($dst_img);
749 }
750 else
751 {
752 // Or save it
753 if ( ! $this->image_save_gd($dst_img))
754 {
755 return FALSE;
756 }
757 }
758
Derek Jones4b9c6292011-07-01 17:40:48 -0500759 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 imagedestroy($dst_img);
761 imagedestroy($src_img);
762
763 // Set the file to 777
764
Derek Jones172e1612009-10-13 14:32:48 +0000765 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000766
Pascal Kriete8761ef52011-02-14 13:13:52 -0500767 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 }
769
770 // --------------------------------------------------------------------
771
772 /**
773 * Create Mirror Image using GD
774 *
775 * This function will flip horizontal or vertical
776 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 * @return bool
778 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200779 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 {
781 if ( ! $src_img = $this->image_create_gd())
782 {
783 return FALSE;
784 }
785
Derek Jones4b9c6292011-07-01 17:40:48 -0500786 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 $height = $this->orig_height;
788
789 if ($this->rotation_angle == 'hor')
790 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200791 for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 while ($left < $right)
794 {
795 $cl = imagecolorat($src_img, $left, $i);
796 $cr = imagecolorat($src_img, $right, $i);
797
798 imagesetpixel($src_img, $left, $i, $cr);
799 imagesetpixel($src_img, $right, $i, $cl);
800
801 $left++;
802 $right--;
803 }
804 }
805 }
806 else
807 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200808 for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 while ($top < $bot)
811 {
812 $ct = imagecolorat($src_img, $i, $top);
813 $cb = imagecolorat($src_img, $i, $bot);
814
815 imagesetpixel($src_img, $i, $top, $cb);
816 imagesetpixel($src_img, $i, $bot, $ct);
817
818 $top++;
819 $bot--;
820 }
821 }
822 }
823
Derek Jones4b9c6292011-07-01 17:40:48 -0500824 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 if ($this->dynamic_output == TRUE)
826 {
827 $this->image_display_gd($src_img);
828 }
829 else
830 {
831 // Or save it
832 if ( ! $this->image_save_gd($src_img))
833 {
834 return FALSE;
835 }
836 }
837
Derek Jones4b9c6292011-07-01 17:40:48 -0500838 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 imagedestroy($src_img);
840
841 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000842 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000843
844 return TRUE;
845 }
846
847 // --------------------------------------------------------------------
848
849 /**
850 * Image Watermark
851 *
852 * This is a wrapper function that chooses the type
853 * of watermarking based on the specified preference.
854 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 * @param string
856 * @return bool
857 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200858 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 {
860 if ($this->wm_type == 'overlay')
861 {
862 return $this->overlay_watermark();
863 }
864 else
865 {
866 return $this->text_watermark();
867 }
868 }
869
870 // --------------------------------------------------------------------
871
872 /**
873 * Watermark - Graphic Version
874 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 * @return bool
876 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200877 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 {
879 if ( ! function_exists('imagecolortransparent'))
880 {
881 $this->set_error('imglib_gd_required');
882 return FALSE;
883 }
884
Derek Jones4b9c6292011-07-01 17:40:48 -0500885 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 $this->get_image_properties();
887
Derek Jones4b9c6292011-07-01 17:40:48 -0500888 // Fetch watermark image properties
Barry Mienydd671972010-10-04 16:33:58 +0200889 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 $wm_img_type = $props['image_type'];
891 $wm_width = $props['width'];
892 $wm_height = $props['height'];
893
Derek Jones4b9c6292011-07-01 17:40:48 -0500894 // Create two image resources
895 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 $src_img = $this->image_create_gd($this->full_src_path);
897
898 // Reverse the offset if necessary
899 // When the image is positioned at the bottom
900 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -0500901 // further down. We want the reverse, so we'll
902 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 // offset when the image is at the right
904
905 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
906 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
907
908 if ($this->wm_vrt_alignment == 'B')
909 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
910
911 if ($this->wm_hor_alignment == 'R')
912 $this->wm_hor_offset = $this->wm_hor_offset * -1;
913
Derek Jones4b9c6292011-07-01 17:40:48 -0500914 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 $x_axis = $this->wm_hor_offset + $this->wm_padding;
916 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
917
Derek Jones4b9c6292011-07-01 17:40:48 -0500918 // Set the vertical position
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 switch ($this->wm_vrt_alignment)
920 {
921 case 'T':
922 break;
923 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
924 break;
925 case 'B': $y_axis += $this->orig_height - $wm_height;
926 break;
927 }
928
Derek Jones4b9c6292011-07-01 17:40:48 -0500929 // Set the horizontal position
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 switch ($this->wm_hor_alignment)
931 {
932 case 'L':
933 break;
934 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
935 break;
936 case 'R': $x_axis += $this->orig_width - $wm_width;
937 break;
938 }
939
Derek Jones4b9c6292011-07-01 17:40:48 -0500940 // Build the finalized image
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
942 {
943 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200944 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000945
946 // Set RGB values for text and shadow
947 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
948 $alpha = ($rgba & 0x7F000000) >> 24;
949
950 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
951 if ($alpha > 0)
952 {
953 // copy the image directly, the image's alpha transparency being the sole determinant of blending
954 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
955 }
956 else
957 {
958 // set our RGB value from above to be transparent and merge the images with the specified opacity
959 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
960 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
961 }
962
Derek Jones4b9c6292011-07-01 17:40:48 -0500963 // Output the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 if ($this->dynamic_output == TRUE)
965 {
966 $this->image_display_gd($src_img);
967 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200968 elseif ( ! $this->image_save_gd($src_img))
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200970 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 }
972
973 imagedestroy($src_img);
974 imagedestroy($wm_img);
975
976 return TRUE;
977 }
978
979 // --------------------------------------------------------------------
980
981 /**
982 * Watermark - Text Version
983 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 * @return bool
985 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200986 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 {
988 if ( ! ($src_img = $this->image_create_gd()))
989 {
990 return FALSE;
991 }
992
993 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
994 {
995 $this->set_error('imglib_missing_font');
996 return FALSE;
997 }
998
Derek Jones4b9c6292011-07-01 17:40:48 -0500999 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 $this->get_image_properties();
1001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 // Reverse the vertical offset
1003 // When the image is positioned at the bottom
1004 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -05001005 // further down. We want the reverse, so we'll
1006 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 // offset flips itself automatically
1008
1009 if ($this->wm_vrt_alignment == 'B')
1010 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1011
1012 if ($this->wm_hor_alignment == 'R')
1013 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1014
1015 // Set font width and height
1016 // These are calculated differently depending on
1017 // whether we are using the true type font or not
1018 if ($this->wm_use_truetype == TRUE)
1019 {
1020 if ($this->wm_font_size == '')
Andrey Andreeva92b9032011-12-24 19:05:58 +02001021 {
1022 $this->wm_font_size = 17;
1023 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001024
Derek Jones4b9c6292011-07-01 17:40:48 -05001025 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 $fontheight = $this->wm_font_size;
1027 $this->wm_vrt_offset += $this->wm_font_size;
1028 }
1029 else
1030 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001031 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 $fontheight = imagefontheight($this->wm_font_size);
1033 }
1034
1035 // Set base X and Y axis values
1036 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1037 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1038
1039 // Set verticle alignment
1040 if ($this->wm_use_drop_shadow == FALSE)
1041 $this->wm_shadow_distance = 0;
1042
1043 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1044 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1045
1046 switch ($this->wm_vrt_alignment)
1047 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001048 case 'T':
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001050 case 'M': $y_axis += ($this->orig_height/2)+($fontheight/2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001052 case 'B': $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 break;
1054 }
1055
1056 $x_shad = $x_axis + $this->wm_shadow_distance;
1057 $y_shad = $y_axis + $this->wm_shadow_distance;
1058
1059 // Set horizontal alignment
1060 switch ($this->wm_hor_alignment)
1061 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001062 case 'L':
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001064 case 'R':
1065 if ($this->wm_use_drop_shadow)
1066 {
1067 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1068 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1069 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001071 case 'C':
1072 if ($this->wm_use_drop_shadow)
1073 {
1074 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1075 $x_axis += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1076 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 break;
1078 }
1079
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001080 if ($this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 {
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001082 // Set RGB values for text and shadow
1083 $txt_color = str_split(substr($this->wm_font_color, 1, 6));
1084 $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
1085 $drp_color = str_split(substr($this->wm_shadow_color, 1, 6));
1086 $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[2]), hexdec($drp_color[3]));
1087
1088 // Add the text to the source image
1089 if ($this->wm_use_truetype)
1090 {
1091 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1092 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1093 }
1094 else
1095 {
1096 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1097 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1098 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 }
1100
Derek Jones4b9c6292011-07-01 17:40:48 -05001101 // Output the final image
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 if ($this->dynamic_output == TRUE)
1103 {
1104 $this->image_display_gd($src_img);
1105 }
1106 else
1107 {
1108 $this->image_save_gd($src_img);
1109 }
1110
1111 imagedestroy($src_img);
1112
1113 return TRUE;
1114 }
1115
1116 // --------------------------------------------------------------------
1117
1118 /**
1119 * Create Image - GD
1120 *
1121 * This simply creates an image resource handle
1122 * based on the type of image being processed
1123 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 * @param string
1125 * @return resource
1126 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001127 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 {
1129 if ($path == '')
1130 $path = $this->full_src_path;
1131
1132 if ($image_type == '')
1133 $image_type = $this->image_type;
1134
1135
1136 switch ($image_type)
1137 {
1138 case 1 :
1139 if ( ! function_exists('imagecreatefromgif'))
1140 {
1141 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1142 return FALSE;
1143 }
1144
1145 return imagecreatefromgif($path);
1146 break;
1147 case 2 :
1148 if ( ! function_exists('imagecreatefromjpeg'))
1149 {
1150 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1151 return FALSE;
1152 }
1153
1154 return imagecreatefromjpeg($path);
1155 break;
1156 case 3 :
1157 if ( ! function_exists('imagecreatefrompng'))
1158 {
1159 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1160 return FALSE;
1161 }
1162
1163 return imagecreatefrompng($path);
1164 break;
1165
1166 }
1167
1168 $this->set_error(array('imglib_unsupported_imagecreate'));
1169 return FALSE;
1170 }
1171
1172 // --------------------------------------------------------------------
1173
1174 /**
1175 * Write image file to disk - GD
1176 *
1177 * Takes an image resource as input and writes the file
1178 * to the specified destination
1179 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 * @param resource
1181 * @return bool
1182 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001183 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 {
1185 switch ($this->image_type)
1186 {
1187 case 1 :
1188 if ( ! function_exists('imagegif'))
1189 {
1190 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1191 return FALSE;
1192 }
1193
Derek Jones541ddbd2008-12-09 15:25:31 +00001194 if ( ! @imagegif($resource, $this->full_dst_path))
1195 {
1196 $this->set_error('imglib_save_failed');
1197 return FALSE;
1198 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 break;
1200 case 2 :
1201 if ( ! function_exists('imagejpeg'))
1202 {
1203 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1204 return FALSE;
1205 }
1206
Derek Jones541ddbd2008-12-09 15:25:31 +00001207 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1208 {
1209 $this->set_error('imglib_save_failed');
1210 return FALSE;
1211 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 break;
1213 case 3 :
1214 if ( ! function_exists('imagepng'))
1215 {
1216 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1217 return FALSE;
1218 }
1219
Derek Jones541ddbd2008-12-09 15:25:31 +00001220 if ( ! @imagepng($resource, $this->full_dst_path))
1221 {
1222 $this->set_error('imglib_save_failed');
1223 return FALSE;
1224 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 break;
1226 default :
1227 $this->set_error(array('imglib_unsupported_imagecreate'));
1228 return FALSE;
1229 break;
1230 }
1231
1232 return TRUE;
1233 }
1234
1235 // --------------------------------------------------------------------
1236
1237 /**
1238 * Dynamically outputs an image
1239 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 * @param resource
1241 * @return void
1242 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001243 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 {
1245 header("Content-Disposition: filename={$this->source_image};");
1246 header("Content-Type: {$this->mime_type}");
1247 header('Content-Transfer-Encoding: binary');
1248 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1249
1250 switch ($this->image_type)
1251 {
Barry Mienydd671972010-10-04 16:33:58 +02001252 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 break;
1254 case 2 : imagejpeg($resource, '', $this->quality);
1255 break;
1256 case 3 : imagepng($resource);
1257 break;
1258 default : echo 'Unable to display the image';
1259 break;
1260 }
1261 }
1262
1263 // --------------------------------------------------------------------
1264
1265 /**
1266 * Re-proportion Image Width/Height
1267 *
1268 * When creating thumbs, the desired width/height
1269 * can end up warping the image due to an incorrect
1270 * ratio between the full-sized image and the thumb.
1271 *
1272 * This function lets us re-proportion the width/height
1273 * if users choose to maintain the aspect ratio when resizing.
1274 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 * @return void
1276 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001277 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 {
1279 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
1280 return;
1281
1282 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
1283 return;
1284
1285 $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
1286 $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
1287
1288 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
1289
1290 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
1291 {
1292 $this->master_dim = ($ratio < 0) ? 'width' : 'height';
1293 }
1294
1295 if (($this->width != $new_width) AND ($this->height != $new_height))
1296 {
1297 if ($this->master_dim == 'height')
1298 {
1299 $this->width = $new_width;
1300 }
1301 else
1302 {
1303 $this->height = $new_height;
1304 }
1305 }
1306 }
1307
1308 // --------------------------------------------------------------------
1309
1310 /**
1311 * Get image properties
1312 *
1313 * A helper function that gets info about the file
1314 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 * @param string
1316 * @return mixed
1317 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001318 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 {
1320 // For now we require GD but we should
1321 // find a way to determine this using IM or NetPBM
1322
1323 if ($path == '')
1324 $path = $this->full_src_path;
1325
1326 if ( ! file_exists($path))
1327 {
1328 $this->set_error('imglib_invalid_path');
1329 return FALSE;
1330 }
1331
Phil Sturgeon901998a2011-08-26 10:03:33 +01001332 $vals = getimagesize($path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
Andrey Andreeva92b9032011-12-24 19:05:58 +02001334 $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001335
1336 if ($return == TRUE)
1337 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001338 return array(
1339 'width' => $vals[0],
1340 'height' => $vals[1],
1341 'image_type' => $vals[2],
1342 'size_str' => $vals[3],
1343 'mime_type' => $mime
1344 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 }
1346
Andrey Andreeva92b9032011-12-24 19:05:58 +02001347 $this->orig_width = $vals[0];
1348 $this->orig_height = $vals[1];
1349 $this->image_type = $vals[2];
1350 $this->size_str = $vals[3];
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 $this->mime_type = $mime;
1352
1353 return TRUE;
1354 }
1355
1356 // --------------------------------------------------------------------
1357
1358 /**
1359 * Size calculator
1360 *
1361 * This function takes a known width x height and
Derek Jones4b9c6292011-07-01 17:40:48 -05001362 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 * new variable needs to be known
1364 *
1365 * $props = array(
Barry Mienydd671972010-10-04 16:33:58 +02001366 * 'width' => $width,
1367 * 'height' => $height,
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 * 'new_width' => 40,
1369 * 'new_height' => ''
Derek Jones4b9c6292011-07-01 17:40:48 -05001370 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 * @param array
1373 * @return array
1374 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001375 public function size_calculator($vals)
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 {
1377 if ( ! is_array($vals))
1378 {
1379 return;
1380 }
1381
1382 $allowed = array('new_width', 'new_height', 'width', 'height');
1383
1384 foreach ($allowed as $item)
1385 {
1386 if ( ! isset($vals[$item]) OR $vals[$item] == '')
1387 $vals[$item] = 0;
1388 }
1389
1390 if ($vals['width'] == 0 OR $vals['height'] == 0)
1391 {
1392 return $vals;
1393 }
1394
1395 if ($vals['new_width'] == 0)
1396 {
1397 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1398 }
1399 elseif ($vals['new_height'] == 0)
1400 {
1401 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1402 }
1403
1404 return $vals;
1405 }
1406
1407 // --------------------------------------------------------------------
1408
1409 /**
1410 * Explode source_image
1411 *
1412 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001413 * from the source_image. This function lets us deal with
1414 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001416 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 * $array['name'] = 'my.cool';
1418 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 * @param array
1420 * @return array
1421 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001422 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001423 {
Derek Jones08cae632009-02-10 20:03:29 +00001424 $ext = strrchr($source_image, '.');
1425 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001426
Derek Jones08cae632009-02-10 20:03:29 +00001427 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001428 }
1429
1430 // --------------------------------------------------------------------
1431
1432 /**
1433 * Is GD Installed?
1434 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001435 * @return bool
1436 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001437 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 {
1439 if ( ! extension_loaded('gd'))
1440 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001441 /* As it is stated in the PHP manual, dl() is not always available
1442 * and even if so - it could generate an E_WARNING message on failure
1443 */
1444 return (function_exists('dl') AND @dl('gd.so'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001445 }
1446
1447 return TRUE;
1448 }
1449
1450 // --------------------------------------------------------------------
1451
1452 /**
1453 * Get GD version
1454 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 * @return mixed
1456 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001457 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 {
1459 if (function_exists('gd_info'))
1460 {
1461 $gd_version = @gd_info();
1462 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
1463
1464 return $gd_version;
1465 }
1466
1467 return FALSE;
1468 }
1469
1470 // --------------------------------------------------------------------
1471
1472 /**
1473 * Set error message
1474 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001475 * @param string
1476 * @return void
1477 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001478 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001479 {
1480 $CI =& get_instance();
1481 $CI->lang->load('imglib');
1482
1483 if (is_array($msg))
1484 {
1485 foreach ($msg as $val)
1486 {
1487
1488 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1489 $this->error_msg[] = $msg;
1490 log_message('error', $msg);
1491 }
1492 }
1493 else
1494 {
1495 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1496 $this->error_msg[] = $msg;
1497 log_message('error', $msg);
1498 }
1499 }
1500
1501 // --------------------------------------------------------------------
1502
1503 /**
1504 * Show error messages
1505 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001506 * @param string
1507 * @return string
1508 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001509 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 {
Andrey Andreev4eea9892011-12-19 12:05:41 +02001511 return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001512 }
1513
1514}
1515// END Image_lib Class
1516
1517/* End of file Image_lib.php */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001518/* Location: ./system/libraries/Image_lib.php */