]>
jfr.im git - z_archive/KronOS.git/blob - video/system/libraries/Image_lib.php
1 <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, 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;
85 public function __construct($props = array())
87 if (count($props) > 0)
89 $this->initialize($props);
92 log_message('debug', "Image Lib Class Initialized");
95 // --------------------------------------------------------------------
98 * Initialize image properties
100 * Resets values in case this class is used in a loop
107 $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', 'width', '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');
109 foreach ($props as $val)
114 // special consideration for master_dim
115 $this->master_dim
= 'auto';
118 // --------------------------------------------------------------------
121 * initialize image preferences
127 function initialize($props = array())
130 * Convert array elements into class variables
132 if (count($props) > 0)
134 foreach ($props as $key => $val)
141 * Is there a source image?
143 * If not, there's no reason to continue
146 if ($this->source_image
== '')
148 $this->set_error('imglib_source_image_required');
153 * Is getimagesize() Available?
155 * We use it to determine the image properties (width/height).
156 * Note: We need to figure out how to determine image
157 * properties using ImageMagick and NetPBM
160 if ( ! function_exists('getimagesize'))
162 $this->set_error('imglib_gd_required_for_props');
166 $this->image_library
= strtolower($this->image_library
);
169 * Set the full server path
171 * The source image may or may not contain a path.
172 * Either way, we'll try use realpath to generate the
173 * full server path in order to more reliably read it.
176 if (function_exists('realpath') AND @realpath($this->source_image
) !== FALSE)
178 $full_source_path = str_replace("\\", "/", realpath($this->source_image
));
182 $full_source_path = $this->source_image
;
185 $x = explode('/', $full_source_path);
186 $this->source_image
= end($x);
187 $this->source_folder
= str_replace($this->source_image
, '', $full_source_path);
189 // Set the Image Properties
190 if ( ! $this->get_image_properties($this->source_folder
.$this->source_image
))
196 * Assign the "new" image name/path
198 * If the user has set a "new_image" name it means
199 * we are making a copy of the source image. If not
200 * it means we are altering the original. We'll
201 * set the destination filename and path accordingly.
204 if ($this->new_image
== '')
206 $this->dest_image
= $this->source_image
;
207 $this->dest_folder
= $this->source_folder
;
211 if (strpos($this->new_image
, '/') === FALSE AND strpos($this->new_image
, '\\') === FALSE)
213 $this->dest_folder
= $this->source_folder
;
214 $this->dest_image
= $this->new_image
;
218 if (function_exists('realpath') AND @realpath($this->new_image
) !== FALSE)
220 $full_dest_path = str_replace("\\", "/", realpath($this->new_image
));
224 $full_dest_path = $this->new_image
;
227 // Is there a file name?
228 if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
230 $this->dest_folder
= $full_dest_path.'/';
231 $this->dest_image
= $this->source_image
;
235 $x = explode('/', $full_dest_path);
236 $this->dest_image
= end($x);
237 $this->dest_folder
= str_replace($this->dest_image
, '', $full_dest_path);
243 * Compile the finalized filenames/paths
245 * We'll create two master strings containing the
246 * full server path to the source image and the
247 * full server path to the destination image.
248 * We'll also split the destination image name
249 * so we can insert the thumbnail marker if needed.
252 if ($this->create_thumb
=== FALSE OR $this->thumb_marker
== '')
254 $this->thumb_marker
= '';
257 $xp = $this->explode_name($this->dest_image
);
259 $filename = $xp['name'];
260 $file_ext = $xp['ext'];
262 $this->full_src_path
= $this->source_folder
.$this->source_image
;
263 $this->full_dst_path
= $this->dest_folder
.$filename.$this->thumb_marker
.$file_ext;
266 * Should we maintain image proportions?
268 * When creating thumbs or copies, the target width/height
269 * might not be in correct proportion with the source
270 * image's width/height. We'll recalculate it here.
273 if ($this->maintain_ratio
=== TRUE && ($this->width
!= '' AND $this->height
!= ''))
275 $this->image_reproportion();
279 * Was a width and height specified?
281 * If the destination width/height was
282 * not submitted we will use the values
283 * from the actual file
286 if ($this->width
== '')
287 $this->width
= $this->orig_width
;
289 if ($this->height
== '')
290 $this->height
= $this->orig_height
;
293 $this->quality
= trim(str_replace("%", "", $this->quality
));
295 if ($this->quality
== '' OR $this->quality
== 0 OR ! is_numeric($this->quality
))
298 // Set the x/y coordinates
299 $this->x_axis
= ($this->x_axis
== '' OR ! is_numeric($this->x_axis
)) ? 0 : $this->x_axis
;
300 $this->y_axis
= ($this->y_axis
== '' OR ! is_numeric($this->y_axis
)) ? 0 : $this->y_axis
;
302 // Watermark-related Stuff...
303 if ($this->wm_font_color
!= '')
305 if (strlen($this->wm_font_color
) == 6)
307 $this->wm_font_color
= '#'.$this->wm_font_color
;
311 if ($this->wm_shadow_color
!= '')
313 if (strlen($this->wm_shadow_color
) == 6)
315 $this->wm_shadow_color
= '#'.$this->wm_shadow_color
;
319 if ($this->wm_overlay_path
!= '')
321 $this->wm_overlay_path
= str_replace("\\", "/", realpath($this->wm_overlay_path
));
324 if ($this->wm_shadow_color
!= '')
326 $this->wm_use_drop_shadow
= TRUE;
329 if ($this->wm_font_path
!= '')
331 $this->wm_use_truetype
= TRUE;
337 // --------------------------------------------------------------------
342 * This is a wrapper function that chooses the proper
343 * resize function based on the protocol specified
350 $protocol = 'image_process_'.$this->image_library
;
352 if (preg_match('/gd2$/i', $protocol))
354 $protocol = 'image_process_gd';
357 return $this->$protocol('resize');
360 // --------------------------------------------------------------------
365 * This is a wrapper function that chooses the proper
366 * cropping function based on the protocol specified
373 $protocol = 'image_process_'.$this->image_library
;
375 if (preg_match('/gd2$/i', $protocol))
377 $protocol = 'image_process_gd';
380 return $this->$protocol('crop');
383 // --------------------------------------------------------------------
388 * This is a wrapper function that chooses the proper
389 * rotation function based on the protocol specified
396 // Allowed rotation values
397 $degs = array(90, 180, 270, 'vrt', 'hor');
399 if ($this->rotation_angle
== '' OR ! in_array($this->rotation_angle
, $degs))
401 $this->set_error('imglib_rotation_angle_required');
405 // Reassign the width and height
406 if ($this->rotation_angle
== 90 OR $this->rotation_angle
== 270)
408 $this->width
= $this->orig_height
;
409 $this->height
= $this->orig_width
;
413 $this->width
= $this->orig_width
;
414 $this->height
= $this->orig_height
;
418 // Choose resizing function
419 if ($this->image_library
== 'imagemagick' OR $this->image_library
== 'netpbm')
421 $protocol = 'image_process_'.$this->image_library
;
423 return $this->$protocol('rotate');
426 if ($this->rotation_angle
== 'hor' OR $this->rotation_angle
== 'vrt')
428 return $this->image_mirror_gd();
432 return $this->image_rotate_gd();
436 // --------------------------------------------------------------------
439 * Image Process Using GD/GD2
441 * This function will resize or crop
447 function image_process_gd($action = 'resize')
449 $v2_override = FALSE;
451 // If the target width/height match the source, AND if the new file name is not equal to the old file name
452 // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
453 if ($this->dynamic_output
=== FALSE)
455 if ($this->orig_width
== $this->width
AND $this->orig_height
== $this->height
)
457 if ($this->source_image
!= $this->new_image
)
459 if (@copy($this->full_src_path
, $this->full_dst_path
))
461 @chmod($this->full_dst_path
, FILE_WRITE_MODE
);
469 // Let's set up our values based on the action
470 if ($action == 'crop')
472 // Reassign the source width/height if cropping
473 $this->orig_width
= $this->width
;
474 $this->orig_height
= $this->height
;
476 // GD 2.0 has a cropping bug so we'll test for it
477 if ($this->gd_version() !== FALSE)
479 $gd_version = str_replace('0', '', $this->gd_version());
480 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
485 // If resizing the x/y axis must be zero
490 // Create the image handle
491 if ( ! ($src_img = $this->image_create_gd()))
498 // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
499 // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
500 // below should that ever prove inaccurate.
502 // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
503 if ($this->image_library
== 'gd2' AND function_exists('imagecreatetruecolor'))
505 $create = 'imagecreatetruecolor';
506 $copy = 'imagecopyresampled';
510 $create = 'imagecreate';
511 $copy = 'imagecopyresized';
514 $dst_img = $create($this->width
, $this->height
);
516 if ($this->image_type
== 3) // png we can actually preserve transparency
518 imagealphablending($dst_img, FALSE);
519 imagesavealpha($dst_img, TRUE);
522 $copy($dst_img, $src_img, 0, 0, $this->x_axis
, $this->y_axis
, $this->width
, $this->height
, $this->orig_width
, $this->orig_height
);
525 if ($this->dynamic_output
== TRUE)
527 $this->image_display_gd($dst_img);
532 if ( ! $this->image_save_gd($dst_img))
538 // Kill the file handles
539 imagedestroy($dst_img);
540 imagedestroy($src_img);
542 // Set the file to 777
543 @chmod($this->full_dst_path
, FILE_WRITE_MODE
);
548 // --------------------------------------------------------------------
551 * Image Process Using ImageMagick
553 * This function will resize, crop or rotate
559 function image_process_imagemagick($action = 'resize')
561 // Do we have a vaild library path?
562 if ($this->library_path
== '')
564 $this->set_error('imglib_libpath_invalid');
568 if ( ! preg_match("/convert$/i", $this->library_path
))
570 $this->library_path
= rtrim($this->library_path
, '/').'/';
572 $this->library_path
.= 'convert';
575 // Execute the command
576 $cmd = $this->library_path
." -quality ".$this->quality
;
578 if ($action == 'crop')
580 $cmd .= " -crop ".$this->width
."x".$this->height
."+".$this->x_axis
."+".$this->y_axis
." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
582 elseif ($action == 'rotate')
584 switch ($this->rotation_angle
)
586 case 'hor' : $angle = '-flop';
588 case 'vrt' : $angle = '-flip';
590 default : $angle = '-rotate '.$this->rotation_angle
;
594 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
598 $cmd .= " -resize ".$this->width
."x".$this->height
." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
603 @exec($cmd, $output, $retval);
608 $this->set_error('imglib_image_process_failed');
612 // Set the file to 777
613 @chmod($this->full_dst_path
, FILE_WRITE_MODE
);
618 // --------------------------------------------------------------------
621 * Image Process Using NetPBM
623 * This function will resize, crop or rotate
629 function image_process_netpbm($action = 'resize')
631 if ($this->library_path
== '')
633 $this->set_error('imglib_libpath_invalid');
637 // Build the resizing command
638 switch ($this->image_type
)
641 $cmd_in = 'giftopnm';
642 $cmd_out = 'ppmtogif';
645 $cmd_in = 'jpegtopnm';
646 $cmd_out = 'ppmtojpeg';
649 $cmd_in = 'pngtopnm';
650 $cmd_out = 'ppmtopng';
654 if ($action == 'crop')
656 $cmd_inner = 'pnmcut -left '.$this->x_axis
.' -top '.$this->y_axis
.' -width '.$this->width
.' -height '.$this->height
;
658 elseif ($action == 'rotate')
660 switch ($this->rotation_angle
)
662 case 90 : $angle = 'r270';
664 case 180 : $angle = 'r180';
666 case 270 : $angle = 'r90';
668 case 'vrt' : $angle = 'tb';
670 case 'hor' : $angle = 'lr';
674 $cmd_inner = 'pnmflip -'.$angle.' ';
678 $cmd_inner = 'pnmscale -xysize '.$this->width
.' '.$this->height
;
681 $cmd = $this->library_path
.$cmd_in.' '.$this->full_src_path
.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder
.'netpbm.tmp';
685 @exec($cmd, $output, $retval);
690 $this->set_error('imglib_image_process_failed');
694 // With NetPBM we have to create a temporary image.
695 // If you try manipulating the original it fails so
696 // we have to rename the temp file.
697 copy ($this->dest_folder
.'netpbm.tmp', $this->full_dst_path
);
698 unlink ($this->dest_folder
.'netpbm.tmp');
699 @chmod($this->full_dst_path
, FILE_WRITE_MODE
);
704 // --------------------------------------------------------------------
707 * Image Rotate Using GD
712 function image_rotate_gd()
714 // Create the image handle
715 if ( ! ($src_img = $this->image_create_gd()))
720 // Set the background color
721 // This won't work with transparent PNG files so we are
722 // going to have to figure out how to determine the color
723 // of the alpha channel in a future release.
725 $white = imagecolorallocate($src_img, 255, 255, 255);
728 $dst_img = imagerotate($src_img, $this->rotation_angle
, $white);
731 if ($this->dynamic_output
== TRUE)
733 $this->image_display_gd($dst_img);
738 if ( ! $this->image_save_gd($dst_img))
744 // Kill the file handles
745 imagedestroy($dst_img);
746 imagedestroy($src_img);
748 // Set the file to 777
750 @chmod($this->full_dst_path
, FILE_WRITE_MODE
);
755 // --------------------------------------------------------------------
758 * Create Mirror Image using GD
760 * This function will flip horizontal or vertical
765 function image_mirror_gd()
767 if ( ! $src_img = $this->image_create_gd())
772 $width = $this->orig_width
;
773 $height = $this->orig_height
;
775 if ($this->rotation_angle
== 'hor')
777 for ($i = 0; $i < $height; $i++
)
782 while ($left < $right)
784 $cl = imagecolorat($src_img, $left, $i);
785 $cr = imagecolorat($src_img, $right, $i);
787 imagesetpixel($src_img, $left, $i, $cr);
788 imagesetpixel($src_img, $right, $i, $cl);
797 for ($i = 0; $i < $width; $i++
)
804 $ct = imagecolorat($src_img, $i, $top);
805 $cb = imagecolorat($src_img, $i, $bot);
807 imagesetpixel($src_img, $i, $top, $cb);
808 imagesetpixel($src_img, $i, $bot, $ct);
817 if ($this->dynamic_output
== TRUE)
819 $this->image_display_gd($src_img);
824 if ( ! $this->image_save_gd($src_img))
830 // Kill the file handles
831 imagedestroy($src_img);
833 // Set the file to 777
834 @chmod($this->full_dst_path
, FILE_WRITE_MODE
);
839 // --------------------------------------------------------------------
844 * This is a wrapper function that chooses the type
845 * of watermarking based on the specified preference.
853 if ($this->wm_type
== 'overlay')
855 return $this->overlay_watermark();
859 return $this->text_watermark();
863 // --------------------------------------------------------------------
866 * Watermark - Graphic Version
871 function overlay_watermark()
873 if ( ! function_exists('imagecolortransparent'))
875 $this->set_error('imglib_gd_required');
879 // Fetch source image properties
880 $this->get_image_properties();
882 // Fetch watermark image properties
883 $props = $this->get_image_properties($this->wm_overlay_path
, TRUE);
884 $wm_img_type = $props['image_type'];
885 $wm_width = $props['width'];
886 $wm_height = $props['height'];
888 // Create two image resources
889 $wm_img = $this->image_create_gd($this->wm_overlay_path
, $wm_img_type);
890 $src_img = $this->image_create_gd($this->full_src_path
);
892 // Reverse the offset if necessary
893 // When the image is positioned at the bottom
894 // we don't want the vertical offset to push it
895 // further down. We want the reverse, so we'll
896 // invert the offset. Same with the horizontal
897 // offset when the image is at the right
899 $this->wm_vrt_alignment
= strtoupper(substr($this->wm_vrt_alignment
, 0, 1));
900 $this->wm_hor_alignment
= strtoupper(substr($this->wm_hor_alignment
, 0, 1));
902 if ($this->wm_vrt_alignment
== 'B')
903 $this->wm_vrt_offset
= $this->wm_vrt_offset
* -1;
905 if ($this->wm_hor_alignment
== 'R')
906 $this->wm_hor_offset
= $this->wm_hor_offset
* -1;
908 // Set the base x and y axis values
909 $x_axis = $this->wm_hor_offset +
$this->wm_padding
;
910 $y_axis = $this->wm_vrt_offset +
$this->wm_padding
;
912 // Set the vertical position
913 switch ($this->wm_vrt_alignment
)
917 case 'M': $y_axis +
= ($this->orig_height
/ 2) - ($wm_height / 2);
919 case 'B': $y_axis +
= $this->orig_height
- $wm_height;
923 // Set the horizontal position
924 switch ($this->wm_hor_alignment
)
928 case 'C': $x_axis +
= ($this->orig_width
/ 2) - ($wm_width / 2);
930 case 'R': $x_axis +
= $this->orig_width
- $wm_width;
934 // Build the finalized image
935 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
937 @imagealphablending($src_img, TRUE);
940 // Set RGB values for text and shadow
941 $rgba = imagecolorat($wm_img, $this->wm_x_transp
, $this->wm_y_transp
);
942 $alpha = ($rgba & 0x7F000000) >> 24;
944 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
947 // copy the image directly, the image's alpha transparency being the sole determinant of blending
948 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
952 // set our RGB value from above to be transparent and merge the images with the specified opacity
953 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp
, $this->wm_y_transp
));
954 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity
);
958 if ($this->dynamic_output
== TRUE)
960 $this->image_display_gd($src_img);
964 if ( ! $this->image_save_gd($src_img))
970 imagedestroy($src_img);
971 imagedestroy($wm_img);
976 // --------------------------------------------------------------------
979 * Watermark - Text Version
984 function text_watermark()
986 if ( ! ($src_img = $this->image_create_gd()))
991 if ($this->wm_use_truetype
== TRUE AND ! file_exists($this->wm_font_path
))
993 $this->set_error('imglib_missing_font');
997 // Fetch source image properties
998 $this->get_image_properties();
1000 // Set RGB values for text and shadow
1001 $this->wm_font_color
= str_replace('#', '', $this->wm_font_color
);
1002 $this->wm_shadow_color
= str_replace('#', '', $this->wm_shadow_color
);
1004 $R1 = hexdec(substr($this->wm_font_color
, 0, 2));
1005 $G1 = hexdec(substr($this->wm_font_color
, 2, 2));
1006 $B1 = hexdec(substr($this->wm_font_color
, 4, 2));
1008 $R2 = hexdec(substr($this->wm_shadow_color
, 0, 2));
1009 $G2 = hexdec(substr($this->wm_shadow_color
, 2, 2));
1010 $B2 = hexdec(substr($this->wm_shadow_color
, 4, 2));
1012 $txt_color = imagecolorclosest($src_img, $R1, $G1, $B1);
1013 $drp_color = imagecolorclosest($src_img, $R2, $G2, $B2);
1015 // Reverse the vertical offset
1016 // When the image is positioned at the bottom
1017 // we don't want the vertical offset to push it
1018 // further down. We want the reverse, so we'll
1019 // invert the offset. Note: The horizontal
1020 // offset flips itself automatically
1022 if ($this->wm_vrt_alignment
== 'B')
1023 $this->wm_vrt_offset
= $this->wm_vrt_offset
* -1;
1025 if ($this->wm_hor_alignment
== 'R')
1026 $this->wm_hor_offset
= $this->wm_hor_offset
* -1;
1028 // Set font width and height
1029 // These are calculated differently depending on
1030 // whether we are using the true type font or not
1031 if ($this->wm_use_truetype
== TRUE)
1033 if ($this->wm_font_size
== '')
1034 $this->wm_font_size
= '17';
1036 $fontwidth = $this->wm_font_size
-($this->wm_font_size
/4);
1037 $fontheight = $this->wm_font_size
;
1038 $this->wm_vrt_offset +
= $this->wm_font_size
;
1042 $fontwidth = imagefontwidth($this->wm_font_size
);
1043 $fontheight = imagefontheight($this->wm_font_size
);
1046 // Set base X and Y axis values
1047 $x_axis = $this->wm_hor_offset +
$this->wm_padding
;
1048 $y_axis = $this->wm_vrt_offset +
$this->wm_padding
;
1050 // Set verticle alignment
1051 if ($this->wm_use_drop_shadow
== FALSE)
1052 $this->wm_shadow_distance
= 0;
1054 $this->wm_vrt_alignment
= strtoupper(substr($this->wm_vrt_alignment
, 0, 1));
1055 $this->wm_hor_alignment
= strtoupper(substr($this->wm_hor_alignment
, 0, 1));
1057 switch ($this->wm_vrt_alignment
)
1061 case "M": $y_axis +
= ($this->orig_height
/2)+
($fontheight/2);
1063 case "B": $y_axis +
= ($this->orig_height
- $fontheight - $this->wm_shadow_distance
- ($fontheight/2));
1067 $x_shad = $x_axis +
$this->wm_shadow_distance
;
1068 $y_shad = $y_axis +
$this->wm_shadow_distance
;
1070 // Set horizontal alignment
1071 switch ($this->wm_hor_alignment
)
1076 if ($this->wm_use_drop_shadow
)
1077 $x_shad +
= ($this->orig_width
- $fontwidth*strlen($this->wm_text
));
1078 $x_axis +
= ($this->orig_width
- $fontwidth*strlen($this->wm_text
));
1081 if ($this->wm_use_drop_shadow
)
1082 $x_shad +
= floor(($this->orig_width
- $fontwidth*strlen($this->wm_text
))/2);
1083 $x_axis +
= floor(($this->orig_width
-$fontwidth*strlen($this->wm_text
))/2);
1087 // Add the text to the source image
1088 if ($this->wm_use_truetype
)
1090 if ($this->wm_use_drop_shadow
)
1091 imagettftext($src_img, $this->wm_font_size
, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path
, $this->wm_text
);
1092 imagettftext($src_img, $this->wm_font_size
, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path
, $this->wm_text
);
1096 if ($this->wm_use_drop_shadow
)
1097 imagestring($src_img, $this->wm_font_size
, $x_shad, $y_shad, $this->wm_text
, $drp_color);
1098 imagestring($src_img, $this->wm_font_size
, $x_axis, $y_axis, $this->wm_text
, $txt_color);
1101 // Output the final image
1102 if ($this->dynamic_output
== TRUE)
1104 $this->image_display_gd($src_img);
1108 $this->image_save_gd($src_img);
1111 imagedestroy($src_img);
1116 // --------------------------------------------------------------------
1121 * This simply creates an image resource handle
1122 * based on the type of image being processed
1128 function image_create_gd($path = '', $image_type = '')
1131 $path = $this->full_src_path
;
1133 if ($image_type == '')
1134 $image_type = $this->image_type
;
1137 switch ($image_type)
1140 if ( ! function_exists('imagecreatefromgif'))
1142 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1146 return imagecreatefromgif($path);
1149 if ( ! function_exists('imagecreatefromjpeg'))
1151 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1155 return imagecreatefromjpeg($path);
1158 if ( ! function_exists('imagecreatefrompng'))
1160 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1164 return imagecreatefrompng($path);
1169 $this->set_error(array('imglib_unsupported_imagecreate'));
1173 // --------------------------------------------------------------------
1176 * Write image file to disk - GD
1178 * Takes an image resource as input and writes the file
1179 * to the specified destination
1185 function image_save_gd($resource)
1187 switch ($this->image_type
)
1190 if ( ! function_exists('imagegif'))
1192 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1196 if ( ! @imagegif($resource, $this->full_dst_path
))
1198 $this->set_error('imglib_save_failed');
1203 if ( ! function_exists('imagejpeg'))
1205 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1209 if ( ! @imagejpeg($resource, $this->full_dst_path
, $this->quality
))
1211 $this->set_error('imglib_save_failed');
1216 if ( ! function_exists('imagepng'))
1218 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1222 if ( ! @imagepng($resource, $this->full_dst_path
))
1224 $this->set_error('imglib_save_failed');
1229 $this->set_error(array('imglib_unsupported_imagecreate'));
1237 // --------------------------------------------------------------------
1240 * Dynamically outputs an image
1246 function image_display_gd($resource)
1248 header("Content-Disposition: filename={$this->source_image};");
1249 header("Content-Type: {$this->mime_type}");
1250 header('Content-Transfer-Encoding: binary');
1251 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1253 switch ($this->image_type
)
1255 case 1 : imagegif($resource);
1257 case 2 : imagejpeg($resource, '', $this->quality
);
1259 case 3 : imagepng($resource);
1261 default : echo 'Unable to display the image';
1266 // --------------------------------------------------------------------
1269 * Re-proportion Image Width/Height
1271 * When creating thumbs, the desired width/height
1272 * can end up warping the image due to an incorrect
1273 * ratio between the full-sized image and the thumb.
1275 * This function lets us re-proportion the width/height
1276 * if users choose to maintain the aspect ratio when resizing.
1281 function image_reproportion()
1283 if ( ! is_numeric($this->width
) OR ! is_numeric($this->height
) OR $this->width
== 0 OR $this->height
== 0)
1286 if ( ! is_numeric($this->orig_width
) OR ! is_numeric($this->orig_height
) OR $this->orig_width
== 0 OR $this->orig_height
== 0)
1289 $new_width = ceil($this->orig_width
*$this->height
/$this->orig_height
);
1290 $new_height = ceil($this->width
*$this->orig_height
/$this->orig_width
);
1292 $ratio = (($this->orig_height
/$this->orig_width
) - ($this->height
/$this->width
));
1294 if ($this->master_dim
!= 'width' AND $this->master_dim
!= 'height')
1296 $this->master_dim
= ($ratio < 0) ? 'width' : 'height';
1299 if (($this->width
!= $new_width) AND ($this->height
!= $new_height))
1301 if ($this->master_dim
== 'height')
1303 $this->width
= $new_width;
1307 $this->height
= $new_height;
1312 // --------------------------------------------------------------------
1315 * Get image properties
1317 * A helper function that gets info about the file
1323 function get_image_properties($path = '', $return = FALSE)
1325 // For now we require GD but we should
1326 // find a way to determine this using IM or NetPBM
1329 $path = $this->full_src_path
;
1331 if ( ! file_exists($path))
1333 $this->set_error('imglib_invalid_path');
1337 $vals = @getimagesize($path);
1339 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
1341 $mime = (isset($types[$vals['2']])) ? 'image/'.$types[$vals['2']] : 'image/jpg';
1343 if ($return == TRUE)
1345 $v['width'] = $vals['0'];
1346 $v['height'] = $vals['1'];
1347 $v['image_type'] = $vals['2'];
1348 $v['size_str'] = $vals['3'];
1349 $v['mime_type'] = $mime;
1354 $this->orig_width
= $vals['0'];
1355 $this->orig_height
= $vals['1'];
1356 $this->image_type
= $vals['2'];
1357 $this->size_str
= $vals['3'];
1358 $this->mime_type
= $mime;
1363 // --------------------------------------------------------------------
1368 * This function takes a known width x height and
1369 * recalculates it to a new size. Only one
1370 * new variable needs to be known
1373 * 'width' => $width,
1374 * 'height' => $height,
1375 * 'new_width' => 40,
1376 * 'new_height' => ''
1383 function size_calculator($vals)
1385 if ( ! is_array($vals))
1390 $allowed = array('new_width', 'new_height', 'width', 'height');
1392 foreach ($allowed as $item)
1394 if ( ! isset($vals[$item]) OR $vals[$item] == '')
1398 if ($vals['width'] == 0 OR $vals['height'] == 0)
1403 if ($vals['new_width'] == 0)
1405 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1407 elseif ($vals['new_height'] == 0)
1409 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1415 // --------------------------------------------------------------------
1418 * Explode source_image
1420 * This is a helper function that extracts the extension
1421 * from the source_image. This function lets us deal with
1422 * source_images with multiple periods, like: my.cool.jpg
1423 * It returns an associative array with two elements:
1424 * $array['ext'] = '.jpg';
1425 * $array['name'] = 'my.cool';
1431 function explode_name($source_image)
1433 $ext = strrchr($source_image, '.');
1434 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
1436 return array('ext' => $ext, 'name' => $name);
1439 // --------------------------------------------------------------------
1447 function gd_loaded()
1449 if ( ! extension_loaded('gd'))
1460 // --------------------------------------------------------------------
1468 function gd_version()
1470 if (function_exists('gd_info'))
1472 $gd_version = @gd_info();
1473 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
1481 // --------------------------------------------------------------------
1490 function set_error($msg)
1492 $CI =& get_instance();
1493 $CI->lang
->load('imglib');
1497 foreach ($msg as $val)
1500 $msg = ($CI->lang
->line($val) == FALSE) ? $val : $CI->lang
->line($val);
1501 $this->error_msg
[] = $msg;
1502 log_message('error', $msg);
1507 $msg = ($CI->lang
->line($msg) == FALSE) ? $msg : $CI->lang
->line($msg);
1508 $this->error_msg
[] = $msg;
1509 log_message('error', $msg);
1513 // --------------------------------------------------------------------
1516 * Show error messages
1522 function display_errors($open = '<p>', $close = '</p>')
1525 foreach ($this->error_msg
as $val)
1527 $str .= $open.$val.$close;
1534 // END Image_lib Class
1536 /* End of file Image_lib.php */
1537 /* Location: ./system/libraries/Image_lib.php */