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