00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 class CI_Image_lib {
00028
00029 var $image_library = 'gd2';
00030 var $library_path = '';
00031 var $dynamic_output = FALSE;
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;
00040 var $master_dim = 'auto';
00041 var $rotation_angle = '';
00042 var $x_axis = '';
00043 var $y_axis = '';
00044
00045
00046 var $wm_text = '';
00047 var $wm_type = 'text';
00048 var $wm_x_transp = 4;
00049 var $wm_y_transp = 4;
00050 var $wm_overlay_path = '';
00051 var $wm_font_path = '';
00052 var $wm_font_size = 17;
00053 var $wm_vrt_alignment = 'B';
00054 var $wm_hor_alignment = 'C';
00055 var $wm_padding = 0;
00056 var $wm_hor_offset = 0;
00057 var $wm_vrt_offset = 0;
00058 var $wm_font_color = '#ffffff';
00059 var $wm_shadow_color = '';
00060 var $wm_shadow_distance = 2;
00061 var $wm_opacity = 50;
00062
00063
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
00081
00082
00083
00084
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
00100
00101
00102
00103
00104
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
00116 $this->master_dim = 'auto';
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 function initialize($props = array())
00129 {
00130
00131
00132
00133 if (count($props) > 0)
00134 {
00135 foreach ($props as $key => $val)
00136 {
00137 $this->$key = $val;
00138 }
00139 }
00140
00141
00142
00143
00144
00145
00146
00147 if ($this->source_image == '')
00148 {
00149 $this->set_error('imglib_source_image_required');
00150 return FALSE;
00151 }
00152
00153
00154
00155
00156
00157
00158
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
00171
00172
00173
00174
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
00191 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
00192 {
00193 return FALSE;
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
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
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
00245
00246
00247
00248
00249
00250
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
00268
00269
00270
00271
00272
00273
00274 if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
00275 {
00276 $this->image_reproportion();
00277 }
00278
00279
00280
00281
00282
00283
00284
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
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
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
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
00342
00343
00344
00345
00346
00347
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
00365
00366
00367
00368
00369
00370
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
00388
00389
00390
00391
00392
00393
00394
00395 function rotate()
00396 {
00397
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
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
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
00441
00442
00443
00444
00445
00446
00447
00448 function image_process_gd($action = 'resize')
00449 {
00450 $v2_override = FALSE;
00451
00452 if ($action == 'crop')
00453 {
00454
00455
00456
00457
00458 if ($this->width >= $this->orig_width AND $this->height >= $this->orig_height AND $this->dynamic_output !== TRUE)
00459 {
00460 return TRUE;
00461 }
00462
00463
00464 $this->orig_width = $this->width;
00465 $this->orig_height = $this->height;
00466
00467
00468 if ($this->gd_version() !== FALSE)
00469 {
00470 $gd_version = str_replace('0', '', $this->gd_version());
00471 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
00472 }
00473 }
00474 else
00475 {
00476
00477
00478
00479 if (($this->orig_width == $this->width AND $this->orig_height == $this->height) AND ($this->source_image != $this->new_image))
00480 {
00481 if ( ! @copy($this->full_src_path, $this->full_dst_path))
00482 {
00483 $this->set_error('imglib_copy_failed');
00484 return FALSE;
00485 }
00486
00487 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00488 return TRUE;
00489 }
00490
00491
00492 $this->x_axis = 0;
00493 $this->y_axis = 0;
00494 }
00495
00496
00497 if ( ! ($src_img = $this->image_create_gd()))
00498 {
00499 return FALSE;
00500 }
00501
00502
00503
00504
00505
00506
00507
00508
00509 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
00510 {
00511 $create = 'imagecreatetruecolor';
00512 $copy = 'imagecopyresampled';
00513 }
00514 else
00515 {
00516 $create = 'imagecreate';
00517 $copy = 'imagecopyresized';
00518 }
00519
00520 $dst_img = $create($this->width, $this->height);
00521 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
00522
00523
00524 if ($this->dynamic_output == TRUE)
00525 {
00526 $this->image_display_gd($dst_img);
00527 }
00528 else
00529 {
00530
00531 if ( ! $this->image_save_gd($dst_img))
00532 {
00533 return FALSE;
00534 }
00535 }
00536
00537
00538 imagedestroy($dst_img);
00539 imagedestroy($src_img);
00540
00541
00542 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00543
00544 return TRUE;
00545 }
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558 function image_process_imagemagick($action = 'resize')
00559 {
00560
00561 if ($this->library_path == '')
00562 {
00563 $this->set_error('imglib_libpath_invalid');
00564 return FALSE;
00565 }
00566
00567 if ( ! eregi("convert$", $this->library_path))
00568 {
00569 if ( ! eregi("/$", $this->library_path)) $this->library_path .= "/";
00570
00571 $this->library_path .= 'convert';
00572 }
00573
00574
00575 $cmd = $this->library_path." -quality ".$this->quality;
00576
00577 if ($action == 'crop')
00578 {
00579 $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
00580 }
00581 elseif ($action == 'rotate')
00582 {
00583 switch ($this->rotation_angle)
00584 {
00585 case 'hor' : $angle = '-flop';
00586 break;
00587 case 'vrt' : $angle = '-flip';
00588 break;
00589 default : $angle = '-rotate '.$this->rotation_angle;
00590 break;
00591 }
00592
00593 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
00594 }
00595 else
00596 {
00597 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
00598 }
00599
00600 $retval = 1;
00601
00602 @exec($cmd, $output, $retval);
00603
00604
00605 if ($retval > 0)
00606 {
00607 $this->set_error('imglib_image_process_failed');
00608 return FALSE;
00609 }
00610
00611
00612 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00613
00614 return TRUE;
00615 }
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628 function image_process_netpbm($action = 'resize')
00629 {
00630 if ($this->library_path == '')
00631 {
00632 $this->set_error('imglib_libpath_invalid');
00633 return FALSE;
00634 }
00635
00636
00637 switch ($this->image_type)
00638 {
00639 case 1 :
00640 $cmd_in = 'giftopnm';
00641 $cmd_out = 'ppmtogif';
00642 break;
00643 case 2 :
00644 $cmd_in = 'jpegtopnm';
00645 $cmd_out = 'ppmtojpeg';
00646 break;
00647 case 3 :
00648 $cmd_in = 'pngtopnm';
00649 $cmd_out = 'ppmtopng';
00650 break;
00651 }
00652
00653 if ($action == 'crop')
00654 {
00655 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
00656 }
00657 elseif ($action == 'rotate')
00658 {
00659 switch ($this->rotation_angle)
00660 {
00661 case 90 : $angle = 'r270';
00662 break;
00663 case 180 : $angle = 'r180';
00664 break;
00665 case 270 : $angle = 'r90';
00666 break;
00667 case 'vrt' : $angle = 'tb';
00668 break;
00669 case 'hor' : $angle = 'lr';
00670 break;
00671 }
00672
00673 $cmd_inner = 'pnmflip -'.$angle.' ';
00674 }
00675 else
00676 {
00677 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
00678 }
00679
00680 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
00681
00682 $retval = 1;
00683
00684 @exec($cmd, $output, $retval);
00685
00686
00687 if ($retval > 0)
00688 {
00689 $this->set_error('imglib_image_process_failed');
00690 return FALSE;
00691 }
00692
00693
00694
00695
00696 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
00697 unlink ($this->dest_folder.'netpbm.tmp');
00698 @chmod($dst_image, DIR_WRITE_MODE);
00699
00700 return TRUE;
00701 }
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711 function image_rotate_gd()
00712 {
00713
00714
00715 if ( ! function_exists('imagerotate'))
00716 {
00717 $this->set_error('imglib_rotate_unsupported');
00718 return FALSE;
00719 }
00720
00721
00722 if ( ! ($src_img = $this->image_create_gd()))
00723 {
00724 return FALSE;
00725 }
00726
00727
00728
00729
00730
00731
00732 $white = imagecolorallocate($src_img, 255, 255, 255);
00733
00734
00735 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
00736
00737
00738 if ($this->dynamic_output == TRUE)
00739 {
00740 $this->image_display_gd($dst_img);
00741 }
00742 else
00743 {
00744
00745 if ( ! $this->image_save_gd($dst_img))
00746 {
00747 return FALSE;
00748 }
00749 }
00750
00751
00752 imagedestroy($dst_img);
00753 imagedestroy($src_img);
00754
00755
00756
00757 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00758
00759 return true;
00760 }
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772 function image_mirror_gd()
00773 {
00774 if ( ! $src_img = $this->image_create_gd())
00775 {
00776 return FALSE;
00777 }
00778
00779 $width = $this->orig_width;
00780 $height = $this->orig_height;
00781
00782 if ($this->rotation_angle == 'hor')
00783 {
00784 for ($i = 0; $i < $height; $i++)
00785 {
00786 $left = 0;
00787 $right = $width-1;
00788
00789 while ($left < $right)
00790 {
00791 $cl = imagecolorat($src_img, $left, $i);
00792 $cr = imagecolorat($src_img, $right, $i);
00793
00794 imagesetpixel($src_img, $left, $i, $cr);
00795 imagesetpixel($src_img, $right, $i, $cl);
00796
00797 $left++;
00798 $right--;
00799 }
00800 }
00801 }
00802 else
00803 {
00804 for ($i = 0; $i < $width; $i++)
00805 {
00806 $top = 0;
00807 $bot = $height-1;
00808
00809 while ($top < $bot)
00810 {
00811 $ct = imagecolorat($src_img, $i, $top);
00812 $cb = imagecolorat($src_img, $i, $bot);
00813
00814 imagesetpixel($src_img, $i, $top, $cb);
00815 imagesetpixel($src_img, $i, $bot, $ct);
00816
00817 $top++;
00818 $bot--;
00819 }
00820 }
00821 }
00822
00823
00824 if ($this->dynamic_output == TRUE)
00825 {
00826 $this->image_display_gd($src_img);
00827 }
00828 else
00829 {
00830
00831 if ( ! $this->image_save_gd($src_img))
00832 {
00833 return FALSE;
00834 }
00835 }
00836
00837
00838 imagedestroy($src_img);
00839
00840
00841 @chmod($this->full_dst_path, DIR_WRITE_MODE);
00842
00843 return TRUE;
00844 }
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858 function watermark()
00859 {
00860 if ($this->wm_type == 'overlay')
00861 {
00862 return $this->overlay_watermark();
00863 }
00864 else
00865 {
00866 return $this->text_watermark();
00867 }
00868 }
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878 function overlay_watermark()
00879 {
00880 if ( ! function_exists('imagecolortransparent'))
00881 {
00882 $this->set_error('imglib_gd_required');
00883 return FALSE;
00884 }
00885
00886
00887 $this->get_image_properties();
00888
00889
00890 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
00891 $wm_img_type = $props['image_type'];
00892 $wm_width = $props['width'];
00893 $wm_height = $props['height'];
00894
00895
00896 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
00897 $src_img = $this->image_create_gd($this->full_src_path);
00898
00899
00900
00901
00902
00903
00904
00905
00906 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
00907 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
00908
00909 if ($this->wm_vrt_alignment == 'B')
00910 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
00911
00912 if ($this->wm_hor_alignment == 'R')
00913 $this->wm_hor_offset = $this->wm_hor_offset * -1;
00914
00915
00916 $x_axis = $this->wm_hor_offset + $this->wm_padding;
00917 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
00918
00919
00920 switch ($this->wm_vrt_alignment)
00921 {
00922 case 'T':
00923 break;
00924 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
00925 break;
00926 case 'B': $y_axis += $this->orig_height - $wm_height;
00927 break;
00928 }
00929
00930
00931 switch ($this->wm_hor_alignment)
00932 {
00933 case 'L':
00934 break;
00935 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
00936 break;
00937 case 'R': $x_axis += $this->orig_width - $wm_width;
00938 break;
00939 }
00940
00941
00942 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
00943 {
00944 @imagealphablending($src_img, TRUE);
00945 }
00946
00947
00948 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
00949 $alpha = ($rgba & 0x7F000000) >> 24;
00950
00951
00952 if ($alpha > 0)
00953 {
00954
00955 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
00956 }
00957 else
00958 {
00959
00960 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
00961 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
00962 }
00963
00964
00965 if ($this->dynamic_output == TRUE)
00966 {
00967 $this->image_display_gd($src_img);
00968 }
00969 else
00970 {
00971 if ( ! $this->image_save_gd($src_img))
00972 {
00973 return FALSE;
00974 }
00975 }
00976
00977 imagedestroy($src_img);
00978 imagedestroy($wm_img);
00979
00980 return TRUE;
00981 }
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991 function text_watermark()
00992 {
00993 if ( ! ($src_img = $this->image_create_gd()))
00994 {
00995 return FALSE;
00996 }
00997
00998 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
00999 {
01000 $this->set_error('imglib_missing_font');
01001 return FALSE;
01002 }
01003
01004
01005 $this->get_image_properties();
01006
01007
01008 $this->wm_font_color = str_replace('#', '', $this->wm_font_color);
01009 $this->wm_shadow_color = str_replace('#', '', $this->wm_shadow_color);
01010
01011 $R1 = hexdec(substr($this->wm_font_color, 0, 2));
01012 $G1 = hexdec(substr($this->wm_font_color, 2, 2));
01013 $B1 = hexdec(substr($this->wm_font_color, 4, 2));
01014
01015 $R2 = hexdec(substr($this->wm_shadow_color, 0, 2));
01016 $G2 = hexdec(substr($this->wm_shadow_color, 2, 2));
01017 $B2 = hexdec(substr($this->wm_shadow_color, 4, 2));
01018
01019 $txt_color = imagecolorclosest($src_img, $R1, $G1, $B1);
01020 $drp_color = imagecolorclosest($src_img, $R2, $G2, $B2);
01021
01022
01023
01024
01025
01026
01027
01028
01029 if ($this->wm_vrt_alignment == 'B')
01030 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
01031
01032 if ($this->wm_hor_alignment == 'R')
01033 $this->wm_hor_offset = $this->wm_hor_offset * -1;
01034
01035
01036
01037
01038 if ($this->wm_use_truetype == TRUE)
01039 {
01040 if ($this->wm_font_size == '')
01041 $this->wm_font_size = '17';
01042
01043 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
01044 $fontheight = $this->wm_font_size;
01045 $this->wm_vrt_offset += $this->wm_font_size;
01046 }
01047 else
01048 {
01049 $fontwidth = imagefontwidth($this->wm_font_size);
01050 $fontheight = imagefontheight($this->wm_font_size);
01051 }
01052
01053
01054 $x_axis = $this->wm_hor_offset + $this->wm_padding;
01055 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
01056
01057
01058 if ($this->wm_use_drop_shadow == FALSE)
01059 $this->wm_shadow_distance = 0;
01060
01061 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
01062 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
01063
01064 switch ($this->wm_vrt_alignment)
01065 {
01066 case "T" :
01067 break;
01068 case "M": $y_axis += ($this->orig_height/2)+($fontheight/2);
01069 break;
01070 case "B": $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
01071 break;
01072 }
01073
01074 $x_shad = $x_axis + $this->wm_shadow_distance;
01075 $y_shad = $y_axis + $this->wm_shadow_distance;
01076
01077
01078 switch ($this->wm_hor_alignment)
01079 {
01080 case "L":
01081 break;
01082 case "R":
01083 if ($this->wm_use_drop_shadow)
01084 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
01085 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
01086 break;
01087 case "C":
01088 if ($this->wm_use_drop_shadow)
01089 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
01090 $x_axis += floor(($this->orig_width -$fontwidth*strlen($this->wm_text))/2);
01091 break;
01092 }
01093
01094
01095 if ($this->wm_use_truetype)
01096 {
01097 if ($this->wm_use_drop_shadow)
01098 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
01099 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
01100 }
01101 else
01102 {
01103 if ($this->wm_use_drop_shadow)
01104 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
01105 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
01106 }
01107
01108
01109 if ($this->dynamic_output == TRUE)
01110 {
01111 $this->image_display_gd($src_img);
01112 }
01113 else
01114 {
01115 $this->image_save_gd($src_img);
01116 }
01117
01118 imagedestroy($src_img);
01119
01120 return TRUE;
01121 }
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135 function image_create_gd($path = '', $image_type = '')
01136 {
01137 if ($path == '')
01138 $path = $this->full_src_path;
01139
01140 if ($image_type == '')
01141 $image_type = $this->image_type;
01142
01143
01144 switch ($image_type)
01145 {
01146 case 1 :
01147 if ( ! function_exists('imagecreatefromgif'))
01148 {
01149 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
01150 return FALSE;
01151 }
01152
01153 return imagecreatefromgif($path);
01154 break;
01155 case 2 :
01156 if ( ! function_exists('imagecreatefromjpeg'))
01157 {
01158 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
01159 return FALSE;
01160 }
01161
01162 return imagecreatefromjpeg($path);
01163 break;
01164 case 3 :
01165 if ( ! function_exists('imagecreatefrompng'))
01166 {
01167 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
01168 return FALSE;
01169 }
01170
01171 return imagecreatefrompng($path);
01172 break;
01173
01174 }
01175
01176 $this->set_error(array('imglib_unsupported_imagecreate'));
01177 return FALSE;
01178 }
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192 function image_save_gd($resource)
01193 {
01194 switch ($this->image_type)
01195 {
01196 case 1 :
01197 if ( ! function_exists('imagegif'))
01198 {
01199 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
01200 return FALSE;
01201 }
01202
01203 @imagegif($resource, $this->full_dst_path);
01204 break;
01205 case 2 :
01206 if ( ! function_exists('imagejpeg'))
01207 {
01208 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
01209 return FALSE;
01210 }
01211
01212 if (phpversion() == '4.4.1')
01213 {
01214 @touch($this->full_dst_path);
01215 }
01216
01217 @imagejpeg($resource, $this->full_dst_path, $this->quality);
01218 break;
01219 case 3 :
01220 if ( ! function_exists('imagepng'))
01221 {
01222 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
01223 return FALSE;
01224 }
01225
01226 @imagepng($resource, $this->full_dst_path);
01227 break;
01228 default :
01229 $this->set_error(array('imglib_unsupported_imagecreate'));
01230 return FALSE;
01231 break;
01232 }
01233
01234 return TRUE;
01235 }
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246 function image_display_gd($resource)
01247 {
01248 header("Content-Disposition: filename={$this->source_image};");
01249 header("Content-Type: {$this->mime_type}");
01250 header('Content-Transfer-Encoding: binary');
01251 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
01252
01253 switch ($this->image_type)
01254 {
01255 case 1 : imagegif($resource);
01256 break;
01257 case 2 : imagejpeg($resource, '', $this->quality);
01258 break;
01259 case 3 : imagepng($resource);
01260 break;
01261 default : echo 'Unable to display the image';
01262 break;
01263 }
01264 }
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281 function image_reproportion()
01282 {
01283 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
01284 return;
01285
01286 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
01287 return;
01288
01289 $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
01290 $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
01291
01292 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
01293
01294 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
01295 {
01296 $this->master_dim = ($ratio < 0) ? 'width' : 'height';
01297 }
01298
01299 if (($this->width != $new_width) AND ($this->height != $new_height))
01300 {
01301 if ($this->master_dim == 'height')
01302 {
01303 $this->width = $new_width;
01304 }
01305 else
01306 {
01307 $this->height = $new_height;
01308 }
01309 }
01310 }
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321
01322
01323 function get_image_properties($path = '', $return = FALSE)
01324 {
01325
01326
01327
01328 if ($path == '')
01329 $path = $this->full_src_path;
01330
01331 if ( ! file_exists($path))
01332 {
01333 $this->set_error('imglib_invalid_path');
01334 return FALSE;
01335 }
01336
01337 $vals = @getimagesize($path);
01338
01339 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
01340
01341 $mime = (isset($types[$vals['2']])) ? 'image/'.$types[$vals['2']] : 'image/jpg';
01342
01343 if ($return == TRUE)
01344 {
01345 $v['width'] = $vals['0'];
01346 $v['height'] = $vals['1'];
01347 $v['image_type'] = $vals['2'];
01348 $v['size_str'] = $vals['3'];
01349 $v['mime_type'] = $mime;
01350
01351 return $v;
01352 }
01353
01354 $this->orig_width = $vals['0'];
01355 $this->orig_height = $vals['1'];
01356 $this->image_type = $vals['2'];
01357 $this->size_str = $vals['3'];
01358 $this->mime_type = $mime;
01359
01360 return TRUE;
01361 }
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383 function size_calculator($vals)
01384 {
01385 if ( ! is_array($vals))
01386 return;
01387
01388 $allowed = array('new_width', 'new_height', 'width', 'height');
01389
01390 foreach ($allowed as $item)
01391 {
01392 if ( ! isset($vals[$item]) OR $vals[$item] == '')
01393 $vals[$item] = 0;
01394 }
01395
01396 if ($vals['width'] == 0 OR $vals['height'] == 0)
01397 {
01398 return $vals;
01399 }
01400
01401 if ($vals['new_width'] == 0)
01402 {
01403 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
01404 }
01405 elseif ($vals['new_height'] == 0)
01406 {
01407 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
01408 }
01409
01410 return $vals;
01411 }
01412
01413
01414
01415
01416
01417
01418
01419
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429 function explode_name($source_image)
01430 {
01431 $x = explode('.', $source_image);
01432 $ret['ext'] = '.'.end($x);
01433
01434 $name = '';
01435
01436 $ct = count($x)-1;
01437
01438 for ($i = 0; $i < $ct; $i++)
01439 {
01440 $name .= $x[$i];
01441
01442 if ($i < ($ct - 1))
01443 {
01444 $name .= '.';
01445 }
01446 }
01447
01448 $ret['name'] = $name;
01449
01450 return $ret;
01451 }
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461 function gd_loaded()
01462 {
01463 if ( ! extension_loaded('gd'))
01464 {
01465 if ( ! dl('gd.so'))
01466 {
01467 return FALSE;
01468 }
01469 }
01470
01471 return TRUE;
01472 }
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482 function gd_version()
01483 {
01484 if (function_exists('gd_info'))
01485 {
01486 $gd_version = @gd_info();
01487 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
01488
01489 return $gd_version;
01490 }
01491
01492 return FALSE;
01493 }
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504 function set_error($msg)
01505 {
01506 $CI =& get_instance();
01507 $CI->lang->load('imglib');
01508
01509 if (is_array($msg))
01510 {
01511 foreach ($msg as $val)
01512 {
01513
01514 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
01515 $this->error_msg[] = $msg;
01516 log_message('error', $msg);
01517 }
01518 }
01519 else
01520 {
01521 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
01522 $this->error_msg[] = $msg;
01523 log_message('error', $msg);
01524 }
01525 }
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536 function display_errors($open = '<p>', $close = '</p>')
01537 {
01538 $str = '';
01539 foreach ($this->error_msg as $val)
01540 {
01541 $str .= $open.$val.$close;
01542 }
01543
01544 return $str;
01545 }
01546
01547 }
01548
01549
01550
01551