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