blob: beb463b3207420689653077707bef1bfd1d12fc3 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
Derek Jones4b9c6292011-07-01 17:40:48 -050041 var $image_library = 'gd2'; // Can be: imagemagick, netpbm, gd, gd2
Derek Allard2067d1a2008-11-13 22:59:24 +000042 var $library_path = '';
43 var $dynamic_output = FALSE; // Whether to send to browser or write to disk
44 var $source_image = '';
45 var $new_image = '';
46 var $width = '';
47 var $height = '';
48 var $quality = '90';
49 var $create_thumb = FALSE;
50 var $thumb_marker = '_thumb';
Barry Mienydd671972010-10-04 16:33:58 +020051 var $maintain_ratio = TRUE; // Whether to maintain aspect ratio when resizing or use hard values
Derek Jones4b9c6292011-07-01 17:40:48 -050052 var $master_dim = 'auto'; // auto, height, or width. Determines what to use as the master dimension
Derek Allard2067d1a2008-11-13 22:59:24 +000053 var $rotation_angle = '';
54 var $x_axis = '';
55 var $y_axis = '';
56
57 // Watermark Vars
58 var $wm_text = ''; // Watermark text if graphic is not used
Derek Jones4b9c6292011-07-01 17:40:48 -050059 var $wm_type = 'text'; // Type of watermarking. Options: text/overlay
Derek Allard2067d1a2008-11-13 22:59:24 +000060 var $wm_x_transp = 4;
61 var $wm_y_transp = 4;
62 var $wm_overlay_path = ''; // Watermark image path
63 var $wm_font_path = ''; // TT font
64 var $wm_font_size = 17; // Font size (different versions of GD will either use points or pixels)
Derek Jones4b9c6292011-07-01 17:40:48 -050065 var $wm_vrt_alignment = 'B'; // Vertical alignment: T M B
Derek Allard2067d1a2008-11-13 22:59:24 +000066 var $wm_hor_alignment = 'C'; // Horizontal alignment: L R C
67 var $wm_padding = 0; // Padding around text
68 var $wm_hor_offset = 0; // Lets you push text to the right
Derek Jones4b9c6292011-07-01 17:40:48 -050069 var $wm_vrt_offset = 0; // Lets you push text down
Derek Allard2067d1a2008-11-13 22:59:24 +000070 var $wm_font_color = '#ffffff'; // Text color
71 var $wm_shadow_color = ''; // Dropshadow color
72 var $wm_shadow_distance = 2; // Dropshadow distance
Derek Jones4b9c6292011-07-01 17:40:48 -050073 var $wm_opacity = 50; // Image opacity: 1 - 100 Only works with image
Derek Allard2067d1a2008-11-13 22:59:24 +000074
75 // Private Vars
76 var $source_folder = '';
77 var $dest_folder = '';
78 var $mime_type = '';
79 var $orig_width = '';
80 var $orig_height = '';
81 var $image_type = '';
82 var $size_str = '';
83 var $full_src_path = '';
84 var $full_dst_path = '';
85 var $create_fnc = 'imagecreatetruecolor';
86 var $copy_fnc = 'imagecopyresampled';
87 var $error_msg = array();
88 var $wm_use_drop_shadow = FALSE;
89 var $wm_use_truetype = FALSE;
90
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 *
114 * @access public
115 * @return void
116 */
117 function clear()
118 {
119 $props = array('source_folder', 'dest_folder', 'source_image', 'full_src_path', 'full_dst_path', 'new_image', 'image_type', 'size_str', 'quality', 'orig_width', 'orig_height', 'rotation_angle', 'x_axis', 'y_axis', 'create_fnc', 'copy_fnc', 'wm_overlay_path', 'wm_use_truetype', 'dynamic_output', 'wm_font_size', 'wm_text', 'wm_vrt_alignment', 'wm_hor_alignment', 'wm_padding', 'wm_hor_offset', 'wm_vrt_offset', 'wm_font_color', 'wm_use_drop_shadow', 'wm_shadow_color', 'wm_shadow_distance', 'wm_opacity');
120
121 foreach ($props as $val)
122 {
123 $this->$val = '';
124 }
125
126 // special consideration for master_dim
127 $this->master_dim = 'auto';
128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * initialize image preferences
134 *
135 * @access public
136 * @param array
137 * @return bool
138 */
139 function initialize($props = array())
140 {
141 /*
142 * Convert array elements into class variables
143 */
144 if (count($props) > 0)
145 {
146 foreach ($props as $key => $val)
147 {
148 $this->$key = $val;
149 }
150 }
151
152 /*
153 * Is there a source image?
154 *
155 * If not, there's no reason to continue
156 *
157 */
158 if ($this->source_image == '')
159 {
160 $this->set_error('imglib_source_image_required');
Derek Jones4b9c6292011-07-01 17:40:48 -0500161 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
163
164 /*
165 * Is getimagesize() Available?
166 *
167 * We use it to determine the image properties (width/height).
Derek Jones4b9c6292011-07-01 17:40:48 -0500168 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 * properties using ImageMagick and NetPBM
170 *
171 */
172 if ( ! function_exists('getimagesize'))
173 {
174 $this->set_error('imglib_gd_required_for_props');
175 return FALSE;
176 }
177
178 $this->image_library = strtolower($this->image_library);
179
180 /*
181 * Set the full server path
182 *
183 * The source image may or may not contain a path.
184 * Either way, we'll try use realpath to generate the
185 * full server path in order to more reliably read it.
186 *
187 */
188 if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE)
189 {
190 $full_source_path = str_replace("\\", "/", realpath($this->source_image));
191 }
192 else
193 {
194 $full_source_path = $this->source_image;
195 }
196
197 $x = explode('/', $full_source_path);
198 $this->source_image = end($x);
199 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
200
201 // Set the Image Properties
202 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
203 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500204 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
206
207 /*
208 * Assign the "new" image name/path
209 *
210 * If the user has set a "new_image" name it means
211 * we are making a copy of the source image. If not
Derek Jones4b9c6292011-07-01 17:40:48 -0500212 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 * set the destination filename and path accordingly.
214 *
215 */
216 if ($this->new_image == '')
217 {
218 $this->dest_image = $this->source_image;
219 $this->dest_folder = $this->source_folder;
220 }
221 else
222 {
223 if (strpos($this->new_image, '/') === FALSE)
224 {
225 $this->dest_folder = $this->source_folder;
226 $this->dest_image = $this->new_image;
227 }
228 else
229 {
230 if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE)
231 {
232 $full_dest_path = str_replace("\\", "/", realpath($this->new_image));
233 }
234 else
235 {
236 $full_dest_path = $this->new_image;
237 }
238
239 // Is there a file name?
240 if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
241 {
242 $this->dest_folder = $full_dest_path.'/';
243 $this->dest_image = $this->source_image;
244 }
245 else
246 {
247 $x = explode('/', $full_dest_path);
248 $this->dest_image = end($x);
249 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
250 }
251 }
252 }
253
254 /*
255 * Compile the finalized filenames/paths
256 *
257 * We'll create two master strings containing the
258 * full server path to the source image and the
259 * full server path to the destination image.
260 * We'll also split the destination image name
261 * so we can insert the thumbnail marker if needed.
262 *
263 */
264 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
265 {
266 $this->thumb_marker = '';
267 }
268
269 $xp = $this->explode_name($this->dest_image);
270
271 $filename = $xp['name'];
272 $file_ext = $xp['ext'];
273
274 $this->full_src_path = $this->source_folder.$this->source_image;
275 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
276
277 /*
278 * Should we maintain image proportions?
279 *
280 * When creating thumbs or copies, the target width/height
281 * might not be in correct proportion with the source
Derek Jones4b9c6292011-07-01 17:40:48 -0500282 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 *
284 */
285 if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
286 {
287 $this->image_reproportion();
288 }
289
290 /*
291 * Was a width and height specified?
292 *
293 * If the destination width/height was
294 * not submitted we will use the values
295 * from the actual file
296 *
297 */
298 if ($this->width == '')
299 $this->width = $this->orig_width;
300
301 if ($this->height == '')
302 $this->height = $this->orig_height;
303
304 // Set the quality
305 $this->quality = trim(str_replace("%", "", $this->quality));
306
307 if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality))
308 $this->quality = 90;
309
310 // Set the x/y coordinates
311 $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis;
312 $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis;
313
314 // Watermark-related Stuff...
315 if ($this->wm_font_color != '')
316 {
317 if (strlen($this->wm_font_color) == 6)
318 {
319 $this->wm_font_color = '#'.$this->wm_font_color;
320 }
321 }
322
323 if ($this->wm_shadow_color != '')
324 {
325 if (strlen($this->wm_shadow_color) == 6)
326 {
327 $this->wm_shadow_color = '#'.$this->wm_shadow_color;
328 }
329 }
330
331 if ($this->wm_overlay_path != '')
332 {
333 $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path));
334 }
335
336 if ($this->wm_shadow_color != '')
337 {
338 $this->wm_use_drop_shadow = TRUE;
339 }
340
341 if ($this->wm_font_path != '')
342 {
343 $this->wm_use_truetype = TRUE;
344 }
345
346 return TRUE;
347 }
348
349 // --------------------------------------------------------------------
350
351 /**
352 * Image Resize
353 *
354 * This is a wrapper function that chooses the proper
355 * resize function based on the protocol specified
356 *
357 * @access public
358 * @return bool
359 */
360 function resize()
361 {
362 $protocol = 'image_process_'.$this->image_library;
363
Derek Jones1322ec52009-03-11 17:01:14 +0000364 if (preg_match('/gd2$/i', $protocol))
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
366 $protocol = 'image_process_gd';
367 }
368
369 return $this->$protocol('resize');
370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * Image Crop
376 *
377 * This is a wrapper function that chooses the proper
378 * cropping function based on the protocol specified
379 *
380 * @access public
381 * @return bool
382 */
383 function crop()
384 {
385 $protocol = 'image_process_'.$this->image_library;
386
Derek Jones1322ec52009-03-11 17:01:14 +0000387 if (preg_match('/gd2$/i', $protocol))
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
389 $protocol = 'image_process_gd';
390 }
391
392 return $this->$protocol('crop');
393 }
394
395 // --------------------------------------------------------------------
396
397 /**
398 * Image Rotate
399 *
400 * This is a wrapper function that chooses the proper
401 * rotation function based on the protocol specified
402 *
403 * @access public
404 * @return bool
405 */
406 function rotate()
407 {
408 // Allowed rotation values
409 $degs = array(90, 180, 270, 'vrt', 'hor');
410
Derek Allardd9c7f032008-12-01 20:18:00 +0000411 if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
413 $this->set_error('imglib_rotation_angle_required');
Derek Jones4b9c6292011-07-01 17:40:48 -0500414 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
416
417 // Reassign the width and height
418 if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
419 {
420 $this->width = $this->orig_height;
421 $this->height = $this->orig_width;
422 }
423 else
424 {
425 $this->width = $this->orig_width;
426 $this->height = $this->orig_height;
427 }
428
429
430 // Choose resizing function
431 if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm')
432 {
433 $protocol = 'image_process_'.$this->image_library;
434
435 return $this->$protocol('rotate');
436 }
437
438 if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
439 {
440 return $this->image_mirror_gd();
441 }
442 else
443 {
444 return $this->image_rotate_gd();
445 }
446 }
447
448 // --------------------------------------------------------------------
449
450 /**
451 * Image Process Using GD/GD2
452 *
453 * This function will resize or crop
454 *
455 * @access public
456 * @param string
457 * @return bool
458 */
459 function image_process_gd($action = 'resize')
460 {
461 $v2_override = FALSE;
462
463 // If the target width/height match the source, AND if the new file name is not equal to the old file name
464 // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
465 if ($this->dynamic_output === FALSE)
466 {
467 if ($this->orig_width == $this->width AND $this->orig_height == $this->height)
468 {
Barry Mienydd671972010-10-04 16:33:58 +0200469 if ($this->source_image != $this->new_image)
470 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 if (@copy($this->full_src_path, $this->full_dst_path))
472 {
Derek Jones172e1612009-10-13 14:32:48 +0000473 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
475 }
476
477 return TRUE;
478 }
479 }
480
481 // Let's set up our values based on the action
482 if ($action == 'crop')
483 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500484 // Reassign the source width/height if cropping
485 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 $this->orig_height = $this->height;
487
488 // GD 2.0 has a cropping bug so we'll test for it
489 if ($this->gd_version() !== FALSE)
490 {
491 $gd_version = str_replace('0', '', $this->gd_version());
492 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
493 }
494 }
495 else
496 {
497 // If resizing the x/y axis must be zero
498 $this->x_axis = 0;
499 $this->y_axis = 0;
500 }
501
Derek Jones4b9c6292011-07-01 17:40:48 -0500502 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 if ( ! ($src_img = $this->image_create_gd()))
504 {
505 return FALSE;
506 }
507
Derek Jones4b9c6292011-07-01 17:40:48 -0500508 // Create The Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500510 // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
511 // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
512 // below should that ever prove inaccurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500514 // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200515 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
517 $create = 'imagecreatetruecolor';
518 $copy = 'imagecopyresampled';
519 }
520 else
521 {
522 $create = 'imagecreate';
523 $copy = 'imagecopyresized';
524 }
525
526 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500527
528 if ($this->image_type == 3) // png we can actually preserve transparency
529 {
530 imagealphablending($dst_img, FALSE);
531 imagesavealpha($dst_img, TRUE);
532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
535
Derek Jones4b9c6292011-07-01 17:40:48 -0500536 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 if ($this->dynamic_output == TRUE)
538 {
539 $this->image_display_gd($dst_img);
540 }
541 else
542 {
543 // Or save it
544 if ( ! $this->image_save_gd($dst_img))
545 {
546 return FALSE;
547 }
548 }
549
Derek Jones4b9c6292011-07-01 17:40:48 -0500550 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 imagedestroy($dst_img);
552 imagedestroy($src_img);
553
554 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000555 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000556
557 return TRUE;
558 }
559
560 // --------------------------------------------------------------------
561
562 /**
563 * Image Process Using ImageMagick
564 *
565 * This function will resize, crop or rotate
566 *
567 * @access public
568 * @param string
569 * @return bool
570 */
571 function image_process_imagemagick($action = 'resize')
572 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500573 // Do we have a vaild library path?
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 if ($this->library_path == '')
575 {
576 $this->set_error('imglib_libpath_invalid');
577 return FALSE;
578 }
579
Derek Jones1322ec52009-03-11 17:01:14 +0000580 if ( ! preg_match("/convert$/i", $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
Derek Jones1322ec52009-03-11 17:01:14 +0000582 $this->library_path = rtrim($this->library_path, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000583
584 $this->library_path .= 'convert';
585 }
586
587 // Execute the command
588 $cmd = $this->library_path." -quality ".$this->quality;
589
590 if ($action == 'crop')
591 {
592 $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
593 }
594 elseif ($action == 'rotate')
595 {
596 switch ($this->rotation_angle)
597 {
Barry Mienydd671972010-10-04 16:33:58 +0200598 case 'hor' : $angle = '-flop';
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 break;
Barry Mienydd671972010-10-04 16:33:58 +0200600 case 'vrt' : $angle = '-flip';
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 break;
602 default : $angle = '-rotate '.$this->rotation_angle;
603 break;
604 }
605
606 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
607 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500608 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
610 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
611 }
612
613 $retval = 1;
614
615 @exec($cmd, $output, $retval);
616
617 // Did it work?
618 if ($retval > 0)
619 {
620 $this->set_error('imglib_image_process_failed');
621 return FALSE;
622 }
623
624 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000625 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000626
627 return TRUE;
628 }
629
630 // --------------------------------------------------------------------
631
632 /**
633 * Image Process Using NetPBM
634 *
635 * This function will resize, crop or rotate
636 *
637 * @access public
638 * @param string
639 * @return bool
640 */
641 function image_process_netpbm($action = 'resize')
642 {
643 if ($this->library_path == '')
644 {
645 $this->set_error('imglib_libpath_invalid');
646 return FALSE;
647 }
648
Derek Jones4b9c6292011-07-01 17:40:48 -0500649 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 switch ($this->image_type)
651 {
652 case 1 :
653 $cmd_in = 'giftopnm';
654 $cmd_out = 'ppmtogif';
655 break;
656 case 2 :
657 $cmd_in = 'jpegtopnm';
658 $cmd_out = 'ppmtojpeg';
659 break;
660 case 3 :
661 $cmd_in = 'pngtopnm';
662 $cmd_out = 'ppmtopng';
663 break;
664 }
665
666 if ($action == 'crop')
667 {
668 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
669 }
670 elseif ($action == 'rotate')
671 {
672 switch ($this->rotation_angle)
673 {
674 case 90 : $angle = 'r270';
675 break;
676 case 180 : $angle = 'r180';
677 break;
Barry Mienydd671972010-10-04 16:33:58 +0200678 case 270 : $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 break;
680 case 'vrt' : $angle = 'tb';
681 break;
682 case 'hor' : $angle = 'lr';
683 break;
684 }
685
686 $cmd_inner = 'pnmflip -'.$angle.' ';
687 }
688 else // Resize
689 {
690 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
691 }
692
693 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
694
695 $retval = 1;
696
697 @exec($cmd, $output, $retval);
698
Derek Jones4b9c6292011-07-01 17:40:48 -0500699 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 if ($retval > 0)
701 {
702 $this->set_error('imglib_image_process_failed');
703 return FALSE;
704 }
705
706 // With NetPBM we have to create a temporary image.
707 // If you try manipulating the original it fails so
708 // we have to rename the temp file.
709 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
710 unlink ($this->dest_folder.'netpbm.tmp');
Derek Jones172e1612009-10-13 14:32:48 +0000711 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000712
713 return TRUE;
714 }
715
716 // --------------------------------------------------------------------
717
718 /**
719 * Image Rotate Using GD
720 *
721 * @access public
722 * @return bool
723 */
724 function image_rotate_gd()
725 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500726 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 if ( ! ($src_img = $this->image_create_gd()))
728 {
729 return FALSE;
730 }
731
732 // Set the background color
733 // This won't work with transparent PNG files so we are
734 // going to have to figure out how to determine the color
735 // of the alpha channel in a future release.
736
737 $white = imagecolorallocate($src_img, 255, 255, 255);
738
Derek Jones4b9c6292011-07-01 17:40:48 -0500739 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
741
Derek Jones4b9c6292011-07-01 17:40:48 -0500742 // Save the Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 if ($this->dynamic_output == TRUE)
744 {
745 $this->image_display_gd($dst_img);
746 }
747 else
748 {
749 // Or save it
750 if ( ! $this->image_save_gd($dst_img))
751 {
752 return FALSE;
753 }
754 }
755
Derek Jones4b9c6292011-07-01 17:40:48 -0500756 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 imagedestroy($dst_img);
758 imagedestroy($src_img);
759
760 // Set the file to 777
761
Derek Jones172e1612009-10-13 14:32:48 +0000762 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000763
Pascal Kriete8761ef52011-02-14 13:13:52 -0500764 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 }
766
767 // --------------------------------------------------------------------
768
769 /**
770 * Create Mirror Image using GD
771 *
772 * This function will flip horizontal or vertical
773 *
774 * @access public
775 * @return bool
776 */
777 function image_mirror_gd()
778 {
779 if ( ! $src_img = $this->image_create_gd())
780 {
781 return FALSE;
782 }
783
Derek Jones4b9c6292011-07-01 17:40:48 -0500784 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 $height = $this->orig_height;
786
787 if ($this->rotation_angle == 'hor')
788 {
789 for ($i = 0; $i < $height; $i++)
790 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500791 $left = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 $right = $width-1;
793
794 while ($left < $right)
795 {
796 $cl = imagecolorat($src_img, $left, $i);
797 $cr = imagecolorat($src_img, $right, $i);
798
799 imagesetpixel($src_img, $left, $i, $cr);
800 imagesetpixel($src_img, $right, $i, $cl);
801
802 $left++;
803 $right--;
804 }
805 }
806 }
807 else
808 {
809 for ($i = 0; $i < $width; $i++)
810 {
811 $top = 0;
812 $bot = $height-1;
813
814 while ($top < $bot)
815 {
816 $ct = imagecolorat($src_img, $i, $top);
817 $cb = imagecolorat($src_img, $i, $bot);
818
819 imagesetpixel($src_img, $i, $top, $cb);
820 imagesetpixel($src_img, $i, $bot, $ct);
821
822 $top++;
823 $bot--;
824 }
825 }
826 }
827
Derek Jones4b9c6292011-07-01 17:40:48 -0500828 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 if ($this->dynamic_output == TRUE)
830 {
831 $this->image_display_gd($src_img);
832 }
833 else
834 {
835 // Or save it
836 if ( ! $this->image_save_gd($src_img))
837 {
838 return FALSE;
839 }
840 }
841
Derek Jones4b9c6292011-07-01 17:40:48 -0500842 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 imagedestroy($src_img);
844
845 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000846 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000847
848 return TRUE;
849 }
850
851 // --------------------------------------------------------------------
852
853 /**
854 * Image Watermark
855 *
856 * This is a wrapper function that chooses the type
857 * of watermarking based on the specified preference.
858 *
859 * @access public
860 * @param string
861 * @return bool
862 */
863 function watermark()
864 {
865 if ($this->wm_type == 'overlay')
866 {
867 return $this->overlay_watermark();
868 }
869 else
870 {
871 return $this->text_watermark();
872 }
873 }
874
875 // --------------------------------------------------------------------
876
877 /**
878 * Watermark - Graphic Version
879 *
880 * @access public
881 * @return bool
882 */
883 function overlay_watermark()
884 {
885 if ( ! function_exists('imagecolortransparent'))
886 {
887 $this->set_error('imglib_gd_required');
888 return FALSE;
889 }
890
Derek Jones4b9c6292011-07-01 17:40:48 -0500891 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 $this->get_image_properties();
893
Derek Jones4b9c6292011-07-01 17:40:48 -0500894 // Fetch watermark image properties
Barry Mienydd671972010-10-04 16:33:58 +0200895 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 $wm_img_type = $props['image_type'];
897 $wm_width = $props['width'];
898 $wm_height = $props['height'];
899
Derek Jones4b9c6292011-07-01 17:40:48 -0500900 // Create two image resources
901 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 $src_img = $this->image_create_gd($this->full_src_path);
903
904 // Reverse the offset if necessary
905 // When the image is positioned at the bottom
906 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -0500907 // further down. We want the reverse, so we'll
908 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 // offset when the image is at the right
910
911 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
912 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
913
914 if ($this->wm_vrt_alignment == 'B')
915 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
916
917 if ($this->wm_hor_alignment == 'R')
918 $this->wm_hor_offset = $this->wm_hor_offset * -1;
919
Derek Jones4b9c6292011-07-01 17:40:48 -0500920 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 $x_axis = $this->wm_hor_offset + $this->wm_padding;
922 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
923
Derek Jones4b9c6292011-07-01 17:40:48 -0500924 // Set the vertical position
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 switch ($this->wm_vrt_alignment)
926 {
927 case 'T':
928 break;
929 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
930 break;
931 case 'B': $y_axis += $this->orig_height - $wm_height;
932 break;
933 }
934
Derek Jones4b9c6292011-07-01 17:40:48 -0500935 // Set the horizontal position
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 switch ($this->wm_hor_alignment)
937 {
938 case 'L':
939 break;
940 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
941 break;
942 case 'R': $x_axis += $this->orig_width - $wm_width;
943 break;
944 }
945
Derek Jones4b9c6292011-07-01 17:40:48 -0500946 // Build the finalized image
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
948 {
949 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200950 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000951
952 // Set RGB values for text and shadow
953 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
954 $alpha = ($rgba & 0x7F000000) >> 24;
955
956 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
957 if ($alpha > 0)
958 {
959 // copy the image directly, the image's alpha transparency being the sole determinant of blending
960 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
961 }
962 else
963 {
964 // set our RGB value from above to be transparent and merge the images with the specified opacity
965 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
966 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
967 }
968
Derek Jones4b9c6292011-07-01 17:40:48 -0500969 // Output the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 if ($this->dynamic_output == TRUE)
971 {
972 $this->image_display_gd($src_img);
973 }
974 else
975 {
976 if ( ! $this->image_save_gd($src_img))
977 {
978 return FALSE;
979 }
980 }
981
982 imagedestroy($src_img);
983 imagedestroy($wm_img);
984
985 return TRUE;
986 }
987
988 // --------------------------------------------------------------------
989
990 /**
991 * Watermark - Text Version
992 *
993 * @access public
994 * @return bool
995 */
996 function text_watermark()
997 {
998 if ( ! ($src_img = $this->image_create_gd()))
999 {
1000 return FALSE;
1001 }
1002
1003 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
1004 {
1005 $this->set_error('imglib_missing_font');
1006 return FALSE;
1007 }
1008
Derek Jones4b9c6292011-07-01 17:40:48 -05001009 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 $this->get_image_properties();
1011
1012 // Set RGB values for text and shadow
1013 $this->wm_font_color = str_replace('#', '', $this->wm_font_color);
1014 $this->wm_shadow_color = str_replace('#', '', $this->wm_shadow_color);
1015
1016 $R1 = hexdec(substr($this->wm_font_color, 0, 2));
1017 $G1 = hexdec(substr($this->wm_font_color, 2, 2));
1018 $B1 = hexdec(substr($this->wm_font_color, 4, 2));
1019
1020 $R2 = hexdec(substr($this->wm_shadow_color, 0, 2));
1021 $G2 = hexdec(substr($this->wm_shadow_color, 2, 2));
1022 $B2 = hexdec(substr($this->wm_shadow_color, 4, 2));
1023
1024 $txt_color = imagecolorclosest($src_img, $R1, $G1, $B1);
1025 $drp_color = imagecolorclosest($src_img, $R2, $G2, $B2);
1026
1027 // Reverse the vertical offset
1028 // When the image is positioned at the bottom
1029 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -05001030 // further down. We want the reverse, so we'll
1031 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 // offset flips itself automatically
1033
1034 if ($this->wm_vrt_alignment == 'B')
1035 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1036
1037 if ($this->wm_hor_alignment == 'R')
1038 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1039
1040 // Set font width and height
1041 // These are calculated differently depending on
1042 // whether we are using the true type font or not
1043 if ($this->wm_use_truetype == TRUE)
1044 {
1045 if ($this->wm_font_size == '')
1046 $this->wm_font_size = '17';
1047
Derek Jones4b9c6292011-07-01 17:40:48 -05001048 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 $fontheight = $this->wm_font_size;
1050 $this->wm_vrt_offset += $this->wm_font_size;
1051 }
1052 else
1053 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001054 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 $fontheight = imagefontheight($this->wm_font_size);
1056 }
1057
1058 // Set base X and Y axis values
1059 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1060 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1061
1062 // Set verticle alignment
1063 if ($this->wm_use_drop_shadow == FALSE)
1064 $this->wm_shadow_distance = 0;
1065
1066 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1067 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1068
1069 switch ($this->wm_vrt_alignment)
1070 {
1071 case "T" :
1072 break;
1073 case "M": $y_axis += ($this->orig_height/2)+($fontheight/2);
1074 break;
1075 case "B": $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
1076 break;
1077 }
1078
1079 $x_shad = $x_axis + $this->wm_shadow_distance;
1080 $y_shad = $y_axis + $this->wm_shadow_distance;
1081
1082 // Set horizontal alignment
1083 switch ($this->wm_hor_alignment)
1084 {
1085 case "L":
1086 break;
1087 case "R":
1088 if ($this->wm_use_drop_shadow)
1089 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1090 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1091 break;
1092 case "C":
1093 if ($this->wm_use_drop_shadow)
1094 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
Derek Jones4b9c6292011-07-01 17:40:48 -05001095 $x_axis += floor(($this->orig_width -$fontwidth*strlen($this->wm_text))/2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 break;
1097 }
1098
Derek Jones4b9c6292011-07-01 17:40:48 -05001099 // Add the text to the source image
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 if ($this->wm_use_truetype)
1101 {
1102 if ($this->wm_use_drop_shadow)
1103 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1104 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1105 }
1106 else
1107 {
1108 if ($this->wm_use_drop_shadow)
1109 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1110 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1111 }
1112
Derek Jones4b9c6292011-07-01 17:40:48 -05001113 // Output the final image
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 if ($this->dynamic_output == TRUE)
1115 {
1116 $this->image_display_gd($src_img);
1117 }
1118 else
1119 {
1120 $this->image_save_gd($src_img);
1121 }
1122
1123 imagedestroy($src_img);
1124
1125 return TRUE;
1126 }
1127
1128 // --------------------------------------------------------------------
1129
1130 /**
1131 * Create Image - GD
1132 *
1133 * This simply creates an image resource handle
1134 * based on the type of image being processed
1135 *
1136 * @access public
1137 * @param string
1138 * @return resource
1139 */
1140 function image_create_gd($path = '', $image_type = '')
1141 {
1142 if ($path == '')
1143 $path = $this->full_src_path;
1144
1145 if ($image_type == '')
1146 $image_type = $this->image_type;
1147
1148
1149 switch ($image_type)
1150 {
1151 case 1 :
1152 if ( ! function_exists('imagecreatefromgif'))
1153 {
1154 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1155 return FALSE;
1156 }
1157
1158 return imagecreatefromgif($path);
1159 break;
1160 case 2 :
1161 if ( ! function_exists('imagecreatefromjpeg'))
1162 {
1163 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1164 return FALSE;
1165 }
1166
1167 return imagecreatefromjpeg($path);
1168 break;
1169 case 3 :
1170 if ( ! function_exists('imagecreatefrompng'))
1171 {
1172 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1173 return FALSE;
1174 }
1175
1176 return imagecreatefrompng($path);
1177 break;
1178
1179 }
1180
1181 $this->set_error(array('imglib_unsupported_imagecreate'));
1182 return FALSE;
1183 }
1184
1185 // --------------------------------------------------------------------
1186
1187 /**
1188 * Write image file to disk - GD
1189 *
1190 * Takes an image resource as input and writes the file
1191 * to the specified destination
1192 *
1193 * @access public
1194 * @param resource
1195 * @return bool
1196 */
1197 function image_save_gd($resource)
1198 {
1199 switch ($this->image_type)
1200 {
1201 case 1 :
1202 if ( ! function_exists('imagegif'))
1203 {
1204 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1205 return FALSE;
1206 }
1207
Derek Jones541ddbd2008-12-09 15:25:31 +00001208 if ( ! @imagegif($resource, $this->full_dst_path))
1209 {
1210 $this->set_error('imglib_save_failed');
1211 return FALSE;
1212 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 break;
1214 case 2 :
1215 if ( ! function_exists('imagejpeg'))
1216 {
1217 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1218 return FALSE;
1219 }
1220
Derek Jones541ddbd2008-12-09 15:25:31 +00001221 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1222 {
1223 $this->set_error('imglib_save_failed');
1224 return FALSE;
1225 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 break;
1227 case 3 :
1228 if ( ! function_exists('imagepng'))
1229 {
1230 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1231 return FALSE;
1232 }
1233
Derek Jones541ddbd2008-12-09 15:25:31 +00001234 if ( ! @imagepng($resource, $this->full_dst_path))
1235 {
1236 $this->set_error('imglib_save_failed');
1237 return FALSE;
1238 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 break;
1240 default :
1241 $this->set_error(array('imglib_unsupported_imagecreate'));
1242 return FALSE;
1243 break;
1244 }
1245
1246 return TRUE;
1247 }
1248
1249 // --------------------------------------------------------------------
1250
1251 /**
1252 * Dynamically outputs an image
1253 *
1254 * @access public
1255 * @param resource
1256 * @return void
1257 */
1258 function image_display_gd($resource)
1259 {
1260 header("Content-Disposition: filename={$this->source_image};");
1261 header("Content-Type: {$this->mime_type}");
1262 header('Content-Transfer-Encoding: binary');
1263 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1264
1265 switch ($this->image_type)
1266 {
Barry Mienydd671972010-10-04 16:33:58 +02001267 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 break;
1269 case 2 : imagejpeg($resource, '', $this->quality);
1270 break;
1271 case 3 : imagepng($resource);
1272 break;
1273 default : echo 'Unable to display the image';
1274 break;
1275 }
1276 }
1277
1278 // --------------------------------------------------------------------
1279
1280 /**
1281 * Re-proportion Image Width/Height
1282 *
1283 * When creating thumbs, the desired width/height
1284 * can end up warping the image due to an incorrect
1285 * ratio between the full-sized image and the thumb.
1286 *
1287 * This function lets us re-proportion the width/height
1288 * if users choose to maintain the aspect ratio when resizing.
1289 *
1290 * @access public
1291 * @return void
1292 */
1293 function image_reproportion()
1294 {
1295 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
1296 return;
1297
1298 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
1299 return;
1300
1301 $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
1302 $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
1303
1304 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
1305
1306 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
1307 {
1308 $this->master_dim = ($ratio < 0) ? 'width' : 'height';
1309 }
1310
1311 if (($this->width != $new_width) AND ($this->height != $new_height))
1312 {
1313 if ($this->master_dim == 'height')
1314 {
1315 $this->width = $new_width;
1316 }
1317 else
1318 {
1319 $this->height = $new_height;
1320 }
1321 }
1322 }
1323
1324 // --------------------------------------------------------------------
1325
1326 /**
1327 * Get image properties
1328 *
1329 * A helper function that gets info about the file
1330 *
1331 * @access public
1332 * @param string
1333 * @return mixed
1334 */
1335 function get_image_properties($path = '', $return = FALSE)
1336 {
1337 // For now we require GD but we should
1338 // find a way to determine this using IM or NetPBM
1339
1340 if ($path == '')
1341 $path = $this->full_src_path;
1342
1343 if ( ! file_exists($path))
1344 {
1345 $this->set_error('imglib_invalid_path');
1346 return FALSE;
1347 }
1348
Phil Sturgeon901998a2011-08-26 10:03:33 +01001349 $vals = getimagesize($path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001350
1351 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
1352
1353 $mime = (isset($types[$vals['2']])) ? 'image/'.$types[$vals['2']] : 'image/jpg';
1354
1355 if ($return == TRUE)
1356 {
1357 $v['width'] = $vals['0'];
1358 $v['height'] = $vals['1'];
1359 $v['image_type'] = $vals['2'];
1360 $v['size_str'] = $vals['3'];
1361 $v['mime_type'] = $mime;
1362
1363 return $v;
1364 }
1365
1366 $this->orig_width = $vals['0'];
1367 $this->orig_height = $vals['1'];
1368 $this->image_type = $vals['2'];
1369 $this->size_str = $vals['3'];
1370 $this->mime_type = $mime;
1371
1372 return TRUE;
1373 }
1374
1375 // --------------------------------------------------------------------
1376
1377 /**
1378 * Size calculator
1379 *
1380 * This function takes a known width x height and
Derek Jones4b9c6292011-07-01 17:40:48 -05001381 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 * new variable needs to be known
1383 *
1384 * $props = array(
Barry Mienydd671972010-10-04 16:33:58 +02001385 * 'width' => $width,
1386 * 'height' => $height,
Derek Allard2067d1a2008-11-13 22:59:24 +00001387 * 'new_width' => 40,
1388 * 'new_height' => ''
Derek Jones4b9c6292011-07-01 17:40:48 -05001389 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 *
1391 * @access public
1392 * @param array
1393 * @return array
1394 */
1395 function size_calculator($vals)
1396 {
1397 if ( ! is_array($vals))
1398 {
1399 return;
1400 }
1401
1402 $allowed = array('new_width', 'new_height', 'width', 'height');
1403
1404 foreach ($allowed as $item)
1405 {
1406 if ( ! isset($vals[$item]) OR $vals[$item] == '')
1407 $vals[$item] = 0;
1408 }
1409
1410 if ($vals['width'] == 0 OR $vals['height'] == 0)
1411 {
1412 return $vals;
1413 }
1414
1415 if ($vals['new_width'] == 0)
1416 {
1417 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1418 }
1419 elseif ($vals['new_height'] == 0)
1420 {
1421 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1422 }
1423
1424 return $vals;
1425 }
1426
1427 // --------------------------------------------------------------------
1428
1429 /**
1430 * Explode source_image
1431 *
1432 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001433 * from the source_image. This function lets us deal with
1434 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001435 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001436 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001437 * $array['name'] = 'my.cool';
1438 *
1439 * @access public
1440 * @param array
1441 * @return array
1442 */
1443 function explode_name($source_image)
1444 {
Derek Jones08cae632009-02-10 20:03:29 +00001445 $ext = strrchr($source_image, '.');
1446 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001447
Derek Jones08cae632009-02-10 20:03:29 +00001448 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 }
1450
1451 // --------------------------------------------------------------------
1452
1453 /**
1454 * Is GD Installed?
1455 *
1456 * @access public
1457 * @return bool
1458 */
1459 function gd_loaded()
1460 {
1461 if ( ! extension_loaded('gd'))
1462 {
1463 if ( ! dl('gd.so'))
1464 {
1465 return FALSE;
1466 }
1467 }
1468
1469 return TRUE;
1470 }
1471
1472 // --------------------------------------------------------------------
1473
1474 /**
1475 * Get GD version
1476 *
1477 * @access public
1478 * @return mixed
1479 */
1480 function gd_version()
1481 {
1482 if (function_exists('gd_info'))
1483 {
1484 $gd_version = @gd_info();
1485 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
1486
1487 return $gd_version;
1488 }
1489
1490 return FALSE;
1491 }
1492
1493 // --------------------------------------------------------------------
1494
1495 /**
1496 * Set error message
1497 *
1498 * @access public
1499 * @param string
1500 * @return void
1501 */
1502 function set_error($msg)
1503 {
1504 $CI =& get_instance();
1505 $CI->lang->load('imglib');
1506
1507 if (is_array($msg))
1508 {
1509 foreach ($msg as $val)
1510 {
1511
1512 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1513 $this->error_msg[] = $msg;
1514 log_message('error', $msg);
1515 }
1516 }
1517 else
1518 {
1519 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1520 $this->error_msg[] = $msg;
1521 log_message('error', $msg);
1522 }
1523 }
1524
1525 // --------------------------------------------------------------------
1526
1527 /**
1528 * Show error messages
1529 *
1530 * @access public
1531 * @param string
1532 * @return string
1533 */
1534 function display_errors($open = '<p>', $close = '</p>')
1535 {
1536 $str = '';
1537 foreach ($this->error_msg as $val)
1538 {
1539 $str .= $open.$val.$close;
1540 }
1541
1542 return $str;
1543 }
1544
1545}
1546// END Image_lib Class
1547
1548/* End of file Image_lib.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001549/* Location: ./system/libraries/Image_lib.php */