1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * Image Manipulation class
21 * @package CodeIgniter
22 * @subpackage Libraries
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/libraries/image_lib.html
29 var $image_library = 'gd2'; // Can be: imagemagick, netpbm, gd, gd2
30 var $library_path = '';
31 var $dynamic_output = FALSE; // Whether to send to browser or write to disk
32 var $source_image = '';
37 var $create_thumb = FALSE;
38 var $thumb_marker = '_thumb';
39 var $maintain_ratio = TRUE; // Whether to maintain aspect ratio when resizing or use hard values
40 var $master_dim = 'auto'; // auto, height, or width. Determines what to use as the master dimension
41 var $rotation_angle = '';
46 var $wm_text = ''; // Watermark text if graphic is not used
47 var $wm_type = 'text'; // Type of watermarking. Options: text/overlay
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)
53 var $wm_vrt_alignment = 'B'; // Vertical alignment: T M B
54 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
57 var $wm_vrt_offset = 0; // Lets you push text down
58 var $wm_font_color = '#ffffff'; // Text color
59 var $wm_shadow_color = ''; // Dropshadow color
60 var $wm_shadow_distance = 2; // Dropshadow distance
61 var $wm_opacity = 50; // Image opacity: 1 - 100 Only works with image
64 var $source_folder = '';
65 var $dest_folder = '';
68 var $orig_height = '';
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;
86 function CI_Image_lib($props = array())
88 if (count($props) > 0)
90 $this->initialize($props);
93 log_message('debug', "Image Lib Class Initialized");
96 // --------------------------------------------------------------------
99 * Initialize image properties
101 * Resets values in case this class is used in a loop
108 $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');
110 foreach ($props as $val)
115 // special consideration for master_dim
116 $this->master_dim = 'auto';
119 // --------------------------------------------------------------------
122 * initialize image preferences
128 function initialize($props = array())
131 * Convert array elements into class variables
133 if (count($props) > 0)
135 foreach ($props as $key => $val)
142 * Is there a source image?
144 * If not, there's no reason to continue
147 if ($this->source_image == '')
149 $this->set_error('imglib_source_image_required');
154 * Is getimagesize() Available?
156 * We use it to determine the image properties (width/height).
157 * Note: We need to figure out how to determine image
158 * properties using ImageMagick and NetPBM
161 if ( ! function_exists('getimagesize'))
163 $this->set_error('imglib_gd_required_for_props');
167 $this->image_library = strtolower($this->image_library);
170 * Set the full server path
172 * The source image may or may not contain a path.
173 * Either way, we'll try use realpath to generate the
174 * full server path in order to more reliably read it.
177 if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE)
179 $full_source_path = str_replace("\\", "/", realpath($this->source_image));
183 $full_source_path = $this->source_image;
186 $x = explode('/', $full_source_path);
187 $this->source_image = end($x);
188 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
190 // Set the Image Properties
191 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
197 * Assign the "new" image name/path
199 * If the user has set a "new_image" name it means
200 * we are making a copy of the source image. If not
201 * it means we are altering the original. We'll
202 * set the destination filename and path accordingly.
205 if ($this->new_image == '')
207 $this->dest_image = $this->source_image;
208 $this->dest_folder = $this->source_folder;
212 if (strpos($this->new_image, '/') === FALSE)
214 $this->dest_folder = $this->source_folder;
215 $this->dest_image = $this->new_image;
219 if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE)
221 $full_dest_path = str_replace("\\", "/", realpath($this->new_image));
225 $full_dest_path = $this->new_image;
228 // Is there a file name?
229 if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
231 $this->dest_folder = $full_dest_path.'/';
232 $this->dest_image = $this->source_image;
236 $x = explode('/', $full_dest_path);
237 $this->dest_image = end($x);
238 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
244 * Compile the finalized filenames/paths
246 * We'll create two master strings containing the
247 * full server path to the source image and the
248 * full server path to the destination image.
249 * We'll also split the destination image name
250 * so we can insert the thumbnail marker if needed.
253 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
255 $this->thumb_marker = '';
258 $xp = $this->explode_name($this->dest_image);
260 $filename = $xp['name'];
261 $file_ext = $xp['ext'];
263 $this->full_src_path = $this->source_folder.$this->source_image;
264 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
267 * Should we maintain image proportions?
269 * When creating thumbs or copies, the target width/height
270 * might not be in correct proportion with the source
271 * image's width/height. We'll recalculate it here.
274 if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
276 $this->image_reproportion();
280 * Was a width and height specified?
282 * If the destination width/height was
283 * not submitted we will use the values
284 * from the actual file
287 if ($this->width == '')
288 $this->width = $this->orig_width;
290 if ($this->height == '')
291 $this->height = $this->orig_height;
294 $this->quality = trim(str_replace("%", "", $this->quality));
296 if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality))
299 // Set the x/y coordinates
300 $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis;
301 $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis;
303 // Watermark-related Stuff...
304 if ($this->wm_font_color != '')
306 if (strlen($this->wm_font_color) == 6)
308 $this->wm_font_color = '#'.$this->wm_font_color;
312 if ($this->wm_shadow_color != '')
314 if (strlen($this->wm_shadow_color) == 6)
316 $this->wm_shadow_color = '#'.$this->wm_shadow_color;
320 if ($this->wm_overlay_path != '')
322 $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path));
325 if ($this->wm_shadow_color != '')
327 $this->wm_use_drop_shadow = TRUE;
330 if ($this->wm_font_path != '')
332 $this->wm_use_truetype = TRUE;
338 // --------------------------------------------------------------------
343 * This is a wrapper function that chooses the proper
344 * resize function based on the protocol specified
351 $protocol = 'image_process_'.$this->image_library;
353 if (preg_match('/gd2$/i', $protocol))
355 $protocol = 'image_process_gd';
358 return $this->$protocol('resize');
361 // --------------------------------------------------------------------
366 * This is a wrapper function that chooses the proper
367 * cropping function based on the protocol specified
374 $protocol = 'image_process_'.$this->image_library;
376 if (preg_match('/gd2$/i', $protocol))
378 $protocol = 'image_process_gd';
381 return $this->$protocol('crop');
384 // --------------------------------------------------------------------
389 * This is a wrapper function that chooses the proper
390 * rotation function based on the protocol specified
397 // Allowed rotation values
398 $degs = array(90, 180, 270, 'vrt', 'hor');
400 if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs))
402 $this->set_error('imglib_rotation_angle_required');
406 // Reassign the width and height
407 if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
409 $this->width = $this->orig_height;
410 $this->height = $this->orig_width;
414 $this->width = $this->orig_width;
415 $this->height = $this->orig_height;
419 // Choose resizing function
420 if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm')
422 $protocol = 'image_process_'.$this->image_library;
424 return $this->$protocol('rotate');
427 if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
429 return $this->image_mirror_gd();
433 return $this->image_rotate_gd();
437 // --------------------------------------------------------------------
440 * Image Process Using GD/GD2
442 * This function will resize or crop
448 function image_process_gd($action = 'resize')
450 $v2_override = FALSE;
452 // If the target width/height match the source, AND if the new file name is not equal to the old file name
453 // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
454 if ($this->dynamic_output === FALSE)
456 if ($this->orig_width == $this->width AND $this->orig_height == $this->height)
458 if ($this->source_image != $this->new_image)
460 if (@copy($this->full_src_path, $this->full_dst_path))
462 @chmod($this->full_dst_path, DIR_WRITE_MODE);
470 // Let's set up our values based on the action
471 if ($action == 'crop')
473 // Reassign the source width/height if cropping
474 $this->orig_width = $this->width;
475 $this->orig_height = $this->height;
477 // GD 2.0 has a cropping bug so we'll test for it
478 if ($this->gd_version() !== FALSE)
480 $gd_version = str_replace('0', '', $this->gd_version());
481 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
486 // If resizing the x/y axis must be zero
491 // Create the image handle
492 if ( ! ($src_img = $this->image_create_gd()))
499 // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
500 // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
501 // below should that ever prove inaccurate.
503 // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
504 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
506 $create = 'imagecreatetruecolor';
507 $copy = 'imagecopyresampled';
511 $create = 'imagecreate';
512 $copy = 'imagecopyresized';
515 $dst_img = $create($this->width, $this->height);
516 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
519 if ($this->dynamic_output == TRUE)
521 $this->image_display_gd($dst_img);
526 if ( ! $this->image_save_gd($dst_img))
532 // Kill the file handles
533 imagedestroy($dst_img);
534 imagedestroy($src_img);
536 // Set the file to 777
537 @chmod($this->full_dst_path, DIR_WRITE_MODE);
542 // --------------------------------------------------------------------
545 * Image Process Using ImageMagick
547 * This function will resize, crop or rotate
553 function image_process_imagemagick($action = 'resize')
555 // Do we have a vaild library path?
556 if ($this->library_path == '')
558 $this->set_error('imglib_libpath_invalid');
562 if ( ! preg_match("/convert$/i", $this->library_path))
564 $this->library_path = rtrim($this->library_path, '/').'/';
566 $this->library_path .= 'convert';
569 // Execute the command
570 $cmd = $this->library_path." -quality ".$this->quality;
572 if ($action == 'crop')
574 $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
576 elseif ($action == 'rotate')
578 switch ($this->rotation_angle)
580 case 'hor' : $angle = '-flop';
582 case 'vrt' : $angle = '-flip';
584 default : $angle = '-rotate '.$this->rotation_angle;
588 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
592 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
597 @exec($cmd, $output, $retval);
602 $this->set_error('imglib_image_process_failed');
606 // Set the file to 777
607 @chmod($this->full_dst_path, DIR_WRITE_MODE);
612 // --------------------------------------------------------------------
615 * Image Process Using NetPBM
617 * This function will resize, crop or rotate
623 function image_process_netpbm($action = 'resize')
625 if ($this->library_path == '')
627 $this->set_error('imglib_libpath_invalid');
631 // Build the resizing command
632 switch ($this->image_type)
635 $cmd_in = 'giftopnm';
636 $cmd_out = 'ppmtogif';
639 $cmd_in = 'jpegtopnm';
640 $cmd_out = 'ppmtojpeg';
643 $cmd_in = 'pngtopnm';
644 $cmd_out = 'ppmtopng';
648 if ($action == 'crop')
650 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
652 elseif ($action == 'rotate')
654 switch ($this->rotation_angle)
656 case 90 : $angle = 'r270';
658 case 180 : $angle = 'r180';
660 case 270 : $angle = 'r90';
662 case 'vrt' : $angle = 'tb';
664 case 'hor' : $angle = 'lr';
668 $cmd_inner = 'pnmflip -'.$angle.' ';
672 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
675 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
679 @exec($cmd, $output, $retval);
684 $this->set_error('imglib_image_process_failed');
688 // With NetPBM we have to create a temporary image.
689 // If you try manipulating the original it fails so
690 // we have to rename the temp file.
691 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
692 unlink ($this->dest_folder.'netpbm.tmp');
693 @chmod($this->full_dst_path, DIR_WRITE_MODE);
698 // --------------------------------------------------------------------
701 * Image Rotate Using GD
706 function image_rotate_gd()
708 // Is Image Rotation Supported?
709 // this function is only supported as of PHP 4.3
710 if ( ! function_exists('imagerotate'))
712 $this->set_error('imglib_rotate_unsupported');
716 // Create the image handle
717 if ( ! ($src_img = $this->image_create_gd()))
722 // Set the background color
723 // This won't work with transparent PNG files so we are
724 // going to have to figure out how to determine the color
725 // of the alpha channel in a future release.
727 $white = imagecolorallocate($src_img, 255, 255, 255);
730 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
733 if ($this->dynamic_output == TRUE)
735 $this->image_display_gd($dst_img);
740 if ( ! $this->image_save_gd($dst_img))
746 // Kill the file handles
747 imagedestroy($dst_img);
748 imagedestroy($src_img);
750 // Set the file to 777
752 @chmod($this->full_dst_path, DIR_WRITE_MODE);
757 // --------------------------------------------------------------------
760 * Create Mirror Image using GD
762 * This function will flip horizontal or vertical
767 function image_mirror_gd()
769 if ( ! $src_img = $this->image_create_gd())
774 $width = $this->orig_width;
775 $height = $this->orig_height;
777 if ($this->rotation_angle == 'hor')
779 for ($i = 0; $i < $height; $i++)
784 while ($left < $right)
786 $cl = imagecolorat($src_img, $left, $i);
787 $cr = imagecolorat($src_img, $right, $i);
789 imagesetpixel($src_img, $left, $i, $cr);
790 imagesetpixel($src_img, $right, $i, $cl);
799 for ($i = 0; $i < $width; $i++)
806 $ct = imagecolorat($src_img, $i, $top);
807 $cb = imagecolorat($src_img, $i, $bot);
809 imagesetpixel($src_img, $i, $top, $cb);
810 imagesetpixel($src_img, $i, $bot, $ct);
819 if ($this->dynamic_output == TRUE)
821 $this->image_display_gd($src_img);
826 if ( ! $this->image_save_gd($src_img))
832 // Kill the file handles
833 imagedestroy($src_img);
835 // Set the file to 777
836 @chmod($this->full_dst_path, DIR_WRITE_MODE);
841 // --------------------------------------------------------------------
846 * This is a wrapper function that chooses the type
847 * of watermarking based on the specified preference.
855 if ($this->wm_type == 'overlay')
857 return $this->overlay_watermark();
861 return $this->text_watermark();
865 // --------------------------------------------------------------------
868 * Watermark - Graphic Version
873 function overlay_watermark()
875 if ( ! function_exists('imagecolortransparent'))
877 $this->set_error('imglib_gd_required');
881 // Fetch source image properties
882 $this->get_image_properties();
884 // Fetch watermark image properties
885 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
886 $wm_img_type = $props['image_type'];
887 $wm_width = $props['width'];
888 $wm_height = $props['height'];
890 // Create two image resources
891 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
892 $src_img = $this->image_create_gd($this->full_src_path);
894 // Reverse the offset if necessary
895 // When the image is positioned at the bottom
896 // we don't want the vertical offset to push it
897 // further down. We want the reverse, so we'll
898 // invert the offset. Same with the horizontal
899 // offset when the image is at the right
901 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
902 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
904 if ($this->wm_vrt_alignment == 'B')
905 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
907 if ($this->wm_hor_alignment == 'R')
908 $this->wm_hor_offset = $this->wm_hor_offset * -1;
910 // Set the base x and y axis values
911 $x_axis = $this->wm_hor_offset + $this->wm_padding;
912 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
914 // Set the vertical position
915 switch ($this->wm_vrt_alignment)
919 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
921 case 'B': $y_axis += $this->orig_height - $wm_height;
925 // Set the horizontal position
926 switch ($this->wm_hor_alignment)
930 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
932 case 'R': $x_axis += $this->orig_width - $wm_width;
936 // Build the finalized image
937 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
939 @imagealphablending($src_img, TRUE);
942 // Set RGB values for text and shadow
943 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
944 $alpha = ($rgba & 0x7F000000) >> 24;
946 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
949 // copy the image directly, the image's alpha transparency being the sole determinant of blending
950 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
954 // set our RGB value from above to be transparent and merge the images with the specified opacity
955 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
956 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
960 if ($this->dynamic_output == TRUE)
962 $this->image_display_gd($src_img);
966 if ( ! $this->image_save_gd($src_img))
972 imagedestroy($src_img);
973 imagedestroy($wm_img);
978 // --------------------------------------------------------------------
981 * Watermark - Text Version
986 function text_watermark()
988 if ( ! ($src_img = $this->image_create_gd()))
993 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
995 $this->set_error('imglib_missing_font');
999 // Fetch source image properties
1000 $this->get_image_properties();
1002 // Set RGB values for text and shadow
1003 $this->wm_font_color = str_replace('#', '', $this->wm_font_color);
1004 $this->wm_shadow_color = str_replace('#', '', $this->wm_shadow_color);
1006 $R1 = hexdec(substr($this->wm_font_color, 0, 2));
1007 $G1 = hexdec(substr($this->wm_font_color, 2, 2));
1008 $B1 = hexdec(substr($this->wm_font_color, 4, 2));
1010 $R2 = hexdec(substr($this->wm_shadow_color, 0, 2));
1011 $G2 = hexdec(substr($this->wm_shadow_color, 2, 2));
1012 $B2 = hexdec(substr($this->wm_shadow_color, 4, 2));
1014 $txt_color = imagecolorclosest($src_img, $R1, $G1, $B1);
1015 $drp_color = imagecolorclosest($src_img, $R2, $G2, $B2);
1017 // Reverse the vertical offset
1018 // When the image is positioned at the bottom
1019 // we don't want the vertical offset to push it
1020 // further down. We want the reverse, so we'll
1021 // invert the offset. Note: The horizontal
1022 // offset flips itself automatically
1024 if ($this->wm_vrt_alignment == 'B')
1025 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1027 if ($this->wm_hor_alignment == 'R')
1028 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1030 // Set font width and height
1031 // These are calculated differently depending on
1032 // whether we are using the true type font or not
1033 if ($this->wm_use_truetype == TRUE)
1035 if ($this->wm_font_size == '')
1036 $this->wm_font_size = '17';
1038 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
1039 $fontheight = $this->wm_font_size;
1040 $this->wm_vrt_offset += $this->wm_font_size;
1044 $fontwidth = imagefontwidth($this->wm_font_size);
1045 $fontheight = imagefontheight($this->wm_font_size);
1048 // Set base X and Y axis values
1049 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1050 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1052 // Set verticle alignment
1053 if ($this->wm_use_drop_shadow == FALSE)
1054 $this->wm_shadow_distance = 0;
1056 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1057 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1059 switch ($this->wm_vrt_alignment)
1063 case "M": $y_axis += ($this->orig_height/2)+($fontheight/2);
1065 case "B": $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
1069 $x_shad = $x_axis + $this->wm_shadow_distance;
1070 $y_shad = $y_axis + $this->wm_shadow_distance;
1072 // Set horizontal alignment
1073 switch ($this->wm_hor_alignment)
1078 if ($this->wm_use_drop_shadow)
1079 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1080 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1083 if ($this->wm_use_drop_shadow)
1084 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1085 $x_axis += floor(($this->orig_width -$fontwidth*strlen($this->wm_text))/2);
1089 // Add the text to the source image
1090 if ($this->wm_use_truetype)
1092 if ($this->wm_use_drop_shadow)
1093 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1094 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1098 if ($this->wm_use_drop_shadow)
1099 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1100 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1103 // Output the final image
1104 if ($this->dynamic_output == TRUE)
1106 $this->image_display_gd($src_img);
1110 $this->image_save_gd($src_img);
1113 imagedestroy($src_img);
1118 // --------------------------------------------------------------------
1123 * This simply creates an image resource handle
1124 * based on the type of image being processed
1130 function image_create_gd($path = '', $image_type = '')
1133 $path = $this->full_src_path;
1135 if ($image_type == '')
1136 $image_type = $this->image_type;
1139 switch ($image_type)
1142 if ( ! function_exists('imagecreatefromgif'))
1144 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1148 return imagecreatefromgif($path);
1151 if ( ! function_exists('imagecreatefromjpeg'))
1153 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1157 return imagecreatefromjpeg($path);
1160 if ( ! function_exists('imagecreatefrompng'))
1162 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1166 return imagecreatefrompng($path);
1171 $this->set_error(array('imglib_unsupported_imagecreate'));
1175 // --------------------------------------------------------------------
1178 * Write image file to disk - GD
1180 * Takes an image resource as input and writes the file
1181 * to the specified destination
1187 function image_save_gd($resource)
1189 switch ($this->image_type)
1192 if ( ! function_exists('imagegif'))
1194 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1198 if ( ! @imagegif($resource, $this->full_dst_path))
1200 $this->set_error('imglib_save_failed');
1205 if ( ! function_exists('imagejpeg'))
1207 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1211 if (phpversion() == '4.4.1')
1213 @touch($this->full_dst_path); // PHP 4.4.1 bug #35060 - workaround
1216 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1218 $this->set_error('imglib_save_failed');
1223 if ( ! function_exists('imagepng'))
1225 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1229 if ( ! @imagepng($resource, $this->full_dst_path))
1231 $this->set_error('imglib_save_failed');
1236 $this->set_error(array('imglib_unsupported_imagecreate'));
1244 // --------------------------------------------------------------------
1247 * Dynamically outputs an image
1253 function image_display_gd($resource)
1255 header("Content-Disposition: filename={$this->source_image};");
1256 header("Content-Type: {$this->mime_type}");
1257 header('Content-Transfer-Encoding: binary');
1258 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1260 switch ($this->image_type)
1262 case 1 : imagegif($resource);
1264 case 2 : imagejpeg($resource, '', $this->quality);
1266 case 3 : imagepng($resource);
1268 default : echo 'Unable to display the image';
1273 // --------------------------------------------------------------------
1276 * Re-proportion Image Width/Height
1278 * When creating thumbs, the desired width/height
1279 * can end up warping the image due to an incorrect
1280 * ratio between the full-sized image and the thumb.
1282 * This function lets us re-proportion the width/height
1283 * if users choose to maintain the aspect ratio when resizing.
1288 function image_reproportion()
1290 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
1293 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
1296 $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
1297 $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
1299 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
1301 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
1303 $this->master_dim = ($ratio < 0) ? 'width' : 'height';
1306 if (($this->width != $new_width) AND ($this->height != $new_height))
1308 if ($this->master_dim == 'height')
1310 $this->width = $new_width;
1314 $this->height = $new_height;
1319 // --------------------------------------------------------------------
1322 * Get image properties
1324 * A helper function that gets info about the file
1330 function get_image_properties($path = '', $return = FALSE)
1332 // For now we require GD but we should
1333 // find a way to determine this using IM or NetPBM
1336 $path = $this->full_src_path;
1338 if ( ! file_exists($path))
1340 $this->set_error('imglib_invalid_path');
1344 $vals = @getimagesize($path);
1346 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
1348 $mime = (isset($types[$vals['2']])) ? 'image/'.$types[$vals['2']] : 'image/jpg';
1350 if ($return == TRUE)
1352 $v['width'] = $vals['0'];
1353 $v['height'] = $vals['1'];
1354 $v['image_type'] = $vals['2'];
1355 $v['size_str'] = $vals['3'];
1356 $v['mime_type'] = $mime;
1361 $this->orig_width = $vals['0'];
1362 $this->orig_height = $vals['1'];
1363 $this->image_type = $vals['2'];
1364 $this->size_str = $vals['3'];
1365 $this->mime_type = $mime;
1370 // --------------------------------------------------------------------
1375 * This function takes a known width x height and
1376 * recalculates it to a new size. Only one
1377 * new variable needs to be known
1380 * 'width' => $width,
1381 * 'height' => $height,
1382 * 'new_width' => 40,
1383 * 'new_height' => ''
1390 function size_calculator($vals)
1392 if ( ! is_array($vals))
1397 $allowed = array('new_width', 'new_height', 'width', 'height');
1399 foreach ($allowed as $item)
1401 if ( ! isset($vals[$item]) OR $vals[$item] == '')
1405 if ($vals['width'] == 0 OR $vals['height'] == 0)
1410 if ($vals['new_width'] == 0)
1412 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1414 elseif ($vals['new_height'] == 0)
1416 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1422 // --------------------------------------------------------------------
1425 * Explode source_image
1427 * This is a helper function that extracts the extension
1428 * from the source_image. This function lets us deal with
1429 * source_images with multiple periods, like: my.cool.jpg
1430 * It returns an associative array with two elements:
1431 * $array['ext'] = '.jpg';
1432 * $array['name'] = 'my.cool';
1438 function explode_name($source_image)
1440 $ext = strrchr($source_image, '.');
1441 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
1443 return array('ext' => $ext, 'name' => $name);
1446 // --------------------------------------------------------------------
1454 function gd_loaded()
1456 if ( ! extension_loaded('gd'))
1467 // --------------------------------------------------------------------
1475 function gd_version()
1477 if (function_exists('gd_info'))
1479 $gd_version = @gd_info();
1480 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
1488 // --------------------------------------------------------------------
1497 function set_error($msg)
1499 $CI =& get_instance();
1500 $CI->lang->load('imglib');
1504 foreach ($msg as $val)
1507 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1508 $this->error_msg[] = $msg;
1509 log_message('error', $msg);
1514 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1515 $this->error_msg[] = $msg;
1516 log_message('error', $msg);
1520 // --------------------------------------------------------------------
1523 * Show error messages
1529 function display_errors($open = '<p>', $close = '</p>')
1532 foreach ($this->error_msg as $val)
1534 $str .= $open.$val.$close;
1541 // END Image_lib Class
1543 /* End of file Image_lib.php */
1544 /* Location: ./system/libraries/Image_lib.php */