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