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