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