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) 2008, 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 the target width/height match the source, AND if the new file name is not equal to the old file name
00453                 // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
00454                 if ($this->dynamic_output === FALSE)
00455                 {
00456                         if ($this->orig_width == $this->width AND $this->orig_height == $this->height)                  
00457                         {
00458                                 if ($this->source_image != $this->new_image)
00459                                 {
00460                                         if (@copy($this->full_src_path, $this->full_dst_path))
00461                                         {
00462                                                 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00463                                         }
00464                                 }
00465                                 
00466                                 return TRUE;
00467                         }
00468                 }
00469                 
00470                 // Let's set up our values based on the action
00471                 if ($action == 'crop')
00472                 {
00473                         //  Reassign the source width/height if cropping
00474                         $this->orig_width  = $this->width;
00475                         $this->orig_height = $this->height;     
00476                                 
00477                         // GD 2.0 has a cropping bug so we'll test for it
00478                         if ($this->gd_version() !== FALSE)
00479                         {
00480                                 $gd_version = str_replace('0', '', $this->gd_version());                        
00481                                 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
00482                         }
00483                 }
00484                 else
00485                 {                       
00486                         // If resizing the x/y axis must be zero
00487                         $this->x_axis = 0;
00488                         $this->y_axis = 0;
00489                 }
00490                 
00491                 //  Create the image handle
00492                 if ( ! ($src_img = $this->image_create_gd()))
00493                 {               
00494                         return FALSE;
00495                 }
00496 
00497                 //  Create The Image
00498                 //
00499                 //  old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
00500                 //  it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
00501                 //  below should that ever prove inaccurate.
00502                 //
00503                 //  if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
00504                 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))          
00505                 {
00506                         $create = 'imagecreatetruecolor';
00507                         $copy   = 'imagecopyresampled';
00508                 }
00509                 else
00510                 {
00511                         $create = 'imagecreate';        
00512                         $copy   = 'imagecopyresized';
00513                 }
00514                         
00515                 $dst_img = $create($this->width, $this->height);
00516                 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
00517 
00518                 //  Show the image      
00519                 if ($this->dynamic_output == TRUE)
00520                 {
00521                         $this->image_display_gd($dst_img);
00522                 }
00523                 else
00524                 {
00525                         // Or save it
00526                         if ( ! $this->image_save_gd($dst_img))
00527                         {
00528                                 return FALSE;
00529                         }
00530                 }
00531 
00532                 //  Kill the file handles
00533                 imagedestroy($dst_img);
00534                 imagedestroy($src_img);
00535                 
00536                 // Set the file to 777
00537                 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00538                 
00539                 return TRUE;
00540         }
00541         
00542         // --------------------------------------------------------------------
00543         
00544         /**
00545          * Image Process Using ImageMagick
00546          *
00547          * This function will resize, crop or rotate
00548          *
00549          * @access      public
00550          * @param       string
00551          * @return      bool
00552          */             
00553         function image_process_imagemagick($action = 'resize')
00554         {
00555                 //  Do we have a vaild library path?
00556                 if ($this->library_path == '')
00557                 {
00558                         $this->set_error('imglib_libpath_invalid');
00559                         return FALSE;
00560                 }
00561                                 
00562                 if ( ! eregi("convert$", $this->library_path))
00563                 {
00564                         if ( ! eregi("/$", $this->library_path)) $this->library_path .= "/";
00565                 
00566                         $this->library_path .= 'convert';
00567                 }
00568                 
00569                 // Execute the command
00570                 $cmd = $this->library_path." -quality ".$this->quality;
00571         
00572                 if ($action == 'crop')
00573                 {
00574                         $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
00575                 }
00576                 elseif ($action == 'rotate')
00577                 {
00578                         switch ($this->rotation_angle)
00579                         {
00580                                 case 'hor'      : $angle = '-flop';
00581                                         break;
00582                                 case 'vrt'      : $angle = '-flip';
00583                                         break;
00584                                 default         : $angle = '-rotate '.$this->rotation_angle;
00585                                         break;
00586                         }                       
00587                 
00588                         $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
00589                 }
00590                 else  // Resize
00591                 {
00592                         $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
00593                 }
00594         
00595                 $retval = 1;
00596         
00597                 @exec($cmd, $output, $retval);
00598 
00599                 //      Did it work?    
00600                 if ($retval > 0)
00601                 {
00602                         $this->set_error('imglib_image_process_failed');
00603                         return FALSE;
00604                 }
00605                 
00606                 // Set the file to 777
00607                 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00608                 
00609                 return TRUE;
00610         }
00611         
00612         // --------------------------------------------------------------------
00613         
00614         /**
00615          * Image Process Using NetPBM
00616          *
00617          * This function will resize, crop or rotate
00618          *
00619          * @access      public
00620          * @param       string
00621          * @return      bool
00622          */             
00623         function image_process_netpbm($action = 'resize')
00624         {
00625                 if ($this->library_path == '')
00626                 {
00627                         $this->set_error('imglib_libpath_invalid');
00628                         return FALSE;
00629                 }
00630                         
00631                 //  Build the resizing command
00632                 switch ($this->image_type)
00633                 {
00634                         case 1 :
00635                                                 $cmd_in         = 'giftopnm';
00636                                                 $cmd_out        = 'ppmtogif';
00637                                 break;
00638                         case 2 :
00639                                                 $cmd_in         = 'jpegtopnm';
00640                                                 $cmd_out        = 'ppmtojpeg';                  
00641                                 break;
00642                         case 3 :
00643                                                 $cmd_in         = 'pngtopnm';
00644                                                 $cmd_out        = 'ppmtopng';
00645                                 break;
00646                 }
00647                 
00648                 if ($action == 'crop')
00649                 {
00650                         $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
00651                 }
00652                 elseif ($action == 'rotate')
00653                 {
00654                         switch ($this->rotation_angle)
00655                         {
00656                                 case 90         :       $angle = 'r270';
00657                                         break;
00658                                 case 180        :       $angle = 'r180';
00659                                         break;
00660                                 case 270        :       $angle = 'r90';
00661                                         break;
00662                                 case 'vrt'      :       $angle = 'tb';
00663                                         break;
00664                                 case 'hor'      :       $angle = 'lr';
00665                                         break;
00666                         }
00667                 
00668                         $cmd_inner = 'pnmflip -'.$angle.' ';
00669                 }
00670                 else // Resize
00671                 {
00672                         $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
00673                 }
00674                                                 
00675                 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
00676                 
00677                 $retval = 1;
00678                 
00679                 @exec($cmd, $output, $retval);
00680                 
00681                 //  Did it work?
00682                 if ($retval > 0)
00683                 {
00684                         $this->set_error('imglib_image_process_failed');
00685                         return FALSE;
00686                 }
00687                 
00688                 // With NetPBM we have to create a temporary image.
00689                 // If you try manipulating the original it fails so
00690                 // we have to rename the temp file.
00691                 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
00692                 unlink ($this->dest_folder.'netpbm.tmp');
00693                 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00694                 
00695                 return TRUE;
00696         }
00697         
00698         // --------------------------------------------------------------------
00699         
00700         /**
00701          * Image Rotate Using GD
00702          *
00703          * @access      public
00704          * @return      bool
00705          */             
00706         function image_rotate_gd()
00707         {       
00708                 // Is Image Rotation Supported?
00709                 // this function is only supported as of PHP 4.3
00710                 if ( ! function_exists('imagerotate'))
00711                 {
00712                         $this->set_error('imglib_rotate_unsupported');
00713                         return FALSE;
00714                 }
00715                 
00716                 //  Create the image handle
00717                 if ( ! ($src_img = $this->image_create_gd()))
00718                 {               
00719                         return FALSE;
00720                 }
00721 
00722                 // Set the background color             
00723                 // This won't work with transparent PNG files so we are
00724                 // going to have to figure out how to determine the color
00725                 // of the alpha channel in a future release.
00726         
00727                 $white  = imagecolorallocate($src_img, 255, 255, 255);
00728 
00729                 //  Rotate it!
00730                 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
00731         
00732                 //  Save the Image
00733                 if ($this->dynamic_output == TRUE)
00734                 {
00735                         $this->image_display_gd($dst_img);
00736                 }
00737                 else
00738                 {
00739                         // Or save it
00740                         if ( ! $this->image_save_gd($dst_img))
00741                         {
00742                                 return FALSE;
00743                         }
00744                 }
00745 
00746                 //  Kill the file handles
00747                 imagedestroy($dst_img);
00748                 imagedestroy($src_img);
00749                 
00750                 // Set the file to 777
00751                 
00752                 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00753                 
00754                 return true;
00755         }
00756         
00757         // --------------------------------------------------------------------
00758         
00759         /**
00760          * Create Mirror Image using GD
00761          *
00762          * This function will flip horizontal or vertical
00763          *
00764          * @access      public
00765          * @return      bool
00766          */                     
00767         function image_mirror_gd()
00768         {               
00769                 if ( ! $src_img = $this->image_create_gd())
00770                 {
00771                         return FALSE;
00772                 }
00773                 
00774                 $width  = $this->orig_width;
00775                 $height = $this->orig_height;
00776         
00777                 if ($this->rotation_angle == 'hor')
00778                 {
00779                         for ($i = 0; $i < $height; $i++)
00780                         {
00781                                 $left  = 0;
00782                                 $right = $width-1;
00783         
00784                                 while ($left < $right)
00785                                 {
00786                                         $cl = imagecolorat($src_img, $left, $i);
00787                                         $cr = imagecolorat($src_img, $right, $i);
00788                                         
00789                                         imagesetpixel($src_img, $left, $i, $cr);
00790                                         imagesetpixel($src_img, $right, $i, $cl);
00791                                         
00792                                         $left++;
00793                                         $right--;
00794                                 }
00795                         }
00796                 }
00797                 else
00798                 {
00799                         for ($i = 0; $i < $width; $i++)
00800                         {
00801                                 $top = 0;
00802                                 $bot = $height-1;
00803         
00804                                 while ($top < $bot)
00805                                 {
00806                                         $ct = imagecolorat($src_img, $i, $top);
00807                                         $cb = imagecolorat($src_img, $i, $bot);
00808                                         
00809                                         imagesetpixel($src_img, $i, $top, $cb);
00810                                         imagesetpixel($src_img, $i, $bot, $ct);
00811                                         
00812                                         $top++;
00813                                         $bot--;
00814                                 }
00815                         }               
00816                 }               
00817 
00818                 //  Show the image
00819                 if ($this->dynamic_output == TRUE)
00820                 {
00821                         $this->image_display_gd($src_img);
00822                 }
00823                 else
00824                 {
00825                         // Or save it
00826                         if ( ! $this->image_save_gd($src_img))
00827                         {
00828                                 return FALSE;
00829                         }
00830                 }
00831                 
00832                 //  Kill the file handles
00833                 imagedestroy($src_img);
00834                 
00835                 // Set the file to 777
00836                 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00837                 
00838                 return TRUE;
00839         }
00840         
00841         // --------------------------------------------------------------------
00842         
00843         /**
00844          * Image Watermark
00845          *
00846          * This is a wrapper function that chooses the type
00847          * of watermarking based on the specified preference.
00848          *
00849          * @access      public
00850          * @param       string
00851          * @return      bool
00852          */     
00853         function watermark()
00854         {
00855                 if ($this->wm_type == 'overlay')
00856                 {
00857                         return $this->overlay_watermark();
00858                 }
00859                 else
00860                 {
00861                         return $this->text_watermark();
00862                 }
00863         }
00864         
00865         // --------------------------------------------------------------------
00866         
00867         /**
00868          * Watermark - Graphic Version
00869          *
00870          * @access      public
00871          * @return      bool
00872          */                     
00873         function overlay_watermark()
00874         {
00875                 if ( ! function_exists('imagecolortransparent'))
00876                 {
00877                         $this->set_error('imglib_gd_required');
00878                         return FALSE;           
00879                 }
00880         
00881                 //  Fetch source image properties
00882                 $this->get_image_properties();
00883 
00884                 //  Fetch watermark image properties
00885                 $props                  = $this->get_image_properties($this->wm_overlay_path, TRUE);    
00886                 $wm_img_type    = $props['image_type'];
00887                 $wm_width               = $props['width'];
00888                 $wm_height              = $props['height'];
00889         
00890                 //  Create two image resources  
00891                 $wm_img  = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
00892                 $src_img = $this->image_create_gd($this->full_src_path);
00893                 
00894                 // Reverse the offset if necessary              
00895                 // When the image is positioned at the bottom
00896                 // we don't want the vertical offset to push it
00897                 // further down.  We want the reverse, so we'll
00898                 // invert the offset.  Same with the horizontal
00899                 // offset when the image is at the right
00900                 
00901                 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
00902                 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
00903         
00904                 if ($this->wm_vrt_alignment == 'B')
00905                         $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
00906         
00907                 if ($this->wm_hor_alignment == 'R')
00908                         $this->wm_hor_offset = $this->wm_hor_offset * -1;
00909 
00910                 //  Set the base x and y axis values
00911                 $x_axis = $this->wm_hor_offset + $this->wm_padding;
00912                 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
00913 
00914                 //  Set the vertical position
00915                 switch ($this->wm_vrt_alignment)
00916                 {
00917                         case 'T':
00918                                 break;
00919                         case 'M':       $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
00920                                 break;
00921                         case 'B':       $y_axis += $this->orig_height - $wm_height;
00922                                 break;
00923                 }
00924 
00925                 //  Set the horizontal position
00926                 switch ($this->wm_hor_alignment)
00927                 {
00928                         case 'L':
00929                                 break;  
00930                         case 'C':       $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
00931                                 break;
00932                         case 'R':       $x_axis += $this->orig_width - $wm_width;
00933                                 break;
00934                 }
00935         
00936                 //  Build the finalized image                   
00937                 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
00938                 {
00939                         @imagealphablending($src_img, TRUE);
00940                 }               
00941 
00942                 // Set RGB values for text and shadow
00943                 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
00944                 $alpha = ($rgba & 0x7F000000) >> 24;
00945                 
00946                 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
00947                 if ($alpha > 0)
00948                 {
00949                         // copy the image directly, the image's alpha transparency being the sole determinant of blending
00950                         imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
00951                 }
00952                 else
00953                 {
00954                         // set our RGB value from above to be transparent and merge the images with the specified opacity
00955                         imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
00956                         imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);                    
00957                 }
00958                                 
00959                 //  Output the image
00960                 if ($this->dynamic_output == TRUE)
00961                 {
00962                         $this->image_display_gd($src_img);
00963                 }
00964                 else
00965                 {
00966                         if ( ! $this->image_save_gd($src_img))
00967                         {
00968                                 return FALSE;
00969                         }
00970                 }
00971                 
00972                 imagedestroy($src_img);
00973                 imagedestroy($wm_img);
00974                                 
00975                 return TRUE;
00976         }
00977         
00978         // --------------------------------------------------------------------
00979         
00980         /**
00981          * Watermark - Text Version
00982          *
00983          * @access      public
00984          * @return      bool
00985          */                     
00986         function text_watermark()
00987         {
00988                 if ( ! ($src_img = $this->image_create_gd()))
00989                 {               
00990                         return FALSE;
00991                 }
00992                                 
00993                 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
00994                 {
00995                         $this->set_error('imglib_missing_font');
00996                         return FALSE;
00997                 }
00998                 
00999                 //  Fetch source image properties               
01000                 $this->get_image_properties();                          
01001                 
01002                 // Set RGB values for text and shadow           
01003                 $this->wm_font_color    = str_replace('#', '', $this->wm_font_color);
01004                 $this->wm_shadow_color  = str_replace('#', '', $this->wm_shadow_color);
01005                 
01006                 $R1 = hexdec(substr($this->wm_font_color, 0, 2));
01007                 $G1 = hexdec(substr($this->wm_font_color, 2, 2));
01008                 $B1 = hexdec(substr($this->wm_font_color, 4, 2));
01009         
01010                 $R2 = hexdec(substr($this->wm_shadow_color, 0, 2));
01011                 $G2 = hexdec(substr($this->wm_shadow_color, 2, 2));
01012                 $B2 = hexdec(substr($this->wm_shadow_color, 4, 2));
01013                 
01014                 $txt_color      = imagecolorclosest($src_img, $R1, $G1, $B1);
01015                 $drp_color      = imagecolorclosest($src_img, $R2, $G2, $B2);
01016 
01017                 // Reverse the vertical offset
01018                 // When the image is positioned at the bottom
01019                 // we don't want the vertical offset to push it
01020                 // further down.  We want the reverse, so we'll
01021                 // invert the offset.  Note: The horizontal
01022                 // offset flips itself automatically
01023         
01024                 if ($this->wm_vrt_alignment == 'B')
01025                         $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
01026                         
01027                 if ($this->wm_hor_alignment == 'R')
01028                         $this->wm_hor_offset = $this->wm_hor_offset * -1;
01029 
01030                 // Set font width and height
01031                 // These are calculated differently depending on
01032                 // whether we are using the true type font or not
01033                 if ($this->wm_use_truetype == TRUE)
01034                 {
01035                         if ($this->wm_font_size == '')
01036                                 $this->wm_font_size = '17';
01037                 
01038                         $fontwidth  = $this->wm_font_size-($this->wm_font_size/4);
01039                         $fontheight = $this->wm_font_size;
01040                         $this->wm_vrt_offset += $this->wm_font_size;
01041                 }
01042                 else
01043                 {
01044                         $fontwidth  = imagefontwidth($this->wm_font_size);
01045                         $fontheight = imagefontheight($this->wm_font_size);
01046                 }
01047 
01048                 // Set base X and Y axis values
01049                 $x_axis = $this->wm_hor_offset + $this->wm_padding;
01050                 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
01051 
01052                 // Set verticle alignment
01053                 if ($this->wm_use_drop_shadow == FALSE)
01054                         $this->wm_shadow_distance = 0;
01055                         
01056                 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
01057                 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
01058         
01059                 switch ($this->wm_vrt_alignment)
01060                 {
01061                         case     "T" :
01062                                 break;
01063                         case "M":       $y_axis += ($this->orig_height/2)+($fontheight/2);
01064                                 break;
01065                         case "B":       $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
01066                                 break;
01067                 }
01068         
01069                 $x_shad = $x_axis + $this->wm_shadow_distance;
01070                 $y_shad = $y_axis + $this->wm_shadow_distance;
01071                 
01072                 // Set horizontal alignment
01073                 switch ($this->wm_hor_alignment)
01074                 {
01075                         case "L":
01076                                 break;
01077                         case "R":
01078                                                 if ($this->wm_use_drop_shadow)
01079                                                         $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
01080                                                         $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
01081                                 break;
01082                         case "C":
01083                                                 if ($this->wm_use_drop_shadow)
01084                                                         $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
01085                                                         $x_axis += floor(($this->orig_width  -$fontwidth*strlen($this->wm_text))/2);
01086                                 break;
01087                 }
01088                 
01089                 //  Add the text to the source image
01090                 if ($this->wm_use_truetype)
01091                 {       
01092                         if ($this->wm_use_drop_shadow)
01093                                 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
01094                                 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
01095                 }
01096                 else
01097                 {
01098                         if ($this->wm_use_drop_shadow)
01099                                 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
01100                                 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
01101                 }
01102         
01103                 //  Output the final image
01104                 if ($this->dynamic_output == TRUE)
01105                 {
01106                         $this->image_display_gd($src_img);
01107                 }
01108                 else
01109                 {
01110                         $this->image_save_gd($src_img);
01111                 }
01112                 
01113                 imagedestroy($src_img);
01114         
01115                 return TRUE;
01116         }
01117         
01118         // --------------------------------------------------------------------
01119         
01120         /**
01121          * Create Image - GD
01122          *
01123          * This simply creates an image resource handle
01124          * based on the type of image being processed
01125          *
01126          * @access      public
01127          * @param       string
01128          * @return      resource
01129          */                     
01130         function image_create_gd($path = '', $image_type = '')
01131         {
01132                 if ($path == '')
01133                         $path = $this->full_src_path;
01134                         
01135                 if ($image_type == '')
01136                         $image_type = $this->image_type;
01137         
01138                 
01139                 switch ($image_type)
01140                 {
01141                         case     1 :
01142                                                 if ( ! function_exists('imagecreatefromgif'))
01143                                                 {
01144                                                         $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
01145                                                         return FALSE;
01146                                                 }
01147                                         
01148                                                 return imagecreatefromgif($path);
01149                                 break;
01150                         case 2 :
01151                                                 if ( ! function_exists('imagecreatefromjpeg'))
01152                                                 {
01153                                                         $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
01154                                                         return FALSE;
01155                                                 }
01156                                         
01157                                                 return imagecreatefromjpeg($path);
01158                                 break;
01159                         case 3 :
01160                                                 if ( ! function_exists('imagecreatefrompng'))
01161                                                 {
01162                                                         $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));                          
01163                                                         return FALSE;
01164                                                 }
01165                                         
01166                                                 return imagecreatefrompng($path);
01167                                 break;                  
01168                 
01169                 }
01170                 
01171                 $this->set_error(array('imglib_unsupported_imagecreate'));
01172                 return FALSE;
01173         }
01174         
01175         // --------------------------------------------------------------------
01176         
01177         /**
01178          * Write image file to disk - GD
01179          *
01180          * Takes an image resource as input and writes the file
01181          * to the specified destination
01182          *
01183          * @access      public
01184          * @param       resource
01185          * @return      bool
01186          */                     
01187         function image_save_gd($resource)
01188         {       
01189                 switch ($this->image_type)
01190                 {
01191                         case 1 :
01192                                                 if ( ! function_exists('imagegif'))
01193                                                 {
01194                                                         $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
01195                                                         return FALSE;           
01196                                                 }
01197                                                 
01198                                                 @imagegif($resource, $this->full_dst_path);
01199                                 break;
01200                         case 2  :
01201                                                 if ( ! function_exists('imagejpeg'))
01202                                                 {
01203                                                         $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
01204                                                         return FALSE;           
01205                                                 }
01206                                                 
01207                                                 if (phpversion() == '4.4.1')
01208                                                 {
01209                                                         @touch($this->full_dst_path); // PHP 4.4.1 bug #35060 - workaround
01210                                                 }
01211                                                 
01212                                                 @imagejpeg($resource, $this->full_dst_path, $this->quality);
01213                                 break;
01214                         case 3  :
01215                                                 if ( ! function_exists('imagepng'))
01216                                                 {
01217                                                         $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
01218                                                         return FALSE;           
01219                                                 }
01220                                         
01221                                                 @imagepng($resource, $this->full_dst_path);
01222                                 break;
01223                         default         :
01224                                                         $this->set_error(array('imglib_unsupported_imagecreate'));
01225                                                         return FALSE;           
01226                                 break;          
01227                 }
01228         
01229                 return TRUE;
01230         }       
01231         
01232         // --------------------------------------------------------------------
01233         
01234         /**
01235          * Dynamically outputs an image
01236          *
01237          * @access      public
01238          * @param       resource
01239          * @return      void
01240          */                     
01241         function image_display_gd($resource)
01242         {               
01243                 header("Content-Disposition: filename={$this->source_image};");
01244                 header("Content-Type: {$this->mime_type}");
01245                 header('Content-Transfer-Encoding: binary');
01246                 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
01247         
01248                 switch ($this->image_type)
01249                 {
01250                         case 1          :       imagegif($resource);
01251                                 break;
01252                         case 2          :       imagejpeg($resource, '', $this->quality);
01253                                 break;
01254                         case 3          :       imagepng($resource);
01255                                 break;
01256                         default         :       echo 'Unable to display the image';
01257                                 break;          
01258                 }                       
01259         }
01260         
01261         // --------------------------------------------------------------------
01262         
01263         /**
01264          * Re-proportion Image Width/Height
01265          *
01266          * When creating thumbs, the desired width/height
01267          * can end up warping the image due to an incorrect
01268          * ratio between the full-sized image and the thumb.
01269          *
01270          * This function lets us re-proportion the width/height
01271          * if users choose to maintain the aspect ratio when resizing.
01272          *
01273          * @access      public
01274          * @return      void
01275          */                     
01276         function image_reproportion()
01277         {
01278                 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
01279                         return;
01280                 
01281                 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
01282                         return;
01283                 
01284                 $new_width      = ceil($this->orig_width*$this->height/$this->orig_height);             
01285                 $new_height     = ceil($this->width*$this->orig_height/$this->orig_width);
01286                 
01287                 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
01288         
01289                 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
01290                 {
01291                         $this->master_dim = ($ratio < 0) ? 'width' : 'height';
01292                 }
01293                 
01294                 if (($this->width != $new_width) AND ($this->height != $new_height))
01295                 {
01296                         if ($this->master_dim == 'height')
01297                         {
01298                                 $this->width = $new_width;
01299                         }
01300                         else
01301                         {
01302                                 $this->height = $new_height;
01303                         }
01304                 }
01305         }       
01306         
01307         // --------------------------------------------------------------------
01308         
01309         /**
01310          * Get image properties
01311          *
01312          * A helper function that gets info about the file
01313          *
01314          * @access      public
01315          * @param       string
01316          * @return      mixed
01317          */                     
01318         function get_image_properties($path = '', $return = FALSE)
01319         {
01320                 // For now we require GD but we should
01321                 // find a way to determine this using IM or NetPBM
01322                 
01323                 if ($path == '')
01324                         $path = $this->full_src_path;
01325                                 
01326                 if ( ! file_exists($path))
01327                 {
01328                         $this->set_error('imglib_invalid_path');                
01329                         return FALSE;                           
01330                 }
01331                 
01332                 $vals = @getimagesize($path);
01333                 
01334                 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
01335                 
01336                 $mime = (isset($types[$vals['2']])) ? 'image/'.$types[$vals['2']] : 'image/jpg';
01337                                 
01338                 if ($return == TRUE)
01339                 {
01340                         $v['width']                     = $vals['0'];
01341                         $v['height']            = $vals['1'];
01342                         $v['image_type']        = $vals['2'];
01343                         $v['size_str']          = $vals['3'];
01344                         $v['mime_type']         = $mime;
01345                         
01346                         return $v;
01347                 }
01348                 
01349                 $this->orig_width       = $vals['0'];
01350                 $this->orig_height      = $vals['1'];
01351                 $this->image_type       = $vals['2'];
01352                 $this->size_str         = $vals['3'];
01353                 $this->mime_type        = $mime;
01354                 
01355                 return TRUE;
01356         }
01357         
01358         // --------------------------------------------------------------------
01359         
01360         /**
01361          * Size calculator
01362          *
01363          * This function takes a known width x height and
01364          * recalculates it to a new size.  Only one
01365          * new variable needs to be known
01366          *
01367          *      $props = array(
01368          *                                      'width'                 => $width,
01369          *                                      'height'                => $height,
01370          *                                      'new_width'             => 40,
01371          *                                      'new_height'    => ''
01372          *                                );
01373          *
01374          * @access      public
01375          * @param       array
01376          * @return      array
01377          */                     
01378         function size_calculator($vals)
01379         {
01380                 if ( ! is_array($vals))
01381                         return;
01382                         
01383                 $allowed = array('new_width', 'new_height', 'width', 'height');
01384         
01385                 foreach ($allowed as $item)
01386                 {
01387                         if ( ! isset($vals[$item]) OR $vals[$item] == '')
01388                                 $vals[$item] = 0;
01389                 }
01390                 
01391                 if ($vals['width'] == 0 OR $vals['height'] == 0)
01392                 {
01393                         return $vals;
01394                 }
01395                         
01396                 if ($vals['new_width'] == 0)
01397                 {
01398                         $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
01399                 }
01400                 elseif ($vals['new_height'] == 0)
01401                 {
01402                         $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
01403                 }
01404         
01405                 return $vals;
01406         }
01407         
01408         // --------------------------------------------------------------------
01409         
01410         /**
01411          * Explode source_image
01412          *
01413          * This is a helper function that extracts the extension
01414          * from the source_image.  This function lets us deal with
01415          * source_images with multiple periods, like:  my.cool.jpg
01416          * It returns an associative array with two elements:
01417          * $array['ext']  = '.jpg';
01418          * $array['name'] = 'my.cool';
01419          *
01420          * @access      public
01421          * @param       array
01422          * @return      array
01423          */     
01424         function explode_name($source_image)
01425         {
01426                 $x = explode('.', $source_image);
01427                 $ret['ext'] = '.'.end($x);
01428                 
01429                 $name = '';
01430                 
01431                 $ct = count($x)-1;
01432                 
01433                 for ($i = 0; $i < $ct; $i++)
01434                 {
01435                         $name .= $x[$i];
01436                         
01437                         if ($i < ($ct - 1))
01438                         {
01439                                 $name .= '.';
01440                         }
01441                 }
01442                 
01443                 $ret['name'] = $name;
01444                 
01445                 return $ret;
01446         }       
01447         
01448         // --------------------------------------------------------------------
01449         
01450         /**
01451          * Is GD Installed?
01452          *
01453          * @access      public
01454          * @return      bool
01455          */     
01456         function gd_loaded()
01457         {
01458                 if ( ! extension_loaded('gd'))
01459                 {
01460                         if ( ! dl('gd.so'))
01461                         {
01462                                 return FALSE;
01463                         }
01464                 }
01465                 
01466                 return TRUE;
01467         }
01468         
01469         // --------------------------------------------------------------------
01470         
01471         /**
01472          * Get GD version
01473          *
01474          * @access      public
01475          * @return      mixed
01476          */     
01477         function gd_version()
01478         {
01479                 if (function_exists('gd_info'))
01480                 {
01481                         $gd_version = @gd_info();
01482                         $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
01483                         
01484                         return $gd_version;
01485                 }
01486                 
01487                 return FALSE;
01488         }
01489         
01490         // --------------------------------------------------------------------
01491         
01492         /**
01493          * Set error message
01494          *
01495          * @access      public
01496          * @param       string
01497          * @return      void
01498          */     
01499         function set_error($msg)
01500         {
01501                 $CI =& get_instance();
01502                 $CI->lang->load('imglib');
01503                 
01504                 if (is_array($msg))
01505                 {
01506                         foreach ($msg as $val)
01507                         {
01508                                 
01509                                 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
01510                                 $this->error_msg[] = $msg;
01511                                 log_message('error', $msg);
01512                         }               
01513                 }
01514                 else
01515                 {
01516                         $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
01517                         $this->error_msg[] = $msg;
01518                         log_message('error', $msg);
01519                 }
01520         }
01521         
01522         // --------------------------------------------------------------------
01523         
01524         /**
01525          * Show error messages
01526          *
01527          * @access      public
01528          * @param       string
01529          * @return      string
01530          */     
01531         function display_errors($open = '<p>', $close = '</p>')
01532         {       
01533                 $str = '';
01534                 foreach ($this->error_msg as $val)
01535                 {
01536                         $str .= $open.$val.$close;
01537                 }
01538         
01539                 return $str;
01540         }
01541 
01542 }
01543 // END Image_lib Class
01544 
01545 /* End of file Image_lib.php */
01546 /* Location: ./system/libraries/Image_lib.php */