Image_lib.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Image Manipulation class
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Libraries
00023  * @category    Image_lib
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/libraries/image_lib.html
00026  */
00027 class CI_Image_lib {
00028         
00029         var $image_library              = 'gd2';        // Can be:  imagemagick, netpbm, gd, gd2
00030         var $library_path               = '';
00031         var $dynamic_output             = FALSE;        // Whether to send to browser or write to disk
00032         var $source_image               = '';   
00033         var $new_image                  = '';
00034         var $width                              = '';
00035         var $height                             = '';
00036         var $quality                    = '90';
00037         var $create_thumb               = FALSE;
00038         var $thumb_marker               = '_thumb';
00039         var $maintain_ratio             = TRUE;         // Whether to maintain aspect ratio when resizing or use hard values
00040         var $master_dim                 = 'auto';       // auto, height, or width.  Determines what to use as the master dimension
00041         var $rotation_angle             = '';
00042         var $x_axis                             = '';
00043         var     $y_axis                         = '';
00044         
00045         // Watermark Vars
00046         var $wm_text                    = '';                   // Watermark text if graphic is not used
00047         var $wm_type                    = 'text';               // Type of watermarking.  Options:  text/overlay
00048         var $wm_x_transp                = 4;
00049         var $wm_y_transp                = 4;
00050         var $wm_overlay_path    = '';                   // Watermark image path
00051         var $wm_font_path               = '';                   // TT font
00052         var $wm_font_size               = 17;                   // Font size (different versions of GD will either use points or pixels)
00053         var $wm_vrt_alignment   = 'B';                  // Vertical alignment:   T M B
00054         var $wm_hor_alignment   = 'C';                  // Horizontal alignment: L R C
00055         var $wm_padding                 = 0;                    // Padding around text
00056         var $wm_hor_offset              = 0;                    // Lets you push text to the right
00057         var $wm_vrt_offset              = 0;                     // Lets you push  text down
00058         var $wm_font_color              = '#ffffff';    // Text color
00059         var $wm_shadow_color    = '';                   // Dropshadow color
00060         var $wm_shadow_distance = 2;                    // Dropshadow distance
00061         var $wm_opacity                 = 50;                   // Image opacity: 1 - 100  Only works with image
00062         
00063         // Private Vars
00064         var $source_folder              = '';
00065         var $dest_folder                = '';
00066         var $mime_type                  = '';
00067         var $orig_width                 = '';
00068         var $orig_height                = '';
00069         var $image_type                 = '';
00070         var $size_str                   = '';
00071         var $full_src_path              = '';
00072         var $full_dst_path              = '';
00073         var $create_fnc                 = 'imagecreatetruecolor';
00074         var $copy_fnc                   = 'imagecopyresampled';
00075         var $error_msg                  = array();
00076         var $wm_use_drop_shadow = FALSE;
00077         var $wm_use_truetype    = FALSE;                
00078         
00079         /**
00080          * Constructor
00081          *
00082          * @access      public
00083          * @param       string
00084          * @return      void
00085          */     
00086         function CI_Image_lib($props = array())
00087         {
00088                 if (count($props) > 0)
00089                 {
00090                         $this->initialize($props);
00091                 }
00092                 
00093                 log_message('debug', "Image Lib Class Initialized");
00094         }
00095         
00096         // --------------------------------------------------------------------
00097         
00098         /**
00099          * Initialize image properties
00100          *
00101          * Resets values in case this class is used in a loop
00102          *
00103          * @access      public
00104          * @return      void
00105          */     
00106         function clear()
00107         {
00108                 $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');
00109         
00110                 foreach ($props as $val)
00111                 {
00112                         $this->$val = '';
00113                 }
00114 
00115                 // special consideration for master_dim
00116                 $this->master_dim = 'auto';
00117         }
00118         
00119         // --------------------------------------------------------------------
00120         
00121         /**
00122          * initialize image preferences
00123          *
00124          * @access      public
00125          * @param       array
00126          * @return      void
00127          */     
00128         function initialize($props = array())
00129         {
00130                 /*
00131                  * Convert array elements into class variables
00132                  */
00133                 if (count($props) > 0)
00134                 {
00135                         foreach ($props as $key => $val)
00136                         {
00137                                 $this->$key = $val;
00138                         }
00139                 }
00140 
00141                 /*
00142                  * Is there a source image?
00143                  *
00144                  * If not, there's no reason to continue
00145                  *
00146                  */
00147                 if ($this->source_image == '')
00148                 {
00149                         $this->set_error('imglib_source_image_required');
00150                         return FALSE;           
00151                 }
00152                 
00153                 /*
00154                  * Is getimagesize() Available?
00155                  *
00156                  * We use it to determine the image properties (width/height).
00157                  * Note:  We need to figure out how to determine image
00158                  * properties using ImageMagick and NetPBM
00159                  *
00160                  */             
00161                 if ( ! function_exists('getimagesize'))
00162                 {
00163                         $this->set_error('imglib_gd_required_for_props');
00164                         return FALSE;           
00165                 }
00166                 
00167                 $this->image_library = strtolower($this->image_library);
00168                 
00169                 /*
00170                  * Set the full server path
00171                  *
00172                  * The source image may or may not contain a path.
00173                  * Either way, we'll try use realpath to generate the
00174                  * full server path in order to more reliably read it.
00175                  *
00176                  */     
00177                 if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE)
00178                 {
00179                         $full_source_path = str_replace("\\", "/", realpath($this->source_image));
00180                 }
00181                 else
00182                 {
00183                         $full_source_path = $this->source_image;
00184                 }
00185                 
00186                 $x = explode('/', $full_source_path);
00187                 $this->source_image = end($x);
00188                 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
00189                                                                 
00190                 // Set the Image Properties
00191                 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
00192                 {
00193                         return FALSE;           
00194                 }                               
00195 
00196                 /*
00197                  * Assign the "new" image name/path
00198                  *
00199                  * If the user has set a "new_image" name it means
00200                  * we are making a copy of the source image. If not
00201                  * it means we are altering the original.  We'll
00202                  * set the destination filename and path accordingly.
00203                  *
00204                  */                     
00205                 if ($this->new_image == '')
00206                 {
00207                         $this->dest_image = $this->source_image;
00208                         $this->dest_folder = $this->source_folder;
00209                 }
00210                 else
00211                 {
00212                         if (strpos($this->new_image, '/') === FALSE)
00213                         {
00214                                 $this->dest_folder = $this->source_folder;
00215                                 $this->dest_image = $this->new_image;
00216                         }
00217                         else
00218                         {
00219                                 if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE)
00220                                 {
00221                                         $full_dest_path = str_replace("\\", "/", realpath($this->new_image));
00222                                 }
00223                                 else
00224                                 {
00225                                         $full_dest_path = $this->new_image;
00226                                 }
00227                                 
00228                                 // Is there a file name?
00229                                 if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
00230                                 {
00231                                         $this->dest_folder = $full_dest_path.'/';
00232                                         $this->dest_image = $this->source_image;
00233                                 }
00234                                 else
00235                                 {
00236                                         $x = explode('/', $full_dest_path);
00237                                         $this->dest_image = end($x);
00238                                         $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
00239                                 }
00240                         }
00241                 }
00242 
00243                 /*
00244                  * Compile the finalized filenames/paths
00245                  *
00246                  * We'll create two master strings containing the
00247                  * full server path to the source image and the
00248                  * full server path to the destination image.
00249                  * We'll also split the destination image name
00250                  * so we can insert the thumbnail marker if needed.
00251                  *
00252                  */     
00253                 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
00254                 {
00255                         $this->thumb_marker = '';
00256                 }
00257 
00258                 $xp     = $this->explode_name($this->dest_image);
00259         
00260                 $filename = $xp['name'];
00261                 $file_ext = $xp['ext'];
00262                                 
00263                 $this->full_src_path = $this->source_folder.$this->source_image;                
00264                 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
00265 
00266                 /*
00267                  * Should we maintain image proportions?
00268                  *
00269                  * When creating thumbs or copies, the target width/height
00270                  * might not be in correct proportion with the source
00271                  * image's width/height.  We'll recalculate it here.
00272                  *
00273                  */     
00274                 if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
00275                 {
00276                         $this->image_reproportion();
00277                 }
00278 
00279                 /*
00280                  * Was a width and height specified?
00281                  *
00282                  * If the destination width/height was
00283                  * not submitted we will use the values
00284                  * from the actual file
00285                  *
00286                  */     
00287                 if ($this->width == '')
00288                         $this->width = $this->orig_width;
00289         
00290                 if ($this->height == '')
00291                         $this->height = $this->orig_height;
00292         
00293                 // Set the quality
00294                 $this->quality = trim(str_replace("%", "", $this->quality));
00295                 
00296                 if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality))
00297                         $this->quality = 90;
00298         
00299                 // Set the x/y coordinates
00300                 $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis;
00301                 $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis;
00302         
00303                 // Watermark-related Stuff...
00304                 if ($this->wm_font_color != '')
00305                 {
00306                         if (strlen($this->wm_font_color) == 6)
00307                         {
00308                                 $this->wm_font_color = '#'.$this->wm_font_color;
00309                         }
00310                 }
00311                 
00312                 if ($this->wm_shadow_color != '')
00313                 {
00314                         if (strlen($this->wm_shadow_color) == 6)
00315                         {
00316                                 $this->wm_shadow_color = '#'.$this->wm_shadow_color;
00317                         }
00318                 }
00319         
00320                 if ($this->wm_overlay_path != '')
00321                 {
00322                         $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path));
00323                 }
00324         
00325                 if ($this->wm_shadow_color != '')
00326                 {
00327                         $this->wm_use_drop_shadow = TRUE;
00328                 }
00329 
00330                 if ($this->wm_font_path != '')
00331                 {
00332                         $this->wm_use_truetype = TRUE;
00333                 }
00334 
00335                 return TRUE;
00336         }
00337         
00338         // --------------------------------------------------------------------
00339         
00340         /**
00341          * Image Resize
00342          *
00343          * This is a wrapper function that chooses the proper
00344          * resize function based on the protocol specified
00345          *
00346          * @access      public
00347          * @return      bool
00348          */     
00349         function resize()
00350         {
00351                 $protocol = 'image_process_'.$this->image_library;
00352                 
00353                 if (eregi("gd2$", $protocol))
00354                 {
00355                         $protocol = 'image_process_gd';
00356                 }
00357                 
00358                 return $this->$protocol('resize');
00359         }
00360         
00361         // --------------------------------------------------------------------
00362         
00363         /**
00364          * Image Crop
00365          *
00366          * This is a wrapper function that chooses the proper
00367          * cropping function based on the protocol specified
00368          *
00369          * @access      public
00370          * @return      bool
00371          */     
00372         function crop()
00373         {
00374                 $protocol = 'image_process_'.$this->image_library;
00375                 
00376                 if (eregi("gd2$", $protocol))
00377                 {
00378                         $protocol = 'image_process_gd';
00379                 }
00380                 
00381                 return $this->$protocol('crop');
00382         }
00383         
00384         // --------------------------------------------------------------------
00385         
00386         /**
00387          * Image Rotate
00388          *
00389          * This is a wrapper function that chooses the proper
00390          * rotation function based on the protocol specified
00391          *
00392          * @access      public
00393          * @return      bool
00394          */     
00395         function rotate()
00396         {
00397                 // Allowed rotation values              
00398                 $degs = array(90, 180, 270, 'vrt', 'hor');      
00399         
00400                 if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs, TRUE))
00401                 {
00402                         $this->set_error('imglib_rotation_angle_required');
00403                         return FALSE;           
00404                 }
00405         
00406                 // Reassign the width and height
00407                 if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
00408                 {
00409                         $this->width    = $this->orig_height;
00410                         $this->height   = $this->orig_width;
00411                 }
00412                 else
00413                 {
00414                         $this->width    = $this->orig_width;
00415                         $this->height   = $this->orig_height;
00416                 }
00417         
00418 
00419                 // Choose resizing function
00420                 if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm')
00421                 {
00422                         $protocol = 'image_process_'.$this->image_library;
00423                 
00424                         return $this->$protocol('rotate');
00425                 }
00426                 
00427                 if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
00428                 {
00429                         return $this->image_mirror_gd();
00430                 }
00431                 else
00432                 {               
00433                         return $this->image_rotate_gd();
00434                 }
00435         }
00436         
00437         // --------------------------------------------------------------------
00438         
00439         /**
00440          * Image Process Using GD/GD2
00441          *
00442          * This function will resize or crop
00443          *
00444          * @access      public
00445          * @param       string
00446          * @return      bool
00447          */             
00448         function image_process_gd($action = 'resize')
00449         {       
00450                 $v2_override = FALSE;
00451                         
00452                 if ($action == 'crop')
00453                 {
00454                         // If the target width/height match the source then it's pointless to crop, right?
00455                         // So if dynamic output isn't on, then we'll return true so the user thinks
00456                         // the process succeeded. It'll be our little secret...
00457 
00458                         if ($this->width >= $this->orig_width AND $this->height >= $this->orig_height AND $this->dynamic_output !== TRUE)
00459                         {
00460                                 return TRUE;
00461                         }
00462 
00463                         //  Reassign the source width/height if cropping
00464                         $this->orig_width  = $this->width;
00465                         $this->orig_height = $this->height;     
00466                                 
00467                         // GD 2.0 has a cropping bug so we'll test for it
00468                         if ($this->gd_version() !== FALSE)
00469                         {
00470                                 $gd_version = str_replace('0', '', $this->gd_version());                        
00471                                 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
00472                         }
00473                 }
00474                 else
00475                 {
00476                         // If the target width/height match the source, AND if
00477                         // the new file name is not equal to the old file name
00478                         // we'll simply make a copy of the original with the new name           
00479                         if (($this->orig_width == $this->width AND $this->orig_height == $this->height) AND ($this->source_image != $this->new_image))                  
00480                         {
00481                                 if ( ! @copy($this->full_src_path, $this->full_dst_path))
00482                                 {
00483                                         $this->set_error('imglib_copy_failed');
00484                                         return FALSE;
00485                                 }
00486                         
00487                                 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00488                                 return TRUE;
00489                         }
00490                         
00491                         // If resizing the x/y axis must be zero
00492                         $this->x_axis = 0;
00493                         $this->y_axis = 0;
00494                 }
00495                 
00496                 //  Create the image handle
00497                 if ( ! ($src_img = $this->image_create_gd()))
00498                 {               
00499                         return FALSE;
00500                 }
00501 
00502                 //  Create The Image
00503                 //
00504                 //  old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
00505                 //  it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
00506                 //  below should that ever prove inaccurate.
00507                 //
00508                 //  if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
00509                 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))          
00510                 {
00511                         $create = 'imagecreatetruecolor';
00512                         $copy   = 'imagecopyresampled';
00513                 }
00514                 else
00515                 {
00516                         $create = 'imagecreate';        
00517                         $copy   = 'imagecopyresized';
00518                 }
00519                         
00520                 $dst_img = $create($this->width, $this->height);
00521                 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
00522 
00523                 //  Show the image      
00524                 if ($this->dynamic_output == TRUE)
00525                 {
00526                         $this->image_display_gd($dst_img);
00527                 }
00528                 else
00529                 {
00530                         // Or save it
00531                         if ( ! $this->image_save_gd($dst_img))
00532                         {
00533                                 return FALSE;
00534                         }
00535                 }
00536 
00537                 //  Kill the file handles
00538                 imagedestroy($dst_img);
00539                 imagedestroy($src_img);
00540                 
00541                 // Set the file to 777
00542                 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00543                 
00544                 return TRUE;
00545         }
00546         
00547         // --------------------------------------------------------------------
00548         
00549         /**
00550          * Image Process Using ImageMagick
00551          *
00552          * This function will resize, crop or rotate
00553          *
00554          * @access      public
00555          * @param       string
00556          * @return      bool
00557          */             
00558         function image_process_imagemagick($action = 'resize')
00559         {
00560                 //  Do we have a vaild library path?
00561                 if ($this->library_path == '')
00562                 {
00563                         $this->set_error('imglib_libpath_invalid');
00564                         return FALSE;
00565                 }
00566                                 
00567                 if ( ! eregi("convert$", $this->library_path))
00568                 {
00569                         if ( ! eregi("/$", $this->library_path)) $this->library_path .= "/";
00570                 
00571                         $this->library_path .= 'convert';
00572                 }
00573                 
00574                 // Execute the command
00575                 $cmd = $this->library_path." -quality ".$this->quality;
00576         
00577                 if ($action == 'crop')
00578                 {
00579                         $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
00580                 }
00581                 elseif ($action == 'rotate')
00582                 {
00583                         switch ($this->rotation_angle)
00584                         {
00585                                 case 'hor'      : $angle = '-flop';
00586                                         break;
00587                                 case 'vrt'      : $angle = '-flip';
00588                                         break;
00589                                 default         : $angle = '-rotate '.$this->rotation_angle;
00590                                         break;
00591                         }                       
00592                 
00593                         $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
00594                 }
00595                 else  // Resize
00596                 {
00597                         $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
00598                 }
00599         
00600                 $retval = 1;
00601         
00602                 @exec($cmd, $output, $retval);
00603 
00604                 //      Did it work?    
00605                 if ($retval > 0)
00606                 {
00607                         $this->set_error('imglib_image_process_failed');
00608                         return FALSE;
00609                 }
00610                 
00611                 // Set the file to 777
00612                 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00613                 
00614                 return TRUE;
00615         }
00616         
00617         // --------------------------------------------------------------------
00618         
00619         /**
00620          * Image Process Using NetPBM
00621          *
00622          * This function will resize, crop or rotate
00623          *
00624          * @access      public
00625          * @param       string
00626          * @return      bool
00627          */             
00628         function image_process_netpbm($action = 'resize')
00629         {
00630                 if ($this->library_path == '')
00631                 {
00632                         $this->set_error('imglib_libpath_invalid');
00633                         return FALSE;
00634                 }
00635                         
00636                 //  Build the resizing command
00637                 switch ($this->image_type)
00638                 {
00639                         case 1 :
00640                                                 $cmd_in         = 'giftopnm';
00641                                                 $cmd_out        = 'ppmtogif';
00642                                 break;
00643                         case 2 :
00644                                                 $cmd_in         = 'jpegtopnm';
00645                                                 $cmd_out        = 'ppmtojpeg';                  
00646                                 break;
00647                         case 3 :
00648                                                 $cmd_in         = 'pngtopnm';
00649                                                 $cmd_out        = 'ppmtopng';
00650                                 break;
00651                 }
00652                 
00653                 if ($action == 'crop')
00654                 {
00655                         $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
00656                 }
00657                 elseif ($action == 'rotate')
00658                 {
00659                         switch ($this->rotation_angle)
00660                         {
00661                                 case 90         :       $angle = 'r270';
00662                                         break;
00663                                 case 180        :       $angle = 'r180';
00664                                         break;
00665                                 case 270        :       $angle = 'r90';
00666                                         break;
00667                                 case 'vrt'      :       $angle = 'tb';
00668                                         break;
00669                                 case 'hor'      :       $angle = 'lr';
00670                                         break;
00671                         }
00672                 
00673                         $cmd_inner = 'pnmflip -'.$angle.' ';
00674                 }
00675                 else // Resize
00676                 {
00677                         $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
00678                 }
00679                                                 
00680                 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
00681                 
00682                 $retval = 1;
00683                 
00684                 @exec($cmd, $output, $retval);
00685                 
00686                 //  Did it work?
00687                 if ($retval > 0)
00688                 {
00689                         $this->set_error('imglib_image_process_failed');
00690                         return FALSE;
00691                 }
00692                 
00693                 // With NetPBM we have to create a temporary image.
00694                 // If you try manipulating the original it fails so
00695                 // we have to rename the temp file.
00696                 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
00697                 unlink ($this->dest_folder.'netpbm.tmp');
00698                 @chmod($dst_image, DIR_WRITE_MODE);
00699                 
00700                 return TRUE;
00701         }
00702         
00703         // --------------------------------------------------------------------
00704         
00705         /**
00706          * Image Rotate Using GD
00707          *
00708          * @access      public
00709          * @return      bool
00710          */             
00711         function image_rotate_gd()
00712         {       
00713                 // Is Image Rotation Supported?
00714                 // this function is only supported as of PHP 4.3
00715                 if ( ! function_exists('imagerotate'))
00716                 {
00717                         $this->set_error('imglib_rotate_unsupported');
00718                         return FALSE;
00719                 }
00720                 
00721                 //  Create the image handle
00722                 if ( ! ($src_img = $this->image_create_gd()))
00723                 {               
00724                         return FALSE;
00725                 }
00726 
00727                 // Set the background color             
00728                 // This won't work with transparent PNG files so we are
00729                 // going to have to figure out how to determine the color
00730                 // of the alpha channel in a future release.
00731         
00732                 $white  = imagecolorallocate($src_img, 255, 255, 255);
00733 
00734                 //  Rotate it!
00735                 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
00736         
00737                 //  Save the Image
00738                 if ($this->dynamic_output == TRUE)
00739                 {
00740                         $this->image_display_gd($dst_img);
00741                 }
00742                 else
00743                 {
00744                         // Or save it
00745                         if ( ! $this->image_save_gd($dst_img))
00746                         {
00747                                 return FALSE;
00748                         }
00749                 }
00750 
00751                 //  Kill the file handles
00752                 imagedestroy($dst_img);
00753                 imagedestroy($src_img);
00754                 
00755                 // Set the file to 777
00756                 
00757                 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00758                 
00759                 return true;
00760         }
00761         
00762         // --------------------------------------------------------------------
00763         
00764         /**
00765          * Create Mirror Image using GD
00766          *
00767          * This function will flip horizontal or vertical
00768          *
00769          * @access      public
00770          * @return      bool
00771          */                     
00772         function image_mirror_gd()
00773         {               
00774                 if ( ! $src_img = $this->image_create_gd())
00775                 {
00776                         return FALSE;
00777                 }
00778                 
00779                 $width  = $this->orig_width;
00780                 $height = $this->orig_height;
00781         
00782                 if ($this->rotation_angle == 'hor')
00783                 {
00784                         for ($i = 0; $i < $height; $i++)
00785                         {
00786                                 $left  = 0;
00787                                 $right = $width-1;
00788         
00789                                 while ($left < $right)
00790                                 {
00791                                         $cl = imagecolorat($src_img, $left, $i);
00792                                         $cr = imagecolorat($src_img, $right, $i);
00793                                         
00794                                         imagesetpixel($src_img, $left, $i, $cr);
00795                                         imagesetpixel($src_img, $right, $i, $cl);
00796                                         
00797                                         $left++;
00798                                         $right--;
00799                                 }
00800                         }
00801                 }
00802                 else
00803                 {
00804                         for ($i = 0; $i < $width; $i++)
00805                         {
00806                                 $top = 0;
00807                                 $bot = $height-1;
00808         
00809                                 while ($top < $bot)
00810                                 {
00811                                         $ct = imagecolorat($src_img, $i, $top);
00812                                         $cb = imagecolorat($src_img, $i, $bot);
00813                                         
00814                                         imagesetpixel($src_img, $i, $top, $cb);
00815                                         imagesetpixel($src_img, $i, $bot, $ct);
00816                                         
00817                                         $top++;
00818                                         $bot--;
00819                                 }
00820                         }               
00821                 }               
00822 
00823                 //  Show the image
00824                 if ($this->dynamic_output == TRUE)
00825                 {
00826                         $this->image_display_gd($src_img);
00827                 }
00828                 else
00829                 {
00830                         // Or save it
00831                         if ( ! $this->image_save_gd($src_img))
00832                         {
00833                                 return FALSE;
00834                         }
00835                 }
00836                 
00837                 //  Kill the file handles
00838                 imagedestroy($src_img);
00839                 
00840                 // Set the file to 777
00841                 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00842                 
00843                 return TRUE;
00844         }
00845         
00846         // --------------------------------------------------------------------
00847         
00848         /**
00849          * Image Watermark
00850          *
00851          * This is a wrapper function that chooses the type
00852          * of watermarking based on the specified preference.
00853          *
00854          * @access      public
00855          * @param       string
00856          * @return      bool
00857          */     
00858         function watermark()
00859         {
00860                 if ($this->wm_type == 'overlay')
00861                 {
00862                         return $this->overlay_watermark();
00863                 }
00864                 else
00865                 {
00866                         return $this->text_watermark();
00867                 }
00868         }
00869         
00870         // --------------------------------------------------------------------
00871         
00872         /**
00873          * Watermark - Graphic Version
00874          *
00875          * @access      public
00876          * @return      bool
00877          */                     
00878         function overlay_watermark()
00879         {
00880                 if ( ! function_exists('imagecolortransparent'))
00881                 {
00882                         $this->set_error('imglib_gd_required');
00883                         return FALSE;           
00884                 }
00885         
00886                 //  Fetch source image properties
00887                 $this->get_image_properties();
00888 
00889                 //  Fetch watermark image properties
00890                 $props                  = $this->get_image_properties($this->wm_overlay_path, TRUE);    
00891                 $wm_img_type    = $props['image_type'];
00892                 $wm_width               = $props['width'];
00893                 $wm_height              = $props['height'];
00894         
00895                 //  Create two image resources  
00896                 $wm_img  = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
00897                 $src_img = $this->image_create_gd($this->full_src_path);
00898                 
00899                 // Reverse the offset if necessary              
00900                 // When the image is positioned at the bottom
00901                 // we don't want the vertical offset to push it
00902                 // further down.  We want the reverse, so we'll
00903                 // invert the offset.  Same with the horizontal
00904                 // offset when the image is at the right
00905                 
00906                 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
00907                 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
00908         
00909                 if ($this->wm_vrt_alignment == 'B')
00910                         $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
00911         
00912                 if ($this->wm_hor_alignment == 'R')
00913                         $this->wm_hor_offset = $this->wm_hor_offset * -1;
00914 
00915                 //  Set the base x and y axis values
00916                 $x_axis = $this->wm_hor_offset + $this->wm_padding;
00917                 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
00918 
00919                 //  Set the vertical position
00920                 switch ($this->wm_vrt_alignment)
00921                 {
00922                         case 'T':
00923                                 break;
00924                         case 'M':       $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
00925                                 break;
00926                         case 'B':       $y_axis += $this->orig_height - $wm_height;
00927                                 break;
00928                 }
00929 
00930                 //  Set the horizontal position
00931                 switch ($this->wm_hor_alignment)
00932                 {
00933                         case 'L':
00934                                 break;  
00935                         case 'C':       $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
00936                                 break;
00937                         case 'R':       $x_axis += $this->orig_width - $wm_width;
00938                                 break;
00939                 }
00940         
00941                 //  Build the finalized image                   
00942                 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
00943                 {
00944                         @imagealphablending($src_img, TRUE);
00945                 }               
00946                 
00947                 // Set RGB values for text and shadow
00948                 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
00949                 $alpha = ($rgba & 0x7F000000) >> 24;
00950                 
00951                 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
00952                 if ($alpha > 0)
00953                 {
00954                         // copy the image directly, the image's alpha transparency being the sole determinant of blending
00955                         imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
00956                 }
00957                 else
00958                 {
00959                         // set our RGB value from above to be transparent and merge the images with the specified opacity
00960                         imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
00961                         imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);                    
00962                 }
00963                                 
00964                 //  Output the image
00965                 if ($this->dynamic_output == TRUE)
00966                 {
00967                         $this->image_display_gd($src_img);
00968                 }
00969                 else
00970                 {
00971                         if ( ! $this->image_save_gd($src_img))
00972                         {
00973                                 return FALSE;
00974                         }
00975                 }
00976                 
00977                 imagedestroy($src_img);
00978                 imagedestroy($wm_img);
00979                                 
00980                 return TRUE;
00981         }
00982         
00983         // --------------------------------------------------------------------
00984         
00985         /**
00986          * Watermark - Text Version
00987          *
00988          * @access      public
00989          * @return      bool
00990          */                     
00991         function text_watermark()
00992         {
00993                 if ( ! ($src_img = $this->image_create_gd()))
00994                 {               
00995                         return FALSE;
00996                 }
00997                                 
00998                 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
00999                 {
01000                         $this->set_error('imglib_missing_font');
01001                         return FALSE;
01002                 }
01003                 
01004                 //  Fetch source image properties               
01005                 $this->get_image_properties();                          
01006                 
01007                 // Set RGB values for text and shadow           
01008                 $this->wm_font_color    = str_replace('#', '', $this->wm_font_color);
01009                 $this->wm_shadow_color  = str_replace('#', '', $this->wm_shadow_color);
01010                 
01011                 $R1 = hexdec(substr($this->wm_font_color, 0, 2));
01012                 $G1 = hexdec(substr($this->wm_font_color, 2, 2));
01013                 $B1 = hexdec(substr($this->wm_font_color, 4, 2));
01014         
01015                 $R2 = hexdec(substr($this->wm_shadow_color, 0, 2));
01016                 $G2 = hexdec(substr($this->wm_shadow_color, 2, 2));
01017                 $B2 = hexdec(substr($this->wm_shadow_color, 4, 2));
01018                 
01019                 $txt_color      = imagecolorclosest($src_img, $R1, $G1, $B1);
01020                 $drp_color      = imagecolorclosest($src_img, $R2, $G2, $B2);
01021 
01022                 // Reverse the vertical offset
01023                 // When the image is positioned at the bottom
01024                 // we don't want the vertical offset to push it
01025                 // further down.  We want the reverse, so we'll
01026                 // invert the offset.  Note: The horizontal
01027                 // offset flips itself automatically
01028         
01029                 if ($this->wm_vrt_alignment == 'B')
01030                         $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
01031                         
01032                 if ($this->wm_hor_alignment == 'R')
01033                         $this->wm_hor_offset = $this->wm_hor_offset * -1;
01034 
01035                 // Set font width and height
01036                 // These are calculated differently depending on
01037                 // whether we are using the true type font or not
01038                 if ($this->wm_use_truetype == TRUE)
01039                 {
01040                         if ($this->wm_font_size == '')
01041                                 $this->wm_font_size = '17';
01042                 
01043                         $fontwidth  = $this->wm_font_size-($this->wm_font_size/4);
01044                         $fontheight = $this->wm_font_size;
01045                         $this->wm_vrt_offset += $this->wm_font_size;
01046                 }
01047                 else
01048                 {
01049                         $fontwidth  = imagefontwidth($this->wm_font_size);
01050                         $fontheight = imagefontheight($this->wm_font_size);
01051                 }
01052 
01053                 // Set base X and Y axis values
01054                 $x_axis = $this->wm_hor_offset + $this->wm_padding;
01055                 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
01056 
01057                 // Set verticle alignment
01058                 if ($this->wm_use_drop_shadow == FALSE)
01059                         $this->wm_shadow_distance = 0;
01060                         
01061                 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
01062                 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
01063         
01064                 switch ($this->wm_vrt_alignment)
01065                 {
01066                         case     "T" :
01067                                 break;
01068                         case "M":       $y_axis += ($this->orig_height/2)+($fontheight/2);
01069                                 break;
01070                         case "B":       $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
01071                                 break;
01072                 }
01073         
01074                 $x_shad = $x_axis + $this->wm_shadow_distance;
01075                 $y_shad = $y_axis + $this->wm_shadow_distance;
01076                 
01077                 // Set horizontal alignment
01078                 switch ($this->wm_hor_alignment)
01079                 {
01080                         case "L":
01081                                 break;
01082                         case "R":
01083                                                 if ($this->wm_use_drop_shadow)
01084                                                         $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
01085                                                         $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
01086                                 break;
01087                         case "C":
01088                                                 if ($this->wm_use_drop_shadow)
01089                                                         $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
01090                                                         $x_axis += floor(($this->orig_width  -$fontwidth*strlen($this->wm_text))/2);
01091                                 break;
01092                 }
01093                 
01094                 //  Add the text to the source image
01095                 if ($this->wm_use_truetype)
01096                 {       
01097                         if ($this->wm_use_drop_shadow)
01098                                 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
01099                                 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
01100                 }
01101                 else
01102                 {
01103                         if ($this->wm_use_drop_shadow)
01104                                 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
01105                                 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
01106                 }
01107         
01108                 //  Output the final image
01109                 if ($this->dynamic_output == TRUE)
01110                 {
01111                         $this->image_display_gd($src_img);
01112                 }
01113                 else
01114                 {
01115                         $this->image_save_gd($src_img);
01116                 }
01117                 
01118                 imagedestroy($src_img);
01119         
01120                 return TRUE;
01121         }
01122         
01123         // --------------------------------------------------------------------
01124         
01125         /**
01126          * Create Image - GD
01127          *
01128          * This simply creates an image resource handle
01129          * based on the type of image being processed
01130          *
01131          * @access      public
01132          * @param       string
01133          * @return      resource
01134          */                     
01135         function image_create_gd($path = '', $image_type = '')
01136         {
01137                 if ($path == '')
01138                         $path = $this->full_src_path;
01139                         
01140                 if ($image_type == '')
01141                         $image_type = $this->image_type;
01142         
01143                 
01144                 switch ($image_type)
01145                 {
01146                         case     1 :
01147                                                 if ( ! function_exists('imagecreatefromgif'))
01148                                                 {
01149                                                         $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
01150                                                         return FALSE;
01151                                                 }
01152                                         
01153                                                 return imagecreatefromgif($path);
01154                                 break;
01155                         case 2 :
01156                                                 if ( ! function_exists('imagecreatefromjpeg'))
01157                                                 {
01158                                                         $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
01159                                                         return FALSE;
01160                                                 }
01161                                         
01162                                                 return imagecreatefromjpeg($path);
01163                                 break;
01164                         case 3 :
01165                                                 if ( ! function_exists('imagecreatefrompng'))
01166                                                 {
01167                                                         $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));                          
01168                                                         return FALSE;
01169                                                 }
01170                                         
01171                                                 return imagecreatefrompng($path);
01172                                 break;                  
01173                 
01174                 }
01175                 
01176                 $this->set_error(array('imglib_unsupported_imagecreate'));
01177                 return FALSE;
01178         }
01179         
01180         // --------------------------------------------------------------------
01181         
01182         /**
01183          * Write image file to disk - GD
01184          *
01185          * Takes an image resource as input and writes the file
01186          * to the specified destination
01187          *
01188          * @access      public
01189          * @param       resource
01190          * @return      bool
01191          */                     
01192         function image_save_gd($resource)
01193         {       
01194                 switch ($this->image_type)
01195                 {
01196                         case 1 :
01197                                                 if ( ! function_exists('imagegif'))
01198                                                 {
01199                                                         $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
01200                                                         return FALSE;           
01201                                                 }
01202                                                 
01203                                                 @imagegif($resource, $this->full_dst_path);
01204                                 break;
01205                         case 2  :
01206                                                 if ( ! function_exists('imagejpeg'))
01207                                                 {
01208                                                         $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
01209                                                         return FALSE;           
01210                                                 }
01211                                                 
01212                                                 if (phpversion() == '4.4.1')
01213                                                 {
01214                                                         @touch($this->full_dst_path); // PHP 4.4.1 bug #35060 - workaround
01215                                                 }
01216                                                 
01217                                                 @imagejpeg($resource, $this->full_dst_path, $this->quality);
01218                                 break;
01219                         case 3  :
01220                                                 if ( ! function_exists('imagepng'))
01221                                                 {
01222                                                         $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
01223                                                         return FALSE;           
01224                                                 }
01225                                         
01226                                                 @imagepng($resource, $this->full_dst_path);
01227                                 break;
01228                         default         :
01229                                                         $this->set_error(array('imglib_unsupported_imagecreate'));
01230                                                         return FALSE;           
01231                                 break;          
01232                 }
01233         
01234                 return TRUE;
01235         }       
01236         
01237         // --------------------------------------------------------------------
01238         
01239         /**
01240          * Dynamically outputs an image
01241          *
01242          * @access      public
01243          * @param       resource
01244          * @return      void
01245          */                     
01246         function image_display_gd($resource)
01247         {               
01248                 header("Content-Disposition: filename={$this->source_image};");
01249                 header("Content-Type: {$this->mime_type}");
01250                 header('Content-Transfer-Encoding: binary');
01251                 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
01252         
01253                 switch ($this->image_type)
01254                 {
01255                         case 1          :       imagegif($resource);
01256                                 break;
01257                         case 2          :       imagejpeg($resource, '', $this->quality);
01258                                 break;
01259                         case 3          :       imagepng($resource);
01260                                 break;
01261                         default         :       echo 'Unable to display the image';
01262                                 break;          
01263                 }                       
01264         }
01265         
01266         // --------------------------------------------------------------------
01267         
01268         /**
01269          * Re-proportion Image Width/Height
01270          *
01271          * When creating thumbs, the desired width/height
01272          * can end up warping the image due to an incorrect
01273          * ratio between the full-sized image and the thumb.
01274          *
01275          * This function lets us re-proportion the width/height
01276          * if users choose to maintain the aspect ratio when resizing.
01277          *
01278          * @access      public
01279          * @return      void
01280          */                     
01281         function image_reproportion()
01282         {
01283                 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
01284                         return;
01285                 
01286                 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
01287                         return;
01288                 
01289                 $new_width      = ceil($this->orig_width*$this->height/$this->orig_height);             
01290                 $new_height     = ceil($this->width*$this->orig_height/$this->orig_width);
01291                 
01292                 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
01293         
01294                 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
01295                 {
01296                         $this->master_dim = ($ratio < 0) ? 'width' : 'height';
01297                 }
01298                 
01299                 if (($this->width != $new_width) AND ($this->height != $new_height))
01300                 {
01301                         if ($this->master_dim == 'height')
01302                         {
01303                                 $this->width = $new_width;
01304                         }
01305                         else
01306                         {
01307                                 $this->height = $new_height;
01308                         }
01309                 }
01310         }       
01311         
01312         // --------------------------------------------------------------------
01313         
01314         /**
01315          * Get image properties
01316          *
01317          * A helper function that gets info about the file
01318          *
01319          * @access      public
01320          * @param       string
01321          * @return      mixed
01322          */                     
01323         function get_image_properties($path = '', $return = FALSE)
01324         {
01325                 // For now we require GD but we should
01326                 // find a way to determine this using IM or NetPBM
01327                 
01328                 if ($path == '')
01329                         $path = $this->full_src_path;
01330                                 
01331                 if ( ! file_exists($path))
01332                 {
01333                         $this->set_error('imglib_invalid_path');                
01334                         return FALSE;                           
01335                 }
01336                 
01337                 $vals = @getimagesize($path);
01338                 
01339                 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
01340                 
01341                 $mime = (isset($types[$vals['2']])) ? 'image/'.$types[$vals['2']] : 'image/jpg';
01342                                 
01343                 if ($return == TRUE)
01344                 {
01345                         $v['width']                     = $vals['0'];
01346                         $v['height']            = $vals['1'];
01347                         $v['image_type']        = $vals['2'];
01348                         $v['size_str']          = $vals['3'];
01349                         $v['mime_type']         = $mime;
01350                         
01351                         return $v;
01352                 }
01353                 
01354                 $this->orig_width       = $vals['0'];
01355                 $this->orig_height      = $vals['1'];
01356                 $this->image_type       = $vals['2'];
01357                 $this->size_str         = $vals['3'];
01358                 $this->mime_type        = $mime;
01359                 
01360                 return TRUE;
01361         }
01362         
01363         // --------------------------------------------------------------------
01364         
01365         /**
01366          * Size calculator
01367          *
01368          * This function takes a known width x height and
01369          * recalculates it to a new size.  Only one
01370          * new variable needs to be known
01371          *
01372          *      $props = array(
01373          *                                      'width'                 => $width,
01374          *                                      'height'                => $height,
01375          *                                      'new_width'             => 40,
01376          *                                      'new_height'    => ''
01377          *                                );
01378          *
01379          * @access      public
01380          * @param       array
01381          * @return      array
01382          */                     
01383         function size_calculator($vals)
01384         {
01385                 if ( ! is_array($vals))
01386                         return;
01387                         
01388                 $allowed = array('new_width', 'new_height', 'width', 'height');
01389         
01390                 foreach ($allowed as $item)
01391                 {
01392                         if ( ! isset($vals[$item]) OR $vals[$item] == '')
01393                                 $vals[$item] = 0;
01394                 }
01395                 
01396                 if ($vals['width'] == 0 OR $vals['height'] == 0)
01397                 {
01398                         return $vals;
01399                 }
01400                         
01401                 if ($vals['new_width'] == 0)
01402                 {
01403                         $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
01404                 }
01405                 elseif ($vals['new_height'] == 0)
01406                 {
01407                         $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
01408                 }
01409         
01410                 return $vals;
01411         }
01412         
01413         // --------------------------------------------------------------------
01414         
01415         /**
01416          * Explode source_image
01417          *
01418          * This is a helper function that extracts the extension
01419          * from the source_image.  This function lets us deal with
01420          * source_images with multiple periods, like:  my.cool.jpg
01421          * It returns an associative array with two elements:
01422          * $array['ext']  = '.jpg';
01423          * $array['name'] = 'my.cool';
01424          *
01425          * @access      public
01426          * @param       array
01427          * @return      array
01428          */     
01429         function explode_name($source_image)
01430         {
01431                 $x = explode('.', $source_image);
01432                 $ret['ext'] = '.'.end($x);
01433                 
01434                 $name = '';
01435                 
01436                 $ct = count($x)-1;
01437                 
01438                 for ($i = 0; $i < $ct; $i++)
01439                 {
01440                         $name .= $x[$i];
01441                         
01442                         if ($i < ($ct - 1))
01443                         {
01444                                 $name .= '.';
01445                         }
01446                 }
01447                 
01448                 $ret['name'] = $name;
01449                 
01450                 return $ret;
01451         }       
01452         
01453         // --------------------------------------------------------------------
01454         
01455         /**
01456          * Is GD Installed?
01457          *
01458          * @access      public
01459          * @return      bool
01460          */     
01461         function gd_loaded()
01462         {
01463                 if ( ! extension_loaded('gd'))
01464                 {
01465                         if ( ! dl('gd.so'))
01466                         {
01467                                 return FALSE;
01468                         }
01469                 }
01470                 
01471                 return TRUE;
01472         }
01473         
01474         // --------------------------------------------------------------------
01475         
01476         /**
01477          * Get GD version
01478          *
01479          * @access      public
01480          * @return      mixed
01481          */     
01482         function gd_version()
01483         {
01484                 if (function_exists('gd_info'))
01485                 {
01486                         $gd_version = @gd_info();
01487                         $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
01488                         
01489                         return $gd_version;
01490                 }
01491                 
01492                 return FALSE;
01493         }
01494         
01495         // --------------------------------------------------------------------
01496         
01497         /**
01498          * Set error message
01499          *
01500          * @access      public
01501          * @param       string
01502          * @return      void
01503          */     
01504         function set_error($msg)
01505         {
01506                 $CI =& get_instance();
01507                 $CI->lang->load('imglib');
01508                 
01509                 if (is_array($msg))
01510                 {
01511                         foreach ($msg as $val)
01512                         {
01513                                 
01514                                 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
01515                                 $this->error_msg[] = $msg;
01516                                 log_message('error', $msg);
01517                         }               
01518                 }
01519                 else
01520                 {
01521                         $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
01522                         $this->error_msg[] = $msg;
01523                         log_message('error', $msg);
01524                 }
01525         }
01526         
01527         // --------------------------------------------------------------------
01528         
01529         /**
01530          * Show error messages
01531          *
01532          * @access      public
01533          * @param       string
01534          * @return      string
01535          */     
01536         function display_errors($open = '<p>', $close = '</p>')
01537         {       
01538                 $str = '';
01539                 foreach ($this->error_msg as $val)
01540                 {
01541                         $str .= $open.$val.$close;
01542                 }
01543         
01544                 return $str;
01545         }
01546 
01547 }
01548 // END Image_lib Class
01549 
01550 /* End of file Image_lib.php */
01551 /* Location: ./system/libraries/Image_lib.php */