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