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