blob: 7beac0ba0e37b5569ef16a0e11340b11a7bd7d5d [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 {
Andrey Andreev665af0c2011-12-30 14:39:29 +0200174 $val = (strlen($matches[1]) === 6)
175 ? '#'.$matches[1]
176 : '#'.$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 +0200177 }
178 else
179 {
180 continue;
181 }
182 }
183
184 $this->$key = $val;
185 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
187 }
188
189 /*
190 * Is there a source image?
191 *
192 * If not, there's no reason to continue
193 *
194 */
195 if ($this->source_image == '')
196 {
197 $this->set_error('imglib_source_image_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500198 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200
201 /*
202 * Is getimagesize() Available?
203 *
204 * We use it to determine the image properties (width/height).
Derek Jones4b9c6292011-07-01 17:40:48 -0500205 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 * properties using ImageMagick and NetPBM
207 *
208 */
209 if ( ! function_exists('getimagesize'))
210 {
211 $this->set_error('imglib_gd_required_for_props');
212 return FALSE;
213 }
214
215 $this->image_library = strtolower($this->image_library);
216
217 /*
218 * Set the full server path
219 *
220 * The source image may or may not contain a path.
221 * Either way, we'll try use realpath to generate the
222 * full server path in order to more reliably read it.
223 *
224 */
225 if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE)
226 {
227 $full_source_path = str_replace("\\", "/", realpath($this->source_image));
228 }
229 else
230 {
231 $full_source_path = $this->source_image;
232 }
233
234 $x = explode('/', $full_source_path);
235 $this->source_image = end($x);
236 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
237
238 // Set the Image Properties
239 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
240 {
Eric Barnesb1673362011-12-05 22:05:38 -0500241 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
243
244 /*
245 * Assign the "new" image name/path
246 *
247 * If the user has set a "new_image" name it means
248 * we are making a copy of the source image. If not
Derek Jones4b9c6292011-07-01 17:40:48 -0500249 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 * set the destination filename and path accordingly.
251 *
252 */
253 if ($this->new_image == '')
254 {
255 $this->dest_image = $this->source_image;
256 $this->dest_folder = $this->source_folder;
257 }
258 else
259 {
260 if (strpos($this->new_image, '/') === FALSE)
261 {
262 $this->dest_folder = $this->source_folder;
263 $this->dest_image = $this->new_image;
264 }
265 else
266 {
267 if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE)
268 {
269 $full_dest_path = str_replace("\\", "/", realpath($this->new_image));
270 }
271 else
272 {
273 $full_dest_path = $this->new_image;
274 }
275
276 // Is there a file name?
277 if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
278 {
279 $this->dest_folder = $full_dest_path.'/';
280 $this->dest_image = $this->source_image;
281 }
282 else
283 {
284 $x = explode('/', $full_dest_path);
285 $this->dest_image = end($x);
286 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
287 }
288 }
289 }
290
291 /*
292 * Compile the finalized filenames/paths
293 *
294 * We'll create two master strings containing the
295 * full server path to the source image and the
296 * full server path to the destination image.
297 * We'll also split the destination image name
298 * so we can insert the thumbnail marker if needed.
299 *
300 */
301 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
302 {
303 $this->thumb_marker = '';
304 }
305
306 $xp = $this->explode_name($this->dest_image);
307
308 $filename = $xp['name'];
309 $file_ext = $xp['ext'];
310
311 $this->full_src_path = $this->source_folder.$this->source_image;
312 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
313
314 /*
315 * Should we maintain image proportions?
316 *
317 * When creating thumbs or copies, the target width/height
318 * might not be in correct proportion with the source
Derek Jones4b9c6292011-07-01 17:40:48 -0500319 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 *
321 */
322 if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
323 {
324 $this->image_reproportion();
325 }
326
327 /*
328 * Was a width and height specified?
329 *
330 * If the destination width/height was
331 * not submitted we will use the values
332 * from the actual file
333 *
334 */
335 if ($this->width == '')
336 $this->width = $this->orig_width;
337
338 if ($this->height == '')
339 $this->height = $this->orig_height;
340
341 // Set the quality
342 $this->quality = trim(str_replace("%", "", $this->quality));
343
344 if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality))
345 $this->quality = 90;
346
347 // Set the x/y coordinates
348 $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis;
349 $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis;
350
351 // Watermark-related Stuff...
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 if ($this->wm_overlay_path != '')
353 {
354 $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path));
355 }
356
357 if ($this->wm_shadow_color != '')
358 {
359 $this->wm_use_drop_shadow = TRUE;
360 }
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200361 elseif ($this->wm_use_drop_shadow == TRUE AND $this->wm_shadow_color == '')
362 {
363 $this->wm_use_drop_shadow = FALSE;
364 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000365
366 if ($this->wm_font_path != '')
367 {
368 $this->wm_use_truetype = TRUE;
369 }
370
371 return TRUE;
372 }
373
374 // --------------------------------------------------------------------
375
376 /**
377 * Image Resize
378 *
379 * This is a wrapper function that chooses the proper
380 * resize function based on the protocol specified
381 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 * @return bool
383 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200384 public function resize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200386 $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 return $this->$protocol('resize');
388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * Image Crop
394 *
395 * This is a wrapper function that chooses the proper
396 * cropping function based on the protocol specified
397 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 * @return bool
399 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200400 public function crop()
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200402 $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 return $this->$protocol('crop');
404 }
405
406 // --------------------------------------------------------------------
407
408 /**
409 * Image Rotate
410 *
411 * This is a wrapper function that chooses the proper
412 * rotation function based on the protocol specified
413 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @return bool
415 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200416 public function rotate()
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
418 // Allowed rotation values
419 $degs = array(90, 180, 270, 'vrt', 'hor');
420
Derek Allardd9c7f032008-12-01 20:18:00 +0000421 if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 $this->set_error('imglib_rotation_angle_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500424 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
426
427 // Reassign the width and height
428 if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
429 {
430 $this->width = $this->orig_height;
431 $this->height = $this->orig_width;
432 }
433 else
434 {
435 $this->width = $this->orig_width;
436 $this->height = $this->orig_height;
437 }
438
439
440 // Choose resizing function
441 if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm')
442 {
443 $protocol = 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 return $this->$protocol('rotate');
445 }
446
447 if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
448 {
449 return $this->image_mirror_gd();
450 }
451 else
452 {
453 return $this->image_rotate_gd();
454 }
455 }
456
457 // --------------------------------------------------------------------
458
459 /**
460 * Image Process Using GD/GD2
461 *
462 * This function will resize or crop
463 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * @param string
465 * @return bool
466 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200467 public function image_process_gd($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 $v2_override = FALSE;
470
471 // If the target width/height match the source, AND if the new file name is not equal to the old file name
472 // 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 +0200473 if ($this->dynamic_output === FALSE AND $this->orig_width == $this->width AND $this->orig_height == $this->height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200475 if ($this->source_image != $this->new_image AND @copy($this->full_src_path, $this->full_dst_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200477 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200479
480 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
482
483 // Let's set up our values based on the action
484 if ($action == 'crop')
485 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500486 // Reassign the source width/height if cropping
487 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 $this->orig_height = $this->height;
489
490 // GD 2.0 has a cropping bug so we'll test for it
491 if ($this->gd_version() !== FALSE)
492 {
493 $gd_version = str_replace('0', '', $this->gd_version());
494 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
495 }
496 }
497 else
498 {
499 // If resizing the x/y axis must be zero
500 $this->x_axis = 0;
501 $this->y_axis = 0;
502 }
503
Derek Jones4b9c6292011-07-01 17:40:48 -0500504 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 if ( ! ($src_img = $this->image_create_gd()))
506 {
507 return FALSE;
508 }
509
Derek Jones4b9c6292011-07-01 17:40:48 -0500510 // Create The Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500512 // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
513 // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
514 // below should that ever prove inaccurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500516 // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200517 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
519 $create = 'imagecreatetruecolor';
520 $copy = 'imagecopyresampled';
521 }
522 else
523 {
524 $create = 'imagecreate';
525 $copy = 'imagecopyresized';
526 }
527
528 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500529
530 if ($this->image_type == 3) // png we can actually preserve transparency
531 {
532 imagealphablending($dst_img, FALSE);
533 imagesavealpha($dst_img, TRUE);
534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
537
Derek Jones4b9c6292011-07-01 17:40:48 -0500538 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 if ($this->dynamic_output == TRUE)
540 {
541 $this->image_display_gd($dst_img);
542 }
543 else
544 {
545 // Or save it
546 if ( ! $this->image_save_gd($dst_img))
547 {
548 return FALSE;
549 }
550 }
551
Derek Jones4b9c6292011-07-01 17:40:48 -0500552 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 imagedestroy($dst_img);
554 imagedestroy($src_img);
555
556 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000557 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000558
559 return TRUE;
560 }
561
562 // --------------------------------------------------------------------
563
564 /**
565 * Image Process Using ImageMagick
566 *
567 * This function will resize, crop or rotate
568 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 * @param string
570 * @return bool
571 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200572 public function image_process_imagemagick($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500574 // Do we have a vaild library path?
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 if ($this->library_path == '')
576 {
577 $this->set_error('imglib_libpath_invalid');
578 return FALSE;
579 }
580
Derek Jones1322ec52009-03-11 17:01:14 +0000581 if ( ! preg_match("/convert$/i", $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200583 $this->library_path = rtrim($this->library_path, '/').'/convert';
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 }
585
586 // Execute the command
587 $cmd = $this->library_path." -quality ".$this->quality;
588
589 if ($action == 'crop')
590 {
591 $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
592 }
593 elseif ($action == 'rotate')
594 {
595 switch ($this->rotation_angle)
596 {
Barry Mienydd671972010-10-04 16:33:58 +0200597 case 'hor' : $angle = '-flop';
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 break;
Barry Mienydd671972010-10-04 16:33:58 +0200599 case 'vrt' : $angle = '-flip';
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 break;
601 default : $angle = '-rotate '.$this->rotation_angle;
602 break;
603 }
604
605 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
606 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500607 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
609 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
610 }
611
612 $retval = 1;
613
614 @exec($cmd, $output, $retval);
615
616 // Did it work?
617 if ($retval > 0)
618 {
619 $this->set_error('imglib_image_process_failed');
620 return FALSE;
621 }
622
623 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000624 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000625
626 return TRUE;
627 }
628
629 // --------------------------------------------------------------------
630
631 /**
632 * Image Process Using NetPBM
633 *
634 * This function will resize, crop or rotate
635 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 * @param string
637 * @return bool
638 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200639 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
641 if ($this->library_path == '')
642 {
643 $this->set_error('imglib_libpath_invalid');
644 return FALSE;
645 }
646
Derek Jones4b9c6292011-07-01 17:40:48 -0500647 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 switch ($this->image_type)
649 {
650 case 1 :
651 $cmd_in = 'giftopnm';
652 $cmd_out = 'ppmtogif';
653 break;
654 case 2 :
655 $cmd_in = 'jpegtopnm';
656 $cmd_out = 'ppmtojpeg';
657 break;
658 case 3 :
659 $cmd_in = 'pngtopnm';
660 $cmd_out = 'ppmtopng';
661 break;
662 }
663
664 if ($action == 'crop')
665 {
666 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
667 }
668 elseif ($action == 'rotate')
669 {
670 switch ($this->rotation_angle)
671 {
672 case 90 : $angle = 'r270';
673 break;
674 case 180 : $angle = 'r180';
675 break;
Barry Mienydd671972010-10-04 16:33:58 +0200676 case 270 : $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 break;
678 case 'vrt' : $angle = 'tb';
679 break;
680 case 'hor' : $angle = 'lr';
681 break;
682 }
683
684 $cmd_inner = 'pnmflip -'.$angle.' ';
685 }
686 else // Resize
687 {
688 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
689 }
690
691 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
692
693 $retval = 1;
694
695 @exec($cmd, $output, $retval);
696
Derek Jones4b9c6292011-07-01 17:40:48 -0500697 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 if ($retval > 0)
699 {
700 $this->set_error('imglib_image_process_failed');
701 return FALSE;
702 }
703
704 // With NetPBM we have to create a temporary image.
705 // If you try manipulating the original it fails so
706 // we have to rename the temp file.
707 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
708 unlink ($this->dest_folder.'netpbm.tmp');
Derek Jones172e1612009-10-13 14:32:48 +0000709 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000710
711 return TRUE;
712 }
713
714 // --------------------------------------------------------------------
715
716 /**
717 * Image Rotate Using GD
718 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 * @return bool
720 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200721 public function image_rotate_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500723 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 if ( ! ($src_img = $this->image_create_gd()))
725 {
726 return FALSE;
727 }
728
729 // Set the background color
730 // This won't work with transparent PNG files so we are
731 // going to have to figure out how to determine the color
732 // of the alpha channel in a future release.
733
734 $white = imagecolorallocate($src_img, 255, 255, 255);
735
Derek Jones4b9c6292011-07-01 17:40:48 -0500736 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
738
Derek Jones4b9c6292011-07-01 17:40:48 -0500739 // Save the Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 if ($this->dynamic_output == TRUE)
741 {
742 $this->image_display_gd($dst_img);
743 }
744 else
745 {
746 // Or save it
747 if ( ! $this->image_save_gd($dst_img))
748 {
749 return FALSE;
750 }
751 }
752
Derek Jones4b9c6292011-07-01 17:40:48 -0500753 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 imagedestroy($dst_img);
755 imagedestroy($src_img);
756
757 // Set the file to 777
758
Derek Jones172e1612009-10-13 14:32:48 +0000759 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000760
Pascal Kriete8761ef52011-02-14 13:13:52 -0500761 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 }
763
764 // --------------------------------------------------------------------
765
766 /**
767 * Create Mirror Image using GD
768 *
769 * This function will flip horizontal or vertical
770 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 * @return bool
772 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200773 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 {
775 if ( ! $src_img = $this->image_create_gd())
776 {
777 return FALSE;
778 }
779
Derek Jones4b9c6292011-07-01 17:40:48 -0500780 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 $height = $this->orig_height;
782
783 if ($this->rotation_angle == 'hor')
784 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200785 for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 while ($left < $right)
788 {
789 $cl = imagecolorat($src_img, $left, $i);
790 $cr = imagecolorat($src_img, $right, $i);
791
792 imagesetpixel($src_img, $left, $i, $cr);
793 imagesetpixel($src_img, $right, $i, $cl);
794
795 $left++;
796 $right--;
797 }
798 }
799 }
800 else
801 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200802 for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 while ($top < $bot)
805 {
806 $ct = imagecolorat($src_img, $i, $top);
807 $cb = imagecolorat($src_img, $i, $bot);
808
809 imagesetpixel($src_img, $i, $top, $cb);
810 imagesetpixel($src_img, $i, $bot, $ct);
811
812 $top++;
813 $bot--;
814 }
815 }
816 }
817
Derek Jones4b9c6292011-07-01 17:40:48 -0500818 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 if ($this->dynamic_output == TRUE)
820 {
821 $this->image_display_gd($src_img);
822 }
823 else
824 {
825 // Or save it
826 if ( ! $this->image_save_gd($src_img))
827 {
828 return FALSE;
829 }
830 }
831
Derek Jones4b9c6292011-07-01 17:40:48 -0500832 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 imagedestroy($src_img);
834
835 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000836 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000837
838 return TRUE;
839 }
840
841 // --------------------------------------------------------------------
842
843 /**
844 * Image Watermark
845 *
846 * This is a wrapper function that chooses the type
847 * of watermarking based on the specified preference.
848 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 * @param string
850 * @return bool
851 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200852 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
854 if ($this->wm_type == 'overlay')
855 {
856 return $this->overlay_watermark();
857 }
858 else
859 {
860 return $this->text_watermark();
861 }
862 }
863
864 // --------------------------------------------------------------------
865
866 /**
867 * Watermark - Graphic Version
868 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 * @return bool
870 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200871 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 {
873 if ( ! function_exists('imagecolortransparent'))
874 {
875 $this->set_error('imglib_gd_required');
876 return FALSE;
877 }
878
Derek Jones4b9c6292011-07-01 17:40:48 -0500879 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 $this->get_image_properties();
881
Derek Jones4b9c6292011-07-01 17:40:48 -0500882 // Fetch watermark image properties
Barry Mienydd671972010-10-04 16:33:58 +0200883 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 $wm_img_type = $props['image_type'];
885 $wm_width = $props['width'];
886 $wm_height = $props['height'];
887
Derek Jones4b9c6292011-07-01 17:40:48 -0500888 // Create two image resources
889 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 $src_img = $this->image_create_gd($this->full_src_path);
891
892 // Reverse the offset if necessary
893 // When the image is positioned at the bottom
894 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -0500895 // further down. We want the reverse, so we'll
896 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 // offset when the image is at the right
898
899 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
900 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
901
902 if ($this->wm_vrt_alignment == 'B')
903 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
904
905 if ($this->wm_hor_alignment == 'R')
906 $this->wm_hor_offset = $this->wm_hor_offset * -1;
907
Derek Jones4b9c6292011-07-01 17:40:48 -0500908 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 $x_axis = $this->wm_hor_offset + $this->wm_padding;
910 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
911
Derek Jones4b9c6292011-07-01 17:40:48 -0500912 // Set the vertical position
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 switch ($this->wm_vrt_alignment)
914 {
915 case 'T':
916 break;
917 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
918 break;
919 case 'B': $y_axis += $this->orig_height - $wm_height;
920 break;
921 }
922
Derek Jones4b9c6292011-07-01 17:40:48 -0500923 // Set the horizontal position
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 switch ($this->wm_hor_alignment)
925 {
926 case 'L':
927 break;
928 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
929 break;
930 case 'R': $x_axis += $this->orig_width - $wm_width;
931 break;
932 }
933
Derek Jones4b9c6292011-07-01 17:40:48 -0500934 // Build the finalized image
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
936 {
937 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200938 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000939
940 // Set RGB values for text and shadow
941 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
942 $alpha = ($rgba & 0x7F000000) >> 24;
943
944 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
945 if ($alpha > 0)
946 {
947 // copy the image directly, the image's alpha transparency being the sole determinant of blending
948 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
949 }
950 else
951 {
952 // set our RGB value from above to be transparent and merge the images with the specified opacity
953 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
954 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
955 }
956
Derek Jones4b9c6292011-07-01 17:40:48 -0500957 // Output the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 if ($this->dynamic_output == TRUE)
959 {
960 $this->image_display_gd($src_img);
961 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200962 elseif ( ! $this->image_save_gd($src_img))
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200964 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
966
967 imagedestroy($src_img);
968 imagedestroy($wm_img);
969
970 return TRUE;
971 }
972
973 // --------------------------------------------------------------------
974
975 /**
976 * Watermark - Text Version
977 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 * @return bool
979 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200980 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
982 if ( ! ($src_img = $this->image_create_gd()))
983 {
984 return FALSE;
985 }
986
987 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
988 {
989 $this->set_error('imglib_missing_font');
990 return FALSE;
991 }
992
Derek Jones4b9c6292011-07-01 17:40:48 -0500993 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 $this->get_image_properties();
995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 // Reverse the vertical offset
997 // When the image is positioned at the bottom
998 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -0500999 // further down. We want the reverse, so we'll
1000 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 // offset flips itself automatically
1002
1003 if ($this->wm_vrt_alignment == 'B')
1004 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1005
1006 if ($this->wm_hor_alignment == 'R')
1007 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1008
1009 // Set font width and height
1010 // These are calculated differently depending on
1011 // whether we are using the true type font or not
1012 if ($this->wm_use_truetype == TRUE)
1013 {
1014 if ($this->wm_font_size == '')
Andrey Andreeva92b9032011-12-24 19:05:58 +02001015 {
1016 $this->wm_font_size = 17;
1017 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001018
Derek Jones4b9c6292011-07-01 17:40:48 -05001019 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 $fontheight = $this->wm_font_size;
1021 $this->wm_vrt_offset += $this->wm_font_size;
1022 }
1023 else
1024 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001025 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 $fontheight = imagefontheight($this->wm_font_size);
1027 }
1028
1029 // Set base X and Y axis values
1030 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1031 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1032
1033 // Set verticle alignment
1034 if ($this->wm_use_drop_shadow == FALSE)
1035 $this->wm_shadow_distance = 0;
1036
1037 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1038 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1039
1040 switch ($this->wm_vrt_alignment)
1041 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001042 case 'T':
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001044 case 'M': $y_axis += ($this->orig_height/2)+($fontheight/2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001046 case 'B': $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 break;
1048 }
1049
1050 $x_shad = $x_axis + $this->wm_shadow_distance;
1051 $y_shad = $y_axis + $this->wm_shadow_distance;
1052
1053 // Set horizontal alignment
1054 switch ($this->wm_hor_alignment)
1055 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001056 case 'L':
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001058 case 'R':
1059 if ($this->wm_use_drop_shadow)
1060 {
1061 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1062 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1063 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001065 case 'C':
1066 if ($this->wm_use_drop_shadow)
1067 {
1068 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1069 $x_axis += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1070 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 break;
1072 }
1073
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001074 if ($this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 {
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001076 // Set RGB values for text and shadow
1077 $txt_color = str_split(substr($this->wm_font_color, 1, 6));
1078 $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
1079 $drp_color = str_split(substr($this->wm_shadow_color, 1, 6));
1080 $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[2]), hexdec($drp_color[3]));
1081
1082 // Add the text to the source image
1083 if ($this->wm_use_truetype)
1084 {
1085 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1086 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1087 }
1088 else
1089 {
1090 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1091 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1092 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 }
1094
Derek Jones4b9c6292011-07-01 17:40:48 -05001095 // Output the final image
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 if ($this->dynamic_output == TRUE)
1097 {
1098 $this->image_display_gd($src_img);
1099 }
1100 else
1101 {
1102 $this->image_save_gd($src_img);
1103 }
1104
1105 imagedestroy($src_img);
1106
1107 return TRUE;
1108 }
1109
1110 // --------------------------------------------------------------------
1111
1112 /**
1113 * Create Image - GD
1114 *
1115 * This simply creates an image resource handle
1116 * based on the type of image being processed
1117 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 * @param string
1119 * @return resource
1120 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001121 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 {
1123 if ($path == '')
1124 $path = $this->full_src_path;
1125
1126 if ($image_type == '')
1127 $image_type = $this->image_type;
1128
1129
1130 switch ($image_type)
1131 {
1132 case 1 :
1133 if ( ! function_exists('imagecreatefromgif'))
1134 {
1135 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1136 return FALSE;
1137 }
1138
1139 return imagecreatefromgif($path);
1140 break;
1141 case 2 :
1142 if ( ! function_exists('imagecreatefromjpeg'))
1143 {
1144 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1145 return FALSE;
1146 }
1147
1148 return imagecreatefromjpeg($path);
1149 break;
1150 case 3 :
1151 if ( ! function_exists('imagecreatefrompng'))
1152 {
1153 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1154 return FALSE;
1155 }
1156
1157 return imagecreatefrompng($path);
1158 break;
1159
1160 }
1161
1162 $this->set_error(array('imglib_unsupported_imagecreate'));
1163 return FALSE;
1164 }
1165
1166 // --------------------------------------------------------------------
1167
1168 /**
1169 * Write image file to disk - GD
1170 *
1171 * Takes an image resource as input and writes the file
1172 * to the specified destination
1173 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 * @param resource
1175 * @return bool
1176 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001177 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 {
1179 switch ($this->image_type)
1180 {
1181 case 1 :
1182 if ( ! function_exists('imagegif'))
1183 {
1184 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1185 return FALSE;
1186 }
1187
Derek Jones541ddbd2008-12-09 15:25:31 +00001188 if ( ! @imagegif($resource, $this->full_dst_path))
1189 {
1190 $this->set_error('imglib_save_failed');
1191 return FALSE;
1192 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 break;
1194 case 2 :
1195 if ( ! function_exists('imagejpeg'))
1196 {
1197 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1198 return FALSE;
1199 }
1200
Derek Jones541ddbd2008-12-09 15:25:31 +00001201 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1202 {
1203 $this->set_error('imglib_save_failed');
1204 return FALSE;
1205 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 break;
1207 case 3 :
1208 if ( ! function_exists('imagepng'))
1209 {
1210 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1211 return FALSE;
1212 }
1213
Derek Jones541ddbd2008-12-09 15:25:31 +00001214 if ( ! @imagepng($resource, $this->full_dst_path))
1215 {
1216 $this->set_error('imglib_save_failed');
1217 return FALSE;
1218 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 break;
1220 default :
1221 $this->set_error(array('imglib_unsupported_imagecreate'));
1222 return FALSE;
1223 break;
1224 }
1225
1226 return TRUE;
1227 }
1228
1229 // --------------------------------------------------------------------
1230
1231 /**
1232 * Dynamically outputs an image
1233 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 * @param resource
1235 * @return void
1236 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001237 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 {
1239 header("Content-Disposition: filename={$this->source_image};");
1240 header("Content-Type: {$this->mime_type}");
1241 header('Content-Transfer-Encoding: binary');
1242 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1243
1244 switch ($this->image_type)
1245 {
Barry Mienydd671972010-10-04 16:33:58 +02001246 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 break;
1248 case 2 : imagejpeg($resource, '', $this->quality);
1249 break;
1250 case 3 : imagepng($resource);
1251 break;
1252 default : echo 'Unable to display the image';
1253 break;
1254 }
1255 }
1256
1257 // --------------------------------------------------------------------
1258
1259 /**
1260 * Re-proportion Image Width/Height
1261 *
1262 * When creating thumbs, the desired width/height
1263 * can end up warping the image due to an incorrect
1264 * ratio between the full-sized image and the thumb.
1265 *
1266 * This function lets us re-proportion the width/height
1267 * if users choose to maintain the aspect ratio when resizing.
1268 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 * @return void
1270 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001271 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 {
1273 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
1274 return;
1275
1276 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
1277 return;
1278
1279 $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
1280 $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
1281
1282 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
1283
1284 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
1285 {
1286 $this->master_dim = ($ratio < 0) ? 'width' : 'height';
1287 }
1288
1289 if (($this->width != $new_width) AND ($this->height != $new_height))
1290 {
1291 if ($this->master_dim == 'height')
1292 {
1293 $this->width = $new_width;
1294 }
1295 else
1296 {
1297 $this->height = $new_height;
1298 }
1299 }
1300 }
1301
1302 // --------------------------------------------------------------------
1303
1304 /**
1305 * Get image properties
1306 *
1307 * A helper function that gets info about the file
1308 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 * @param string
1310 * @return mixed
1311 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001312 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 {
1314 // For now we require GD but we should
1315 // find a way to determine this using IM or NetPBM
1316
1317 if ($path == '')
1318 $path = $this->full_src_path;
1319
1320 if ( ! file_exists($path))
1321 {
1322 $this->set_error('imglib_invalid_path');
1323 return FALSE;
1324 }
1325
Phil Sturgeon901998a2011-08-26 10:03:33 +01001326 $vals = getimagesize($path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
Andrey Andreeva92b9032011-12-24 19:05:58 +02001328 $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001329
1330 if ($return == TRUE)
1331 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001332 return array(
1333 'width' => $vals[0],
1334 'height' => $vals[1],
1335 'image_type' => $vals[2],
1336 'size_str' => $vals[3],
1337 'mime_type' => $mime
1338 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 }
1340
Andrey Andreeva92b9032011-12-24 19:05:58 +02001341 $this->orig_width = $vals[0];
1342 $this->orig_height = $vals[1];
1343 $this->image_type = $vals[2];
1344 $this->size_str = $vals[3];
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 $this->mime_type = $mime;
1346
1347 return TRUE;
1348 }
1349
1350 // --------------------------------------------------------------------
1351
1352 /**
1353 * Size calculator
1354 *
1355 * This function takes a known width x height and
Derek Jones4b9c6292011-07-01 17:40:48 -05001356 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 * new variable needs to be known
1358 *
1359 * $props = array(
Barry Mienydd671972010-10-04 16:33:58 +02001360 * 'width' => $width,
1361 * 'height' => $height,
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 * 'new_width' => 40,
1363 * 'new_height' => ''
Derek Jones4b9c6292011-07-01 17:40:48 -05001364 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 * @param array
1367 * @return array
1368 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001369 public function size_calculator($vals)
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 {
1371 if ( ! is_array($vals))
1372 {
1373 return;
1374 }
1375
1376 $allowed = array('new_width', 'new_height', 'width', 'height');
1377
1378 foreach ($allowed as $item)
1379 {
1380 if ( ! isset($vals[$item]) OR $vals[$item] == '')
1381 $vals[$item] = 0;
1382 }
1383
1384 if ($vals['width'] == 0 OR $vals['height'] == 0)
1385 {
1386 return $vals;
1387 }
1388
1389 if ($vals['new_width'] == 0)
1390 {
1391 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1392 }
1393 elseif ($vals['new_height'] == 0)
1394 {
1395 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1396 }
1397
1398 return $vals;
1399 }
1400
1401 // --------------------------------------------------------------------
1402
1403 /**
1404 * Explode source_image
1405 *
1406 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001407 * from the source_image. This function lets us deal with
1408 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001410 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 * $array['name'] = 'my.cool';
1412 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 * @param array
1414 * @return array
1415 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001416 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 {
Derek Jones08cae632009-02-10 20:03:29 +00001418 $ext = strrchr($source_image, '.');
1419 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001420
Derek Jones08cae632009-02-10 20:03:29 +00001421 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001422 }
1423
1424 // --------------------------------------------------------------------
1425
1426 /**
1427 * Is GD Installed?
1428 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001429 * @return bool
1430 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001431 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001432 {
1433 if ( ! extension_loaded('gd'))
1434 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001435 /* As it is stated in the PHP manual, dl() is not always available
1436 * and even if so - it could generate an E_WARNING message on failure
1437 */
1438 return (function_exists('dl') AND @dl('gd.so'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001439 }
1440
1441 return TRUE;
1442 }
1443
1444 // --------------------------------------------------------------------
1445
1446 /**
1447 * Get GD version
1448 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 * @return mixed
1450 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001451 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 {
1453 if (function_exists('gd_info'))
1454 {
1455 $gd_version = @gd_info();
1456 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
1457
1458 return $gd_version;
1459 }
1460
1461 return FALSE;
1462 }
1463
1464 // --------------------------------------------------------------------
1465
1466 /**
1467 * Set error message
1468 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 * @param string
1470 * @return void
1471 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001472 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 {
1474 $CI =& get_instance();
1475 $CI->lang->load('imglib');
1476
1477 if (is_array($msg))
1478 {
1479 foreach ($msg as $val)
1480 {
1481
1482 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1483 $this->error_msg[] = $msg;
1484 log_message('error', $msg);
1485 }
1486 }
1487 else
1488 {
1489 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1490 $this->error_msg[] = $msg;
1491 log_message('error', $msg);
1492 }
1493 }
1494
1495 // --------------------------------------------------------------------
1496
1497 /**
1498 * Show error messages
1499 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001500 * @param string
1501 * @return string
1502 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001503 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001504 {
Andrey Andreev4eea9892011-12-19 12:05:41 +02001505 return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001506 }
1507
1508}
1509// END Image_lib Class
1510
1511/* End of file Image_lib.php */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001512/* Location: ./system/libraries/Image_lib.php */