blob: dbb6f82e066b5bcb5319f8290f7848a48ae3f516 [file] [log] [blame]
Derek Allardc5d88792007-02-01 03:12:32 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allardc5d88792007-02-01 03:12:32 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardc5d88792007-02-01 03:12:32 +000012 * @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
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000025 * @link http://codeigniter.com/user_guide/libraries/image_lib.html
Derek Allardc5d88792007-02-01 03:12:32 +000026 */
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 = '';
Derek Allard44dbc782008-01-29 20:38:55 +0000113 }
114
115 // special consideration for master_dim
116 $this->master_dim = 'auto';
Derek Allardc5d88792007-02-01 03:12:32 +0000117 }
118
119 // --------------------------------------------------------------------
120
121 /**
122 * initialize image preferences
123 *
124 * @access public
125 * @param array
126 * @return void
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
400 if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs, TRUE))
401 {
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 ($action == 'crop')
453 {
454 // If the target width/height match the source then it's pointless to crop, right?
Derek Allard1d3137b2008-01-29 18:44:07 +0000455 // So if dynamic output isn't on, then we'll return true so the user thinks
456 // the process succeeded. It'll be our little secret...
457
458 if ($this->width >= $this->orig_width AND $this->height >= $this->orig_height AND $this->dynamic_output !== TRUE)
Derek Allardc5d88792007-02-01 03:12:32 +0000459 {
Derek Allardc5d88792007-02-01 03:12:32 +0000460 return TRUE;
461 }
Derek Allard1d3137b2008-01-29 18:44:07 +0000462
Derek Allardc5d88792007-02-01 03:12:32 +0000463 // Reassign the source width/height if cropping
464 $this->orig_width = $this->width;
465 $this->orig_height = $this->height;
466
467 // GD 2.0 has a cropping bug so we'll test for it
468 if ($this->gd_version() !== FALSE)
469 {
470 $gd_version = str_replace('0', '', $this->gd_version());
471 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
472 }
473 }
474 else
475 {
476 // If the target width/height match the source, AND if
477 // the new file name is not equal to the old file name
478 // we'll simply make a copy of the original with the new name
Derek Allard1d3137b2008-01-29 18:44:07 +0000479 if (($this->orig_width == $this->width AND $this->orig_height == $this->height) AND ($this->source_image != $this->new_image))
Derek Allardc5d88792007-02-01 03:12:32 +0000480 {
481 if ( ! @copy($this->full_src_path, $this->full_dst_path))
482 {
483 $this->set_error('imglib_copy_failed');
484 return FALSE;
485 }
486
487 @chmod($this->full_dst_path, 0777);
488 return TRUE;
489 }
490
491 // If resizing the x/y axis must be zero
492 $this->x_axis = 0;
493 $this->y_axis = 0;
494 }
495
496 // Create the image handle
497 if ( ! ($src_img = $this->image_create_gd()))
498 {
499 return FALSE;
500 }
501
502 // Create The Image
Derek Allard1d3137b2008-01-29 18:44:07 +0000503 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
Derek Allardc5d88792007-02-01 03:12:32 +0000504 {
505 $create = 'imagecreatetruecolor';
506 $copy = 'imagecopyresampled';
507 }
508 else
509 {
510 $create = 'imagecreate';
511 $copy = 'imagecopyresized';
512 }
513
514 $dst_img = $create($this->width, $this->height);
515 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
516
517 // Show the image
518 if ($this->dynamic_output == TRUE)
519 {
520 $this->image_display_gd($dst_img);
521 }
522 else
523 {
524 // Or save it
525 if ( ! $this->image_save_gd($dst_img))
526 {
527 return FALSE;
528 }
529 }
530
531 // Kill the file handles
532 imagedestroy($dst_img);
533 imagedestroy($src_img);
534
535 // Set the file to 777
536 @chmod($this->full_dst_path, 0777);
537
538 return TRUE;
539 }
540
541 // --------------------------------------------------------------------
542
543 /**
544 * Image Process Using ImageMagick
545 *
546 * This function will resize, crop or rotate
547 *
548 * @access public
549 * @param string
550 * @return bool
551 */
552 function image_process_imagemagick($action = 'resize')
553 {
554 // Do we have a vaild library path?
555 if ($this->library_path == '')
556 {
557 $this->set_error('imglib_libpath_invalid');
558 return FALSE;
559 }
560
561 if ( ! eregi("convert$", $this->library_path))
562 {
563 if ( ! eregi("/$", $this->library_path)) $this->library_path .= "/";
564
565 $this->library_path .= 'convert';
566 }
567
568 // Execute the command
569 $cmd = $this->library_path." -quality ".$this->quality;
570
571 if ($action == 'crop')
572 {
573 $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
574 }
575 elseif ($action == 'rotate')
576 {
577 switch ($this->rotation_angle)
578 {
579 case 'hor' : $angle = '-flop';
580 break;
581 case 'vrt' : $angle = '-flip';
582 break;
583 default : $angle = '-rotate '.$this->rotation_angle;
584 break;
585 }
586
587 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
588 }
589 else // Resize
590 {
591 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
592 }
593
594 $retval = 1;
595
596 @exec($cmd, $output, $retval);
597
598 // Did it work?
599 if ($retval > 0)
600 {
601 $this->set_error('imglib_image_process_failed');
602 return FALSE;
603 }
604
605 // Set the file to 777
606 @chmod($this->full_dst_path, 0777);
607
608 return TRUE;
609 }
610
611 // --------------------------------------------------------------------
612
613 /**
614 * Image Process Using NetPBM
615 *
616 * This function will resize, crop or rotate
617 *
618 * @access public
619 * @param string
620 * @return bool
621 */
622 function image_process_netpbm($action = 'resize')
623 {
624 if ($this->library_path == '')
625 {
626 $this->set_error('imglib_libpath_invalid');
627 return FALSE;
628 }
629
630 // Build the resizing command
631 switch ($this->image_type)
632 {
633 case 1 :
634 $cmd_in = 'giftopnm';
635 $cmd_out = 'ppmtogif';
636 break;
637 case 2 :
638 $cmd_in = 'jpegtopnm';
639 $cmd_out = 'ppmtojpeg';
640 break;
641 case 3 :
642 $cmd_in = 'pngtopnm';
643 $cmd_out = 'ppmtopng';
644 break;
645 }
646
647 if ($action == 'crop')
648 {
649 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
650 }
651 elseif ($action == 'rotate')
652 {
653 switch ($this->rotation_angle)
654 {
655 case 90 : $angle = 'r270';
656 break;
657 case 180 : $angle = 'r180';
658 break;
659 case 270 : $angle = 'r90';
660 break;
661 case 'vrt' : $angle = 'tb';
662 break;
663 case 'hor' : $angle = 'lr';
664 break;
665 }
666
667 $cmd_inner = 'pnmflip -'.$angle.' ';
668 }
669 else // Resize
670 {
671 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
672 }
673
674 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
675
676 $retval = 1;
677
678 @exec($cmd, $output, $retval);
679
680 // Did it work?
681 if ($retval > 0)
682 {
683 $this->set_error('imglib_image_process_failed');
684 return FALSE;
685 }
686
687 // With NetPBM we have to create a temporary image.
688 // If you try manipulating the original it fails so
689 // we have to rename the temp file.
690 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
691 unlink ($this->dest_folder.'netpbm.tmp');
Derek Allard44dbc782008-01-29 20:38:55 +0000692 @chmod($dst_image, 0777);
Derek Allardc5d88792007-02-01 03:12:32 +0000693
694 return TRUE;
695 }
696
697 // --------------------------------------------------------------------
698
699 /**
700 * Image Rotate Using GD
701 *
702 * @access public
703 * @return bool
704 */
705 function image_rotate_gd()
706 {
707 // Is Image Rotation Supported?
708 // this function is only supported as of PHP 4.3
709 if ( ! function_exists('imagerotate'))
710 {
711 $this->set_error('imglib_rotate_unsupported');
712 return FALSE;
713 }
714
715 // Create the image handle
716 if ( ! ($src_img = $this->image_create_gd()))
717 {
718 return FALSE;
719 }
720
721 // Set the background color
722 // This won't work with transparent PNG files so we are
723 // going to have to figure out how to determine the color
724 // of the alpha channel in a future release.
725
726 $white = imagecolorallocate($src_img, 255, 255, 255);
727
728 // Rotate it!
729 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
730
731 // Save the Image
732 if ($this->dynamic_output == TRUE)
733 {
734 $this->image_display_gd($dst_img);
735 }
736 else
737 {
738 // Or save it
739 if ( ! $this->image_save_gd($dst_img))
740 {
741 return FALSE;
742 }
743 }
744
745 // Kill the file handles
746 imagedestroy($dst_img);
747 imagedestroy($src_img);
748
749 // Set the file to 777
750
751 @chmod($this->full_dst_path, 0777);
752
753 return true;
754 }
755
756 // --------------------------------------------------------------------
757
758 /**
759 * Create Mirror Image using GD
760 *
761 * This function will flip horizontal or vertical
762 *
763 * @access public
764 * @return bool
765 */
766 function image_mirror_gd()
767 {
768 if ( ! $src_img = $this->image_create_gd())
769 {
770 return FALSE;
771 }
772
773 $width = $this->orig_width;
774 $height = $this->orig_height;
775
776 if ($this->rotation_angle == 'hor')
777 {
778 for ($i = 0; $i < $height; $i++)
779 {
780 $left = 0;
781 $right = $width-1;
782
783 while ($left < $right)
784 {
785 $cl = imagecolorat($src_img, $left, $i);
786 $cr = imagecolorat($src_img, $right, $i);
787
788 imagesetpixel($src_img, $left, $i, $cr);
789 imagesetpixel($src_img, $right, $i, $cl);
790
791 $left++;
792 $right--;
793 }
794 }
795 }
796 else
797 {
798 for ($i = 0; $i < $width; $i++)
799 {
800 $top = 0;
801 $bot = $height-1;
802
803 while ($top < $bot)
804 {
805 $ct = imagecolorat($src_img, $i, $top);
806 $cb = imagecolorat($src_img, $i, $bot);
807
808 imagesetpixel($src_img, $i, $top, $cb);
809 imagesetpixel($src_img, $i, $bot, $ct);
810
811 $top++;
812 $bot--;
813 }
814 }
815 }
816
817 // Show the image
818 if ($this->dynamic_output == TRUE)
819 {
820 $this->image_display_gd($src_img);
821 }
822 else
823 {
824 // Or save it
825 if ( ! $this->image_save_gd($src_img))
826 {
827 return FALSE;
828 }
829 }
830
831 // Kill the file handles
832 imagedestroy($src_img);
833
834 // Set the file to 777
835 @chmod($this->full_dst_path, 0777);
836
837 return TRUE;
838 }
839
840 // --------------------------------------------------------------------
841
842 /**
843 * Image Watermark
844 *
845 * This is a wrapper function that chooses the type
846 * of watermarking based on the specified preference.
847 *
848 * @access public
849 * @param string
850 * @return bool
851 */
852 function watermark()
853 {
854 if ($this->wm_type == 'overlay')
855 {
856 return $this->overlay_watermark();
857 }
858 else
859 {
860 return $this->text_watermark();
861 }
862 }
863
864 // --------------------------------------------------------------------
865
866 /**
867 * Watermark - Graphic Version
868 *
869 * @access public
870 * @return bool
871 */
872 function overlay_watermark()
873 {
874 if ( ! function_exists('imagecolortransparent'))
875 {
876 $this->set_error('imglib_gd_required');
877 return FALSE;
878 }
879
880 // Fetch source image properties
881 $this->get_image_properties();
882
883 // Fetch watermark image properties
884 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
885 $wm_img_type = $props['image_type'];
886 $wm_width = $props['width'];
887 $wm_height = $props['height'];
888
889 // Create two image resources
890 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
891 $src_img = $this->image_create_gd($this->full_src_path);
892
893 // Reverse the offset if necessary
894 // When the image is positioned at the bottom
895 // we don't want the vertical offset to push it
896 // further down. We want the reverse, so we'll
897 // invert the offset. Same with the horizontal
898 // offset when the image is at the right
899
900 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
901 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
902
903 if ($this->wm_vrt_alignment == 'B')
904 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
905
906 if ($this->wm_hor_alignment == 'R')
907 $this->wm_hor_offset = $this->wm_hor_offset * -1;
908
909 // Set the base x and y axis values
910 $x_axis = $this->wm_hor_offset + $this->wm_padding;
911 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
912
913 // Set the vertical position
914 switch ($this->wm_vrt_alignment)
915 {
916 case 'T':
917 break;
918 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
919 break;
920 case 'B': $y_axis += $this->orig_height - $wm_height;
921 break;
922 }
923
924 // Set the horizontal position
925 switch ($this->wm_hor_alignment)
926 {
927 case 'L':
928 break;
929 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
930 break;
931 case 'R': $x_axis += $this->orig_width - $wm_width;
932 break;
933 }
934
935 // Build the finalized image
936 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
937 {
938 @imagealphablending($src_img, TRUE);
939 }
940
941 // Set RGB values for text and shadow
942 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
943 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
944
945 // Output the image
946 if ($this->dynamic_output == TRUE)
947 {
948 $this->image_display_gd($src_img);
949 }
950 else
951 {
952 if ( ! $this->image_save_gd($src_img))
953 {
954 return FALSE;
955 }
956 }
957
958 imagedestroy($src_img);
959 imagedestroy($wm_img);
960
961 return TRUE;
962 }
963
964 // --------------------------------------------------------------------
965
966 /**
967 * Watermark - Text Version
968 *
969 * @access public
970 * @return bool
971 */
972 function text_watermark()
973 {
974 if ( ! ($src_img = $this->image_create_gd()))
975 {
976 return FALSE;
977 }
978
979 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
980 {
981 $this->set_error('imglib_missing_font');
982 return FALSE;
983 }
984
985 // Fetch source image properties
986 $this->get_image_properties();
987
988 // Set RGB values for text and shadow
989 $this->wm_font_color = str_replace('#', '', $this->wm_font_color);
990 $this->wm_shadow_color = str_replace('#', '', $this->wm_shadow_color);
991
992 $R1 = hexdec(substr($this->wm_font_color, 0, 2));
993 $G1 = hexdec(substr($this->wm_font_color, 2, 2));
994 $B1 = hexdec(substr($this->wm_font_color, 4, 2));
995
996 $R2 = hexdec(substr($this->wm_shadow_color, 0, 2));
997 $G2 = hexdec(substr($this->wm_shadow_color, 2, 2));
998 $B2 = hexdec(substr($this->wm_shadow_color, 4, 2));
999
1000 $txt_color = imagecolorclosest($src_img, $R1, $G1, $B1);
1001 $drp_color = imagecolorclosest($src_img, $R2, $G2, $B2);
1002
1003 // Reverse the vertical offset
1004 // When the image is positioned at the bottom
1005 // we don't want the vertical offset to push it
1006 // further down. We want the reverse, so we'll
1007 // invert the offset. Note: The horizontal
1008 // offset flips itself automatically
1009
1010 if ($this->wm_vrt_alignment == 'B')
1011 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1012
1013 if ($this->wm_hor_alignment == 'R')
1014 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1015
1016 // Set font width and height
1017 // These are calculated differently depending on
1018 // whether we are using the true type font or not
1019 if ($this->wm_use_truetype == TRUE)
1020 {
1021 if ($this->wm_font_size == '')
1022 $this->wm_font_size = '17';
1023
1024 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
1025 $fontheight = $this->wm_font_size;
1026 $this->wm_vrt_offset += $this->wm_font_size;
1027 }
1028 else
1029 {
1030 $fontwidth = imagefontwidth($this->wm_font_size);
1031 $fontheight = imagefontheight($this->wm_font_size);
1032 }
1033
1034 // Set base X and Y axis values
1035 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1036 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1037
1038 // Set verticle alignment
1039 if ($this->wm_use_drop_shadow == FALSE)
1040 $this->wm_shadow_distance = 0;
1041
1042 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1043 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1044
1045 switch ($this->wm_vrt_alignment)
1046 {
1047 case "T" :
1048 break;
1049 case "M": $y_axis += ($this->orig_height/2)+($fontheight/2);
1050 break;
1051 case "B": $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
1052 break;
1053 }
1054
1055 $x_shad = $x_axis + $this->wm_shadow_distance;
1056 $y_shad = $y_axis + $this->wm_shadow_distance;
1057
1058 // Set horizontal alignment
1059 switch ($this->wm_hor_alignment)
1060 {
1061 case "L":
1062 break;
1063 case "R":
1064 if ($this->wm_use_drop_shadow)
1065 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1066 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1067 break;
1068 case "C":
1069 if ($this->wm_use_drop_shadow)
1070 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1071 $x_axis += floor(($this->orig_width -$fontwidth*strlen($this->wm_text))/2);
1072 break;
1073 }
1074
1075 // Add the text to the source image
1076 if ($this->wm_use_truetype)
1077 {
1078 if ($this->wm_use_drop_shadow)
1079 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1080 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1081 }
1082 else
1083 {
1084 if ($this->wm_use_drop_shadow)
1085 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1086 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1087 }
1088
1089 // Output the final image
1090 if ($this->dynamic_output == TRUE)
1091 {
1092 $this->image_display_gd($src_img);
1093 }
1094 else
1095 {
1096 $this->image_save_gd($src_img);
1097 }
1098
1099 imagedestroy($src_img);
1100
1101 return TRUE;
1102 }
1103
1104 // --------------------------------------------------------------------
1105
1106 /**
1107 * Create Image - GD
1108 *
1109 * This simply creates an image resource handle
1110 * based on the type of image being processed
1111 *
1112 * @access public
1113 * @param string
1114 * @return resource
1115 */
1116 function image_create_gd($path = '', $image_type = '')
1117 {
1118 if ($path == '')
1119 $path = $this->full_src_path;
1120
1121 if ($image_type == '')
1122 $image_type = $this->image_type;
1123
1124
1125 switch ($image_type)
1126 {
1127 case 1 :
1128 if ( ! function_exists('imagecreatefromgif'))
1129 {
1130 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1131 return FALSE;
1132 }
1133
1134 return imagecreatefromgif($path);
1135 break;
1136 case 2 :
1137 if ( ! function_exists('imagecreatefromjpeg'))
1138 {
1139 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1140 return FALSE;
1141 }
1142
1143 return imagecreatefromjpeg($path);
1144 break;
1145 case 3 :
1146 if ( ! function_exists('imagecreatefrompng'))
1147 {
1148 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1149 return FALSE;
1150 }
1151
1152 return imagecreatefrompng($path);
1153 break;
1154
1155 }
1156
1157 $this->set_error(array('imglib_unsupported_imagecreate'));
1158 return FALSE;
1159 }
1160
1161 // --------------------------------------------------------------------
1162
1163 /**
1164 * Write image file to disk - GD
1165 *
1166 * Takes an image resource as input and writes the file
1167 * to the specified destination
1168 *
1169 * @access public
1170 * @param resource
1171 * @return bool
1172 */
1173 function image_save_gd($resource)
1174 {
1175 switch ($this->image_type)
1176 {
1177 case 1 :
1178 if ( ! function_exists('imagegif'))
1179 {
1180 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1181 return FALSE;
1182 }
1183
1184 @imagegif($resource, $this->full_dst_path);
1185 break;
1186 case 2 :
1187 if ( ! function_exists('imagejpeg'))
1188 {
1189 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1190 return FALSE;
1191 }
1192
1193 if (phpversion() == '4.4.1')
1194 {
1195 @touch($this->full_dst_path); // PHP 4.4.1 bug #35060 - workaround
1196 }
1197
1198 @imagejpeg($resource, $this->full_dst_path, $this->quality);
1199 break;
1200 case 3 :
1201 if ( ! function_exists('imagepng'))
1202 {
1203 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1204 return FALSE;
1205 }
1206
1207 @imagepng($resource, $this->full_dst_path);
1208 break;
1209 default :
1210 $this->set_error(array('imglib_unsupported_imagecreate'));
1211 return FALSE;
1212 break;
1213 }
1214
1215 return TRUE;
1216 }
1217
1218 // --------------------------------------------------------------------
1219
1220 /**
1221 * Dynamically outputs an image
1222 *
1223 * @access public
1224 * @param resource
1225 * @return void
1226 */
1227 function image_display_gd($resource)
1228 {
1229 header("Content-Disposition: filename={$this->source_image};");
1230 header("Content-Type: {$this->mime_type}");
1231 header('Content-Transfer-Encoding: binary');
1232 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1233
1234 switch ($this->image_type)
1235 {
1236 case 1 : imagegif($resource);
1237 break;
1238 case 2 : imagejpeg($resource, '', $this->quality);
1239 break;
1240 case 3 : imagepng($resource);
1241 break;
1242 default : echo 'Unable to display the image';
1243 break;
1244 }
1245 }
1246
1247 // --------------------------------------------------------------------
1248
1249 /**
1250 * Re-proportion Image Width/Height
1251 *
1252 * When creating thumbs, the desired width/height
1253 * can end up warping the image due to an incorrect
1254 * ratio between the full-sized image and the thumb.
1255 *
1256 * This function lets us re-proportion the width/height
1257 * if users choose to maintain the aspect ratio when resizing.
1258 *
1259 * @access public
1260 * @return void
1261 */
1262 function image_reproportion()
1263 {
1264 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
1265 return;
1266
1267 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
1268 return;
1269
1270 $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
1271 $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
1272
1273 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
1274
1275 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
1276 {
1277 $this->master_dim = ($ratio < 0) ? 'width' : 'height';
1278 }
1279
1280 if (($this->width != $new_width) AND ($this->height != $new_height))
1281 {
1282 if ($this->master_dim == 'height')
1283 {
1284 $this->width = $new_width;
1285 }
1286 else
1287 {
1288 $this->height = $new_height;
1289 }
1290 }
1291 }
1292
1293 // --------------------------------------------------------------------
1294
1295 /**
1296 * Get image properties
1297 *
1298 * A helper function that gets info about the file
1299 *
1300 * @access public
1301 * @param string
1302 * @return mixed
1303 */
1304 function get_image_properties($path = '', $return = FALSE)
1305 {
1306 // For now we require GD but we should
1307 // find a way to determine this using IM or NetPBM
1308
1309 if ($path == '')
1310 $path = $this->full_src_path;
1311
1312 if ( ! file_exists($path))
1313 {
1314 $this->set_error('imglib_invalid_path');
1315 return FALSE;
1316 }
1317
1318 $vals = @getimagesize($path);
1319
1320 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
1321
1322 $mime = (isset($types[$vals['2']])) ? 'image/'.$types[$vals['2']] : 'image/jpg';
1323
1324 if ($return == TRUE)
1325 {
1326 $v['width'] = $vals['0'];
1327 $v['height'] = $vals['1'];
1328 $v['image_type'] = $vals['2'];
1329 $v['size_str'] = $vals['3'];
1330 $v['mime_type'] = $mime;
1331
1332 return $v;
1333 }
1334
1335 $this->orig_width = $vals['0'];
1336 $this->orig_height = $vals['1'];
1337 $this->image_type = $vals['2'];
1338 $this->size_str = $vals['3'];
1339 $this->mime_type = $mime;
1340
1341 return TRUE;
1342 }
1343
1344 // --------------------------------------------------------------------
1345
1346 /**
1347 * Size calculator
1348 *
1349 * This function takes a known width x height and
1350 * recalculates it to a new size. Only one
1351 * new variable needs to be known
1352 *
1353 * $props = array(
1354 * 'width' => $width,
1355 * 'height' => $height,
1356 * 'new_width' => 40,
1357 * 'new_height' => ''
1358 * );
1359 *
1360 * @access public
1361 * @param array
1362 * @return array
1363 */
1364 function size_calculator($vals)
1365 {
1366 if ( ! is_array($vals))
1367 return;
1368
1369 $allowed = array('new_width', 'new_height', 'width', 'height');
1370
1371 foreach ($allowed as $item)
1372 {
1373 if ( ! isset($vals[$item]) OR $vals[$item] == '')
1374 $vals[$item] = 0;
1375 }
1376
1377 if ($vals['width'] == 0 OR $vals['height'] == 0)
1378 {
1379 return $vals;
1380 }
1381
1382 if ($vals['new_width'] == 0)
1383 {
1384 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1385 }
1386 elseif ($vals['new_height'] == 0)
1387 {
1388 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1389 }
1390
1391 return $vals;
1392 }
1393
1394 // --------------------------------------------------------------------
1395
1396 /**
1397 * Explode source_image
1398 *
1399 * This is a helper function that extracts the extension
1400 * from the source_image. This function lets us deal with
1401 * source_images with multiple periods, like: my.cool.jpg
1402 * It returns an associative array with two elements:
1403 * $array['ext'] = '.jpg';
1404 * $array['name'] = 'my.cool';
1405 *
1406 * @access public
1407 * @param array
1408 * @return array
1409 */
1410 function explode_name($source_image)
1411 {
1412 $x = explode('.', $source_image);
1413 $ret['ext'] = '.'.end($x);
1414
1415 $name = '';
1416
1417 $ct = count($x)-1;
1418
1419 for ($i = 0; $i < $ct; $i++)
1420 {
1421 $name .= $x[$i];
1422
1423 if ($i < ($ct - 1))
1424 {
1425 $name .= '.';
1426 }
1427 }
1428
1429 $ret['name'] = $name;
1430
1431 return $ret;
1432 }
1433
1434 // --------------------------------------------------------------------
1435
1436 /**
1437 * Is GD Installed?
1438 *
1439 * @access public
1440 * @return bool
1441 */
1442 function gd_loaded()
1443 {
1444 if ( ! extension_loaded('gd'))
1445 {
1446 if ( ! dl('gd.so'))
1447 {
1448 return FALSE;
1449 }
1450 }
1451
1452 return TRUE;
1453 }
1454
1455 // --------------------------------------------------------------------
1456
1457 /**
1458 * Get GD version
1459 *
1460 * @access public
1461 * @return mixed
1462 */
1463 function gd_version()
1464 {
1465 if (function_exists('gd_info'))
1466 {
1467 $gd_version = @gd_info();
1468 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
1469
1470 return $gd_version;
1471 }
1472
1473 return FALSE;
1474 }
1475
1476 // --------------------------------------------------------------------
1477
1478 /**
1479 * Set error message
1480 *
1481 * @access public
1482 * @param string
1483 * @return void
1484 */
1485 function set_error($msg)
1486 {
1487 $CI =& get_instance();
1488 $CI->lang->load('imglib');
1489
1490 if (is_array($msg))
1491 {
1492 foreach ($msg as $val)
1493 {
1494
1495 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1496 $this->error_msg[] = $msg;
1497 log_message('error', $msg);
1498 }
1499 }
1500 else
1501 {
1502 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1503 $this->error_msg[] = $msg;
1504 log_message('error', $msg);
1505 }
1506 }
1507
1508 // --------------------------------------------------------------------
1509
1510 /**
1511 * Show error messages
1512 *
1513 * @access public
1514 * @param string
1515 * @return string
1516 */
1517 function display_errors($open = '<p>', $close = '</p>')
1518 {
1519 $str = '';
1520 foreach ($this->error_msg as $val)
1521 {
1522 $str .= $open.$val.$close;
1523 }
1524
1525 return $str;
1526 }
1527
1528}
1529// END Image_lib Class
adminb0dd10f2006-08-25 17:25:49 +00001530?>