blob: 6855e44986ae5e2e130ff03157e7cb17d3bbbd7c [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 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Image Manipulation class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Image_lib
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/libraries/image_lib.html
26 */
27class CI_Image_lib {
28
Derek Jones4b9c6292011-07-01 17:40:48 -050029 var $image_library = 'gd2'; // Can be: imagemagick, netpbm, gd, gd2
Derek Allard2067d1a2008-11-13 22:59:24 +000030 var $library_path = '';
31 var $dynamic_output = FALSE; // Whether to send to browser or write to disk
32 var $source_image = '';
33 var $new_image = '';
34 var $width = '';
35 var $height = '';
36 var $quality = '90';
37 var $create_thumb = FALSE;
38 var $thumb_marker = '_thumb';
Barry Mienydd671972010-10-04 16:33:58 +020039 var $maintain_ratio = TRUE; // Whether to maintain aspect ratio when resizing or use hard values
Derek Jones4b9c6292011-07-01 17:40:48 -050040 var $master_dim = 'auto'; // auto, height, or width. Determines what to use as the master dimension
Derek Allard2067d1a2008-11-13 22:59:24 +000041 var $rotation_angle = '';
42 var $x_axis = '';
43 var $y_axis = '';
44
45 // Watermark Vars
46 var $wm_text = ''; // Watermark text if graphic is not used
Derek Jones4b9c6292011-07-01 17:40:48 -050047 var $wm_type = 'text'; // Type of watermarking. Options: text/overlay
Derek Allard2067d1a2008-11-13 22:59:24 +000048 var $wm_x_transp = 4;
49 var $wm_y_transp = 4;
50 var $wm_overlay_path = ''; // Watermark image path
51 var $wm_font_path = ''; // TT font
52 var $wm_font_size = 17; // Font size (different versions of GD will either use points or pixels)
Derek Jones4b9c6292011-07-01 17:40:48 -050053 var $wm_vrt_alignment = 'B'; // Vertical alignment: T M B
Derek Allard2067d1a2008-11-13 22:59:24 +000054 var $wm_hor_alignment = 'C'; // Horizontal alignment: L R C
55 var $wm_padding = 0; // Padding around text
56 var $wm_hor_offset = 0; // Lets you push text to the right
Derek Jones4b9c6292011-07-01 17:40:48 -050057 var $wm_vrt_offset = 0; // Lets you push text down
Derek Allard2067d1a2008-11-13 22:59:24 +000058 var $wm_font_color = '#ffffff'; // Text color
59 var $wm_shadow_color = ''; // Dropshadow color
60 var $wm_shadow_distance = 2; // Dropshadow distance
Derek Jones4b9c6292011-07-01 17:40:48 -050061 var $wm_opacity = 50; // Image opacity: 1 - 100 Only works with image
Derek Allard2067d1a2008-11-13 22:59:24 +000062
63 // Private Vars
64 var $source_folder = '';
65 var $dest_folder = '';
66 var $mime_type = '';
67 var $orig_width = '';
68 var $orig_height = '';
69 var $image_type = '';
70 var $size_str = '';
71 var $full_src_path = '';
72 var $full_dst_path = '';
73 var $create_fnc = 'imagecreatetruecolor';
74 var $copy_fnc = 'imagecopyresampled';
75 var $error_msg = array();
76 var $wm_use_drop_shadow = FALSE;
77 var $wm_use_truetype = FALSE;
78
79 /**
80 * Constructor
81 *
Derek Allard2067d1a2008-11-13 22:59:24 +000082 * @param string
83 * @return void
84 */
Greg Akera9263282010-11-10 15:26:43 -060085 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
87 if (count($props) > 0)
88 {
89 $this->initialize($props);
90 }
91
92 log_message('debug', "Image Lib Class Initialized");
93 }
94
95 // --------------------------------------------------------------------
96
97 /**
98 * Initialize image properties
99 *
100 * Resets values in case this class is used in a loop
101 *
102 * @access public
103 * @return void
104 */
105 function clear()
106 {
Michael Denniscb07a322011-08-20 23:40:59 -0700107 $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 +0000108
109 foreach ($props as $val)
110 {
111 $this->$val = '';
112 }
113
Michael Denniscb07a322011-08-20 23:40:59 -0700114 $this->image_library = 'gd2';
115 $this->dynamic_output = FALSE;
116 $this->quality = '90';
117 $this->create_thumb = FALSE;
118 $this->thumb_marker = '_thumb';
119 $this->maintain_ratio = TRUE;
120 $this->master_dim = 'auto';
121 $this->wm_type = 'text';
122 $this->wm_x_transp = 4;
123 $this->wm_y_transp = 4;
124 $this->wm_font_size = 17;
125 $this->wm_vrt_alignment = 'B';
126 $this->wm_hor_alignment = 'C';
127 $this->wm_padding = 0;
128 $this->wm_hor_offset = 0;
129 $this->wm_vrt_offset = 0;
130 $this->wm_font_color = '#ffffff';
131 $this->wm_shadow_distance = 2;
132 $this->wm_opacity = 50;
133 $this->create_fnc = 'imagecreatetruecolor';
134 $this->copy_fnc = 'imagecopyresampled';
135 $this->error_msg = array();
136 $this->wm_use_drop_shadow = FALSE;
137 $this->wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
139
140 // --------------------------------------------------------------------
141
142 /**
143 * initialize image preferences
144 *
145 * @access public
146 * @param array
147 * @return bool
148 */
149 function initialize($props = array())
150 {
151 /*
152 * Convert array elements into class variables
153 */
154 if (count($props) > 0)
155 {
156 foreach ($props as $key => $val)
157 {
158 $this->$key = $val;
159 }
160 }
161
162 /*
163 * Is there a source image?
164 *
165 * If not, there's no reason to continue
166 *
167 */
168 if ($this->source_image == '')
169 {
170 $this->set_error('imglib_source_image_required');
Derek Jones4b9c6292011-07-01 17:40:48 -0500171 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 }
173
174 /*
175 * Is getimagesize() Available?
176 *
177 * We use it to determine the image properties (width/height).
Derek Jones4b9c6292011-07-01 17:40:48 -0500178 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 * properties using ImageMagick and NetPBM
180 *
181 */
182 if ( ! function_exists('getimagesize'))
183 {
184 $this->set_error('imglib_gd_required_for_props');
185 return FALSE;
186 }
187
188 $this->image_library = strtolower($this->image_library);
189
190 /*
191 * Set the full server path
192 *
193 * The source image may or may not contain a path.
194 * Either way, we'll try use realpath to generate the
195 * full server path in order to more reliably read it.
196 *
197 */
198 if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE)
199 {
200 $full_source_path = str_replace("\\", "/", realpath($this->source_image));
201 }
202 else
203 {
204 $full_source_path = $this->source_image;
205 }
206
207 $x = explode('/', $full_source_path);
208 $this->source_image = end($x);
209 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
210
211 // Set the Image Properties
212 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
213 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500214 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
216
217 /*
218 * Assign the "new" image name/path
219 *
220 * If the user has set a "new_image" name it means
221 * we are making a copy of the source image. If not
Derek Jones4b9c6292011-07-01 17:40:48 -0500222 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 * set the destination filename and path accordingly.
224 *
225 */
226 if ($this->new_image == '')
227 {
228 $this->dest_image = $this->source_image;
229 $this->dest_folder = $this->source_folder;
230 }
231 else
232 {
233 if (strpos($this->new_image, '/') === FALSE)
234 {
235 $this->dest_folder = $this->source_folder;
236 $this->dest_image = $this->new_image;
237 }
238 else
239 {
240 if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE)
241 {
242 $full_dest_path = str_replace("\\", "/", realpath($this->new_image));
243 }
244 else
245 {
246 $full_dest_path = $this->new_image;
247 }
248
249 // Is there a file name?
250 if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
251 {
252 $this->dest_folder = $full_dest_path.'/';
253 $this->dest_image = $this->source_image;
254 }
255 else
256 {
257 $x = explode('/', $full_dest_path);
258 $this->dest_image = end($x);
259 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
260 }
261 }
262 }
263
264 /*
265 * Compile the finalized filenames/paths
266 *
267 * We'll create two master strings containing the
268 * full server path to the source image and the
269 * full server path to the destination image.
270 * We'll also split the destination image name
271 * so we can insert the thumbnail marker if needed.
272 *
273 */
274 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
275 {
276 $this->thumb_marker = '';
277 }
278
279 $xp = $this->explode_name($this->dest_image);
280
281 $filename = $xp['name'];
282 $file_ext = $xp['ext'];
283
284 $this->full_src_path = $this->source_folder.$this->source_image;
285 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
286
287 /*
288 * Should we maintain image proportions?
289 *
290 * When creating thumbs or copies, the target width/height
291 * might not be in correct proportion with the source
Derek Jones4b9c6292011-07-01 17:40:48 -0500292 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 *
294 */
295 if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
296 {
297 $this->image_reproportion();
298 }
299
300 /*
301 * Was a width and height specified?
302 *
303 * If the destination width/height was
304 * not submitted we will use the values
305 * from the actual file
306 *
307 */
308 if ($this->width == '')
309 $this->width = $this->orig_width;
310
311 if ($this->height == '')
312 $this->height = $this->orig_height;
313
314 // Set the quality
315 $this->quality = trim(str_replace("%", "", $this->quality));
316
317 if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality))
318 $this->quality = 90;
319
320 // Set the x/y coordinates
321 $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis;
322 $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis;
323
324 // Watermark-related Stuff...
325 if ($this->wm_font_color != '')
326 {
327 if (strlen($this->wm_font_color) == 6)
328 {
329 $this->wm_font_color = '#'.$this->wm_font_color;
330 }
331 }
332
333 if ($this->wm_shadow_color != '')
334 {
335 if (strlen($this->wm_shadow_color) == 6)
336 {
337 $this->wm_shadow_color = '#'.$this->wm_shadow_color;
338 }
339 }
340
341 if ($this->wm_overlay_path != '')
342 {
343 $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path));
344 }
345
346 if ($this->wm_shadow_color != '')
347 {
348 $this->wm_use_drop_shadow = TRUE;
349 }
350
351 if ($this->wm_font_path != '')
352 {
353 $this->wm_use_truetype = TRUE;
354 }
355
356 return TRUE;
357 }
358
359 // --------------------------------------------------------------------
360
361 /**
362 * Image Resize
363 *
364 * This is a wrapper function that chooses the proper
365 * resize function based on the protocol specified
366 *
367 * @access public
368 * @return bool
369 */
370 function resize()
371 {
372 $protocol = 'image_process_'.$this->image_library;
373
Derek Jones1322ec52009-03-11 17:01:14 +0000374 if (preg_match('/gd2$/i', $protocol))
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
376 $protocol = 'image_process_gd';
377 }
378
379 return $this->$protocol('resize');
380 }
381
382 // --------------------------------------------------------------------
383
384 /**
385 * Image Crop
386 *
387 * This is a wrapper function that chooses the proper
388 * cropping function based on the protocol specified
389 *
390 * @access public
391 * @return bool
392 */
393 function crop()
394 {
395 $protocol = 'image_process_'.$this->image_library;
396
Derek Jones1322ec52009-03-11 17:01:14 +0000397 if (preg_match('/gd2$/i', $protocol))
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
399 $protocol = 'image_process_gd';
400 }
401
402 return $this->$protocol('crop');
403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * Image Rotate
409 *
410 * This is a wrapper function that chooses the proper
411 * rotation function based on the protocol specified
412 *
413 * @access public
414 * @return bool
415 */
416 function rotate()
417 {
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');
Derek Jones4b9c6292011-07-01 17:40:48 -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;
444
445 return $this->$protocol('rotate');
446 }
447
448 if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
449 {
450 return $this->image_mirror_gd();
451 }
452 else
453 {
454 return $this->image_rotate_gd();
455 }
456 }
457
458 // --------------------------------------------------------------------
459
460 /**
461 * Image Process Using GD/GD2
462 *
463 * This function will resize or crop
464 *
465 * @access public
466 * @param string
467 * @return bool
468 */
469 function image_process_gd($action = 'resize')
470 {
471 $v2_override = FALSE;
472
473 // If the target width/height match the source, AND if the new file name is not equal to the old file name
474 // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
475 if ($this->dynamic_output === FALSE)
476 {
477 if ($this->orig_width == $this->width AND $this->orig_height == $this->height)
478 {
Barry Mienydd671972010-10-04 16:33:58 +0200479 if ($this->source_image != $this->new_image)
480 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 if (@copy($this->full_src_path, $this->full_dst_path))
482 {
Derek Jones172e1612009-10-13 14:32:48 +0000483 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
485 }
486
487 return TRUE;
488 }
489 }
490
491 // Let's set up our values based on the action
492 if ($action == 'crop')
493 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500494 // Reassign the source width/height if cropping
495 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 $this->orig_height = $this->height;
497
498 // GD 2.0 has a cropping bug so we'll test for it
499 if ($this->gd_version() !== FALSE)
500 {
501 $gd_version = str_replace('0', '', $this->gd_version());
502 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
503 }
504 }
505 else
506 {
507 // If resizing the x/y axis must be zero
508 $this->x_axis = 0;
509 $this->y_axis = 0;
510 }
511
Derek Jones4b9c6292011-07-01 17:40:48 -0500512 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 if ( ! ($src_img = $this->image_create_gd()))
514 {
515 return FALSE;
516 }
517
Derek Jones4b9c6292011-07-01 17:40:48 -0500518 // Create The Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500520 // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
521 // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
522 // below should that ever prove inaccurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500524 // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200525 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
527 $create = 'imagecreatetruecolor';
528 $copy = 'imagecopyresampled';
529 }
530 else
531 {
532 $create = 'imagecreate';
533 $copy = 'imagecopyresized';
534 }
535
536 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500537
538 if ($this->image_type == 3) // png we can actually preserve transparency
539 {
540 imagealphablending($dst_img, FALSE);
541 imagesavealpha($dst_img, TRUE);
542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
545
Derek Jones4b9c6292011-07-01 17:40:48 -0500546 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 if ($this->dynamic_output == TRUE)
548 {
549 $this->image_display_gd($dst_img);
550 }
551 else
552 {
553 // Or save it
554 if ( ! $this->image_save_gd($dst_img))
555 {
556 return FALSE;
557 }
558 }
559
Derek Jones4b9c6292011-07-01 17:40:48 -0500560 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 imagedestroy($dst_img);
562 imagedestroy($src_img);
563
564 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000565 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000566
567 return TRUE;
568 }
569
570 // --------------------------------------------------------------------
571
572 /**
573 * Image Process Using ImageMagick
574 *
575 * This function will resize, crop or rotate
576 *
577 * @access public
578 * @param string
579 * @return bool
580 */
581 function image_process_imagemagick($action = 'resize')
582 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500583 // Do we have a vaild library path?
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 if ($this->library_path == '')
585 {
586 $this->set_error('imglib_libpath_invalid');
587 return FALSE;
588 }
589
Derek Jones1322ec52009-03-11 17:01:14 +0000590 if ( ! preg_match("/convert$/i", $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
Derek Jones1322ec52009-03-11 17:01:14 +0000592 $this->library_path = rtrim($this->library_path, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000593
594 $this->library_path .= 'convert';
595 }
596
597 // Execute the command
598 $cmd = $this->library_path." -quality ".$this->quality;
599
600 if ($action == 'crop')
601 {
602 $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
603 }
604 elseif ($action == 'rotate')
605 {
606 switch ($this->rotation_angle)
607 {
Barry Mienydd671972010-10-04 16:33:58 +0200608 case 'hor' : $angle = '-flop';
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 break;
Barry Mienydd671972010-10-04 16:33:58 +0200610 case 'vrt' : $angle = '-flip';
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 break;
612 default : $angle = '-rotate '.$this->rotation_angle;
613 break;
614 }
615
616 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
617 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500618 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
620 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
621 }
622
623 $retval = 1;
624
625 @exec($cmd, $output, $retval);
626
627 // Did it work?
628 if ($retval > 0)
629 {
630 $this->set_error('imglib_image_process_failed');
631 return FALSE;
632 }
633
634 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000635 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000636
637 return TRUE;
638 }
639
640 // --------------------------------------------------------------------
641
642 /**
643 * Image Process Using NetPBM
644 *
645 * This function will resize, crop or rotate
646 *
647 * @access public
648 * @param string
649 * @return bool
650 */
651 function image_process_netpbm($action = 'resize')
652 {
653 if ($this->library_path == '')
654 {
655 $this->set_error('imglib_libpath_invalid');
656 return FALSE;
657 }
658
Derek Jones4b9c6292011-07-01 17:40:48 -0500659 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 switch ($this->image_type)
661 {
662 case 1 :
663 $cmd_in = 'giftopnm';
664 $cmd_out = 'ppmtogif';
665 break;
666 case 2 :
667 $cmd_in = 'jpegtopnm';
668 $cmd_out = 'ppmtojpeg';
669 break;
670 case 3 :
671 $cmd_in = 'pngtopnm';
672 $cmd_out = 'ppmtopng';
673 break;
674 }
675
676 if ($action == 'crop')
677 {
678 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
679 }
680 elseif ($action == 'rotate')
681 {
682 switch ($this->rotation_angle)
683 {
684 case 90 : $angle = 'r270';
685 break;
686 case 180 : $angle = 'r180';
687 break;
Barry Mienydd671972010-10-04 16:33:58 +0200688 case 270 : $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 break;
690 case 'vrt' : $angle = 'tb';
691 break;
692 case 'hor' : $angle = 'lr';
693 break;
694 }
695
696 $cmd_inner = 'pnmflip -'.$angle.' ';
697 }
698 else // Resize
699 {
700 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
701 }
702
703 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
704
705 $retval = 1;
706
707 @exec($cmd, $output, $retval);
708
Derek Jones4b9c6292011-07-01 17:40:48 -0500709 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 if ($retval > 0)
711 {
712 $this->set_error('imglib_image_process_failed');
713 return FALSE;
714 }
715
716 // With NetPBM we have to create a temporary image.
717 // If you try manipulating the original it fails so
718 // we have to rename the temp file.
719 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
720 unlink ($this->dest_folder.'netpbm.tmp');
Derek Jones172e1612009-10-13 14:32:48 +0000721 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000722
723 return TRUE;
724 }
725
726 // --------------------------------------------------------------------
727
728 /**
729 * Image Rotate Using GD
730 *
731 * @access public
732 * @return bool
733 */
734 function image_rotate_gd()
735 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500736 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 if ( ! ($src_img = $this->image_create_gd()))
738 {
739 return FALSE;
740 }
741
742 // Set the background color
743 // This won't work with transparent PNG files so we are
744 // going to have to figure out how to determine the color
745 // of the alpha channel in a future release.
746
747 $white = imagecolorallocate($src_img, 255, 255, 255);
748
Derek Jones4b9c6292011-07-01 17:40:48 -0500749 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
751
Derek Jones4b9c6292011-07-01 17:40:48 -0500752 // Save the Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 if ($this->dynamic_output == TRUE)
754 {
755 $this->image_display_gd($dst_img);
756 }
757 else
758 {
759 // Or save it
760 if ( ! $this->image_save_gd($dst_img))
761 {
762 return FALSE;
763 }
764 }
765
Derek Jones4b9c6292011-07-01 17:40:48 -0500766 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 imagedestroy($dst_img);
768 imagedestroy($src_img);
769
770 // Set the file to 777
771
Derek Jones172e1612009-10-13 14:32:48 +0000772 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000773
Pascal Kriete8761ef52011-02-14 13:13:52 -0500774 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 }
776
777 // --------------------------------------------------------------------
778
779 /**
780 * Create Mirror Image using GD
781 *
782 * This function will flip horizontal or vertical
783 *
784 * @access public
785 * @return bool
786 */
787 function image_mirror_gd()
788 {
789 if ( ! $src_img = $this->image_create_gd())
790 {
791 return FALSE;
792 }
793
Derek Jones4b9c6292011-07-01 17:40:48 -0500794 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 $height = $this->orig_height;
796
797 if ($this->rotation_angle == 'hor')
798 {
799 for ($i = 0; $i < $height; $i++)
800 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500801 $left = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 $right = $width-1;
803
804 while ($left < $right)
805 {
806 $cl = imagecolorat($src_img, $left, $i);
807 $cr = imagecolorat($src_img, $right, $i);
808
809 imagesetpixel($src_img, $left, $i, $cr);
810 imagesetpixel($src_img, $right, $i, $cl);
811
812 $left++;
813 $right--;
814 }
815 }
816 }
817 else
818 {
819 for ($i = 0; $i < $width; $i++)
820 {
821 $top = 0;
822 $bot = $height-1;
823
824 while ($top < $bot)
825 {
826 $ct = imagecolorat($src_img, $i, $top);
827 $cb = imagecolorat($src_img, $i, $bot);
828
829 imagesetpixel($src_img, $i, $top, $cb);
830 imagesetpixel($src_img, $i, $bot, $ct);
831
832 $top++;
833 $bot--;
834 }
835 }
836 }
837
Derek Jones4b9c6292011-07-01 17:40:48 -0500838 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 if ($this->dynamic_output == TRUE)
840 {
841 $this->image_display_gd($src_img);
842 }
843 else
844 {
845 // Or save it
846 if ( ! $this->image_save_gd($src_img))
847 {
848 return FALSE;
849 }
850 }
851
Derek Jones4b9c6292011-07-01 17:40:48 -0500852 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 imagedestroy($src_img);
854
855 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000856 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000857
858 return TRUE;
859 }
860
861 // --------------------------------------------------------------------
862
863 /**
864 * Image Watermark
865 *
866 * This is a wrapper function that chooses the type
867 * of watermarking based on the specified preference.
868 *
869 * @access public
870 * @param string
871 * @return bool
872 */
873 function watermark()
874 {
875 if ($this->wm_type == 'overlay')
876 {
877 return $this->overlay_watermark();
878 }
879 else
880 {
881 return $this->text_watermark();
882 }
883 }
884
885 // --------------------------------------------------------------------
886
887 /**
888 * Watermark - Graphic Version
889 *
890 * @access public
891 * @return bool
892 */
893 function overlay_watermark()
894 {
895 if ( ! function_exists('imagecolortransparent'))
896 {
897 $this->set_error('imglib_gd_required');
898 return FALSE;
899 }
900
Derek Jones4b9c6292011-07-01 17:40:48 -0500901 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 $this->get_image_properties();
903
Derek Jones4b9c6292011-07-01 17:40:48 -0500904 // Fetch watermark image properties
Barry Mienydd671972010-10-04 16:33:58 +0200905 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 $wm_img_type = $props['image_type'];
907 $wm_width = $props['width'];
908 $wm_height = $props['height'];
909
Derek Jones4b9c6292011-07-01 17:40:48 -0500910 // Create two image resources
911 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 $src_img = $this->image_create_gd($this->full_src_path);
913
914 // Reverse the offset if necessary
915 // When the image is positioned at the bottom
916 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -0500917 // further down. We want the reverse, so we'll
918 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 // offset when the image is at the right
920
921 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
922 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
923
924 if ($this->wm_vrt_alignment == 'B')
925 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
926
927 if ($this->wm_hor_alignment == 'R')
928 $this->wm_hor_offset = $this->wm_hor_offset * -1;
929
Derek Jones4b9c6292011-07-01 17:40:48 -0500930 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 $x_axis = $this->wm_hor_offset + $this->wm_padding;
932 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
933
Derek Jones4b9c6292011-07-01 17:40:48 -0500934 // Set the vertical position
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 switch ($this->wm_vrt_alignment)
936 {
937 case 'T':
938 break;
939 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
940 break;
941 case 'B': $y_axis += $this->orig_height - $wm_height;
942 break;
943 }
944
Derek Jones4b9c6292011-07-01 17:40:48 -0500945 // Set the horizontal position
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 switch ($this->wm_hor_alignment)
947 {
948 case 'L':
949 break;
950 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
951 break;
952 case 'R': $x_axis += $this->orig_width - $wm_width;
953 break;
954 }
955
Derek Jones4b9c6292011-07-01 17:40:48 -0500956 // Build the finalized image
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
958 {
959 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200960 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000961
962 // Set RGB values for text and shadow
963 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
964 $alpha = ($rgba & 0x7F000000) >> 24;
965
966 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
967 if ($alpha > 0)
968 {
969 // copy the image directly, the image's alpha transparency being the sole determinant of blending
970 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
971 }
972 else
973 {
974 // set our RGB value from above to be transparent and merge the images with the specified opacity
975 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
976 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
977 }
978
Derek Jones4b9c6292011-07-01 17:40:48 -0500979 // Output the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 if ($this->dynamic_output == TRUE)
981 {
982 $this->image_display_gd($src_img);
983 }
984 else
985 {
986 if ( ! $this->image_save_gd($src_img))
987 {
988 return FALSE;
989 }
990 }
991
992 imagedestroy($src_img);
993 imagedestroy($wm_img);
994
995 return TRUE;
996 }
997
998 // --------------------------------------------------------------------
999
1000 /**
1001 * Watermark - Text Version
1002 *
1003 * @access public
1004 * @return bool
1005 */
1006 function text_watermark()
1007 {
1008 if ( ! ($src_img = $this->image_create_gd()))
1009 {
1010 return FALSE;
1011 }
1012
1013 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
1014 {
1015 $this->set_error('imglib_missing_font');
1016 return FALSE;
1017 }
1018
Derek Jones4b9c6292011-07-01 17:40:48 -05001019 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 $this->get_image_properties();
1021
1022 // Set RGB values for text and shadow
1023 $this->wm_font_color = str_replace('#', '', $this->wm_font_color);
1024 $this->wm_shadow_color = str_replace('#', '', $this->wm_shadow_color);
1025
1026 $R1 = hexdec(substr($this->wm_font_color, 0, 2));
1027 $G1 = hexdec(substr($this->wm_font_color, 2, 2));
1028 $B1 = hexdec(substr($this->wm_font_color, 4, 2));
1029
1030 $R2 = hexdec(substr($this->wm_shadow_color, 0, 2));
1031 $G2 = hexdec(substr($this->wm_shadow_color, 2, 2));
1032 $B2 = hexdec(substr($this->wm_shadow_color, 4, 2));
1033
1034 $txt_color = imagecolorclosest($src_img, $R1, $G1, $B1);
1035 $drp_color = imagecolorclosest($src_img, $R2, $G2, $B2);
1036
1037 // Reverse the vertical offset
1038 // When the image is positioned at the bottom
1039 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -05001040 // further down. We want the reverse, so we'll
1041 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 // offset flips itself automatically
1043
1044 if ($this->wm_vrt_alignment == 'B')
1045 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1046
1047 if ($this->wm_hor_alignment == 'R')
1048 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1049
1050 // Set font width and height
1051 // These are calculated differently depending on
1052 // whether we are using the true type font or not
1053 if ($this->wm_use_truetype == TRUE)
1054 {
1055 if ($this->wm_font_size == '')
1056 $this->wm_font_size = '17';
1057
Derek Jones4b9c6292011-07-01 17:40:48 -05001058 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 $fontheight = $this->wm_font_size;
1060 $this->wm_vrt_offset += $this->wm_font_size;
1061 }
1062 else
1063 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001064 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 $fontheight = imagefontheight($this->wm_font_size);
1066 }
1067
1068 // Set base X and Y axis values
1069 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1070 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1071
1072 // Set verticle alignment
1073 if ($this->wm_use_drop_shadow == FALSE)
1074 $this->wm_shadow_distance = 0;
1075
1076 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1077 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1078
1079 switch ($this->wm_vrt_alignment)
1080 {
1081 case "T" :
1082 break;
1083 case "M": $y_axis += ($this->orig_height/2)+($fontheight/2);
1084 break;
1085 case "B": $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
1086 break;
1087 }
1088
1089 $x_shad = $x_axis + $this->wm_shadow_distance;
1090 $y_shad = $y_axis + $this->wm_shadow_distance;
1091
1092 // Set horizontal alignment
1093 switch ($this->wm_hor_alignment)
1094 {
1095 case "L":
1096 break;
1097 case "R":
1098 if ($this->wm_use_drop_shadow)
1099 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1100 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1101 break;
1102 case "C":
1103 if ($this->wm_use_drop_shadow)
1104 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
Derek Jones4b9c6292011-07-01 17:40:48 -05001105 $x_axis += floor(($this->orig_width -$fontwidth*strlen($this->wm_text))/2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 break;
1107 }
1108
Derek Jones4b9c6292011-07-01 17:40:48 -05001109 // Add the text to the source image
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 if ($this->wm_use_truetype)
1111 {
1112 if ($this->wm_use_drop_shadow)
1113 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1114 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1115 }
1116 else
1117 {
1118 if ($this->wm_use_drop_shadow)
1119 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1120 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1121 }
1122
Derek Jones4b9c6292011-07-01 17:40:48 -05001123 // Output the final image
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 if ($this->dynamic_output == TRUE)
1125 {
1126 $this->image_display_gd($src_img);
1127 }
1128 else
1129 {
1130 $this->image_save_gd($src_img);
1131 }
1132
1133 imagedestroy($src_img);
1134
1135 return TRUE;
1136 }
1137
1138 // --------------------------------------------------------------------
1139
1140 /**
1141 * Create Image - GD
1142 *
1143 * This simply creates an image resource handle
1144 * based on the type of image being processed
1145 *
1146 * @access public
1147 * @param string
1148 * @return resource
1149 */
1150 function image_create_gd($path = '', $image_type = '')
1151 {
1152 if ($path == '')
1153 $path = $this->full_src_path;
1154
1155 if ($image_type == '')
1156 $image_type = $this->image_type;
1157
1158
1159 switch ($image_type)
1160 {
1161 case 1 :
1162 if ( ! function_exists('imagecreatefromgif'))
1163 {
1164 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1165 return FALSE;
1166 }
1167
1168 return imagecreatefromgif($path);
1169 break;
1170 case 2 :
1171 if ( ! function_exists('imagecreatefromjpeg'))
1172 {
1173 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1174 return FALSE;
1175 }
1176
1177 return imagecreatefromjpeg($path);
1178 break;
1179 case 3 :
1180 if ( ! function_exists('imagecreatefrompng'))
1181 {
1182 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1183 return FALSE;
1184 }
1185
1186 return imagecreatefrompng($path);
1187 break;
1188
1189 }
1190
1191 $this->set_error(array('imglib_unsupported_imagecreate'));
1192 return FALSE;
1193 }
1194
1195 // --------------------------------------------------------------------
1196
1197 /**
1198 * Write image file to disk - GD
1199 *
1200 * Takes an image resource as input and writes the file
1201 * to the specified destination
1202 *
1203 * @access public
1204 * @param resource
1205 * @return bool
1206 */
1207 function image_save_gd($resource)
1208 {
1209 switch ($this->image_type)
1210 {
1211 case 1 :
1212 if ( ! function_exists('imagegif'))
1213 {
1214 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1215 return FALSE;
1216 }
1217
Derek Jones541ddbd2008-12-09 15:25:31 +00001218 if ( ! @imagegif($resource, $this->full_dst_path))
1219 {
1220 $this->set_error('imglib_save_failed');
1221 return FALSE;
1222 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 break;
1224 case 2 :
1225 if ( ! function_exists('imagejpeg'))
1226 {
1227 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1228 return FALSE;
1229 }
1230
Derek Jones541ddbd2008-12-09 15:25:31 +00001231 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1232 {
1233 $this->set_error('imglib_save_failed');
1234 return FALSE;
1235 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 break;
1237 case 3 :
1238 if ( ! function_exists('imagepng'))
1239 {
1240 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1241 return FALSE;
1242 }
1243
Derek Jones541ddbd2008-12-09 15:25:31 +00001244 if ( ! @imagepng($resource, $this->full_dst_path))
1245 {
1246 $this->set_error('imglib_save_failed');
1247 return FALSE;
1248 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 break;
1250 default :
1251 $this->set_error(array('imglib_unsupported_imagecreate'));
1252 return FALSE;
1253 break;
1254 }
1255
1256 return TRUE;
1257 }
1258
1259 // --------------------------------------------------------------------
1260
1261 /**
1262 * Dynamically outputs an image
1263 *
1264 * @access public
1265 * @param resource
1266 * @return void
1267 */
1268 function image_display_gd($resource)
1269 {
1270 header("Content-Disposition: filename={$this->source_image};");
1271 header("Content-Type: {$this->mime_type}");
1272 header('Content-Transfer-Encoding: binary');
1273 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1274
1275 switch ($this->image_type)
1276 {
Barry Mienydd671972010-10-04 16:33:58 +02001277 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 break;
1279 case 2 : imagejpeg($resource, '', $this->quality);
1280 break;
1281 case 3 : imagepng($resource);
1282 break;
1283 default : echo 'Unable to display the image';
1284 break;
1285 }
1286 }
1287
1288 // --------------------------------------------------------------------
1289
1290 /**
1291 * Re-proportion Image Width/Height
1292 *
1293 * When creating thumbs, the desired width/height
1294 * can end up warping the image due to an incorrect
1295 * ratio between the full-sized image and the thumb.
1296 *
1297 * This function lets us re-proportion the width/height
1298 * if users choose to maintain the aspect ratio when resizing.
1299 *
1300 * @access public
1301 * @return void
1302 */
1303 function image_reproportion()
1304 {
1305 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
1306 return;
1307
1308 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
1309 return;
1310
1311 $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
1312 $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
1313
1314 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
1315
1316 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
1317 {
1318 $this->master_dim = ($ratio < 0) ? 'width' : 'height';
1319 }
1320
1321 if (($this->width != $new_width) AND ($this->height != $new_height))
1322 {
1323 if ($this->master_dim == 'height')
1324 {
1325 $this->width = $new_width;
1326 }
1327 else
1328 {
1329 $this->height = $new_height;
1330 }
1331 }
1332 }
1333
1334 // --------------------------------------------------------------------
1335
1336 /**
1337 * Get image properties
1338 *
1339 * A helper function that gets info about the file
1340 *
1341 * @access public
1342 * @param string
1343 * @return mixed
1344 */
1345 function get_image_properties($path = '', $return = FALSE)
1346 {
1347 // For now we require GD but we should
1348 // find a way to determine this using IM or NetPBM
1349
1350 if ($path == '')
1351 $path = $this->full_src_path;
1352
1353 if ( ! file_exists($path))
1354 {
1355 $this->set_error('imglib_invalid_path');
1356 return FALSE;
1357 }
1358
1359 $vals = @getimagesize($path);
1360
1361 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
1362
1363 $mime = (isset($types[$vals['2']])) ? 'image/'.$types[$vals['2']] : 'image/jpg';
1364
1365 if ($return == TRUE)
1366 {
1367 $v['width'] = $vals['0'];
1368 $v['height'] = $vals['1'];
1369 $v['image_type'] = $vals['2'];
1370 $v['size_str'] = $vals['3'];
1371 $v['mime_type'] = $mime;
1372
1373 return $v;
1374 }
1375
1376 $this->orig_width = $vals['0'];
1377 $this->orig_height = $vals['1'];
1378 $this->image_type = $vals['2'];
1379 $this->size_str = $vals['3'];
1380 $this->mime_type = $mime;
1381
1382 return TRUE;
1383 }
1384
1385 // --------------------------------------------------------------------
1386
1387 /**
1388 * Size calculator
1389 *
1390 * This function takes a known width x height and
Derek Jones4b9c6292011-07-01 17:40:48 -05001391 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 * new variable needs to be known
1393 *
1394 * $props = array(
Barry Mienydd671972010-10-04 16:33:58 +02001395 * 'width' => $width,
1396 * 'height' => $height,
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 * 'new_width' => 40,
1398 * 'new_height' => ''
Derek Jones4b9c6292011-07-01 17:40:48 -05001399 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 *
1401 * @access public
1402 * @param array
1403 * @return array
1404 */
1405 function size_calculator($vals)
1406 {
1407 if ( ! is_array($vals))
1408 {
1409 return;
1410 }
1411
1412 $allowed = array('new_width', 'new_height', 'width', 'height');
1413
1414 foreach ($allowed as $item)
1415 {
1416 if ( ! isset($vals[$item]) OR $vals[$item] == '')
1417 $vals[$item] = 0;
1418 }
1419
1420 if ($vals['width'] == 0 OR $vals['height'] == 0)
1421 {
1422 return $vals;
1423 }
1424
1425 if ($vals['new_width'] == 0)
1426 {
1427 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1428 }
1429 elseif ($vals['new_height'] == 0)
1430 {
1431 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1432 }
1433
1434 return $vals;
1435 }
1436
1437 // --------------------------------------------------------------------
1438
1439 /**
1440 * Explode source_image
1441 *
1442 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001443 * from the source_image. This function lets us deal with
1444 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001445 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001446 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 * $array['name'] = 'my.cool';
1448 *
1449 * @access public
1450 * @param array
1451 * @return array
1452 */
1453 function explode_name($source_image)
1454 {
Derek Jones08cae632009-02-10 20:03:29 +00001455 $ext = strrchr($source_image, '.');
1456 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001457
Derek Jones08cae632009-02-10 20:03:29 +00001458 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 }
1460
1461 // --------------------------------------------------------------------
1462
1463 /**
1464 * Is GD Installed?
1465 *
1466 * @access public
1467 * @return bool
1468 */
1469 function gd_loaded()
1470 {
1471 if ( ! extension_loaded('gd'))
1472 {
1473 if ( ! dl('gd.so'))
1474 {
1475 return FALSE;
1476 }
1477 }
1478
1479 return TRUE;
1480 }
1481
1482 // --------------------------------------------------------------------
1483
1484 /**
1485 * Get GD version
1486 *
1487 * @access public
1488 * @return mixed
1489 */
1490 function gd_version()
1491 {
1492 if (function_exists('gd_info'))
1493 {
1494 $gd_version = @gd_info();
1495 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
1496
1497 return $gd_version;
1498 }
1499
1500 return FALSE;
1501 }
1502
1503 // --------------------------------------------------------------------
1504
1505 /**
1506 * Set error message
1507 *
1508 * @access public
1509 * @param string
1510 * @return void
1511 */
1512 function set_error($msg)
1513 {
1514 $CI =& get_instance();
1515 $CI->lang->load('imglib');
1516
1517 if (is_array($msg))
1518 {
1519 foreach ($msg as $val)
1520 {
1521
1522 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1523 $this->error_msg[] = $msg;
1524 log_message('error', $msg);
1525 }
1526 }
1527 else
1528 {
1529 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1530 $this->error_msg[] = $msg;
1531 log_message('error', $msg);
1532 }
1533 }
1534
1535 // --------------------------------------------------------------------
1536
1537 /**
1538 * Show error messages
1539 *
1540 * @access public
1541 * @param string
1542 * @return string
1543 */
1544 function display_errors($open = '<p>', $close = '</p>')
1545 {
1546 $str = '';
1547 foreach ($this->error_msg as $val)
1548 {
1549 $str .= $open.$val.$close;
1550 }
1551
1552 return $str;
1553 }
1554
1555}
1556// END Image_lib Class
1557
1558/* End of file Image_lib.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001559/* Location: ./system/libraries/Image_lib.php */