blob: 20ca1f0553519d2e9816329f468e6f92565a3b4a [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnesb1673362011-12-05 22:05:38 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnesb1673362011-12-05 22:05:38 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Image Manipulation class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Image_lib
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/image_lib.html
38 */
39class CI_Image_lib {
40
Andrey Andreev3a459572011-12-21 11:23:11 +020041 public $image_library = 'gd2'; // Can be: imagemagick, netpbm, gd, gd2
42 public $library_path = '';
43 public $dynamic_output = FALSE; // Whether to send to browser or write to disk
44 public $source_image = '';
45 public $new_image = '';
46 public $width = '';
47 public $height = '';
48 public $quality = '90';
49 public $create_thumb = FALSE;
50 public $thumb_marker = '_thumb';
51 public $maintain_ratio = TRUE; // Whether to maintain aspect ratio when resizing or use hard values
52 public $master_dim = 'auto'; // auto, height, or width. Determines what to use as the master dimension
53 public $rotation_angle = '';
54 public $x_axis = '';
55 public $y_axis = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000056
57 // Watermark Vars
Andrey Andreev3a459572011-12-21 11:23:11 +020058 public $wm_text = ''; // Watermark text if graphic is not used
59 public $wm_type = 'text'; // Type of watermarking. Options: text/overlay
60 public $wm_x_transp = 4;
61 public $wm_y_transp = 4;
62 public $wm_overlay_path = ''; // Watermark image path
63 public $wm_font_path = ''; // TT font
64 public $wm_font_size = 17; // Font size (different versions of GD will either use points or pixels)
65 public $wm_vrt_alignment = 'B'; // Vertical alignment: T M B
66 public $wm_hor_alignment = 'C'; // Horizontal alignment: L R C
67 public $wm_padding = 0; // Padding around text
68 public $wm_hor_offset = 0; // Lets you push text to the right
69 public $wm_vrt_offset = 0; // Lets you push text down
70 public $wm_font_color = '#ffffff'; // Text color
71 public $wm_shadow_color = ''; // Dropshadow color
72 public $wm_shadow_distance = 2; // Dropshadow distance
73 public $wm_opacity = 50; // Image opacity: 1 - 100 Only works with image
Derek Allard2067d1a2008-11-13 22:59:24 +000074
75 // Private Vars
Andrey Andreev3a459572011-12-21 11:23:11 +020076 public $source_folder = '';
77 public $dest_folder = '';
78 public $mime_type = '';
79 public $orig_width = '';
80 public $orig_height = '';
81 public $image_type = '';
82 public $size_str = '';
83 public $full_src_path = '';
84 public $full_dst_path = '';
85 public $create_fnc = 'imagecreatetruecolor';
86 public $copy_fnc = 'imagecopyresampled';
87 public $error_msg = array();
88 public $wm_use_drop_shadow = FALSE;
89 public $wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000090
91 /**
92 * Constructor
93 *
Derek Allard2067d1a2008-11-13 22:59:24 +000094 * @param string
95 * @return void
96 */
Greg Akera9263282010-11-10 15:26:43 -060097 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
99 if (count($props) > 0)
100 {
101 $this->initialize($props);
102 }
103
104 log_message('debug', "Image Lib Class Initialized");
105 }
106
107 // --------------------------------------------------------------------
108
109 /**
110 * Initialize image properties
111 *
112 * Resets values in case this class is used in a loop
113 *
114 * @access public
115 * @return void
116 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200117 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Michael Denniscb07a322011-08-20 23:40:59 -0700119 $props = array('library_path', 'source_image', 'new_image', 'width', 'height', 'rotation_angle', 'x_axis', 'y_axis', 'wm_text', 'wm_overlay_path', 'wm_font_path', 'wm_shadow_color', 'source_folder', 'dest_folder', 'mime_type', 'orig_width', 'orig_height', 'image_type', 'size_str', 'full_src_path', 'full_dst_path');
Derek Allard2067d1a2008-11-13 22:59:24 +0000120
121 foreach ($props as $val)
122 {
123 $this->$val = '';
124 }
125
Michael Denniscb07a322011-08-20 23:40:59 -0700126 $this->image_library = 'gd2';
127 $this->dynamic_output = FALSE;
128 $this->quality = '90';
129 $this->create_thumb = FALSE;
130 $this->thumb_marker = '_thumb';
131 $this->maintain_ratio = TRUE;
132 $this->master_dim = 'auto';
133 $this->wm_type = 'text';
134 $this->wm_x_transp = 4;
135 $this->wm_y_transp = 4;
136 $this->wm_font_size = 17;
137 $this->wm_vrt_alignment = 'B';
138 $this->wm_hor_alignment = 'C';
139 $this->wm_padding = 0;
140 $this->wm_hor_offset = 0;
141 $this->wm_vrt_offset = 0;
142 $this->wm_font_color = '#ffffff';
143 $this->wm_shadow_distance = 2;
144 $this->wm_opacity = 50;
145 $this->create_fnc = 'imagecreatetruecolor';
146 $this->copy_fnc = 'imagecopyresampled';
147 $this->error_msg = array();
148 $this->wm_use_drop_shadow = FALSE;
149 $this->wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * initialize image preferences
156 *
157 * @access public
158 * @param array
159 * @return bool
160 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200161 public function initialize($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
163 /*
164 * Convert array elements into class variables
165 */
166 if (count($props) > 0)
167 {
168 foreach ($props as $key => $val)
169 {
170 $this->$key = $val;
171 }
172 }
173
174 /*
175 * Is there a source image?
176 *
177 * If not, there's no reason to continue
178 *
179 */
180 if ($this->source_image == '')
181 {
182 $this->set_error('imglib_source_image_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500183 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185
186 /*
187 * Is getimagesize() Available?
188 *
189 * We use it to determine the image properties (width/height).
Derek Jones4b9c6292011-07-01 17:40:48 -0500190 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * properties using ImageMagick and NetPBM
192 *
193 */
194 if ( ! function_exists('getimagesize'))
195 {
196 $this->set_error('imglib_gd_required_for_props');
197 return FALSE;
198 }
199
200 $this->image_library = strtolower($this->image_library);
201
202 /*
203 * Set the full server path
204 *
205 * The source image may or may not contain a path.
206 * Either way, we'll try use realpath to generate the
207 * full server path in order to more reliably read it.
208 *
209 */
210 if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE)
211 {
212 $full_source_path = str_replace("\\", "/", realpath($this->source_image));
213 }
214 else
215 {
216 $full_source_path = $this->source_image;
217 }
218
219 $x = explode('/', $full_source_path);
220 $this->source_image = end($x);
221 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
222
223 // Set the Image Properties
224 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
225 {
Eric Barnesb1673362011-12-05 22:05:38 -0500226 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
228
229 /*
230 * Assign the "new" image name/path
231 *
232 * If the user has set a "new_image" name it means
233 * we are making a copy of the source image. If not
Derek Jones4b9c6292011-07-01 17:40:48 -0500234 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 * set the destination filename and path accordingly.
236 *
237 */
238 if ($this->new_image == '')
239 {
240 $this->dest_image = $this->source_image;
241 $this->dest_folder = $this->source_folder;
242 }
243 else
244 {
245 if (strpos($this->new_image, '/') === FALSE)
246 {
247 $this->dest_folder = $this->source_folder;
248 $this->dest_image = $this->new_image;
249 }
250 else
251 {
252 if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE)
253 {
254 $full_dest_path = str_replace("\\", "/", realpath($this->new_image));
255 }
256 else
257 {
258 $full_dest_path = $this->new_image;
259 }
260
261 // Is there a file name?
262 if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
263 {
264 $this->dest_folder = $full_dest_path.'/';
265 $this->dest_image = $this->source_image;
266 }
267 else
268 {
269 $x = explode('/', $full_dest_path);
270 $this->dest_image = end($x);
271 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
272 }
273 }
274 }
275
276 /*
277 * Compile the finalized filenames/paths
278 *
279 * We'll create two master strings containing the
280 * full server path to the source image and the
281 * full server path to the destination image.
282 * We'll also split the destination image name
283 * so we can insert the thumbnail marker if needed.
284 *
285 */
286 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
287 {
288 $this->thumb_marker = '';
289 }
290
291 $xp = $this->explode_name($this->dest_image);
292
293 $filename = $xp['name'];
294 $file_ext = $xp['ext'];
295
296 $this->full_src_path = $this->source_folder.$this->source_image;
297 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
298
299 /*
300 * Should we maintain image proportions?
301 *
302 * When creating thumbs or copies, the target width/height
303 * might not be in correct proportion with the source
Derek Jones4b9c6292011-07-01 17:40:48 -0500304 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 *
306 */
307 if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
308 {
309 $this->image_reproportion();
310 }
311
312 /*
313 * Was a width and height specified?
314 *
315 * If the destination width/height was
316 * not submitted we will use the values
317 * from the actual file
318 *
319 */
320 if ($this->width == '')
321 $this->width = $this->orig_width;
322
323 if ($this->height == '')
324 $this->height = $this->orig_height;
325
326 // Set the quality
327 $this->quality = trim(str_replace("%", "", $this->quality));
328
329 if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality))
330 $this->quality = 90;
331
332 // Set the x/y coordinates
333 $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis;
334 $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis;
335
336 // Watermark-related Stuff...
337 if ($this->wm_font_color != '')
338 {
339 if (strlen($this->wm_font_color) == 6)
340 {
341 $this->wm_font_color = '#'.$this->wm_font_color;
342 }
343 }
344
345 if ($this->wm_shadow_color != '')
346 {
347 if (strlen($this->wm_shadow_color) == 6)
348 {
349 $this->wm_shadow_color = '#'.$this->wm_shadow_color;
350 }
351 }
352
353 if ($this->wm_overlay_path != '')
354 {
355 $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path));
356 }
357
358 if ($this->wm_shadow_color != '')
359 {
360 $this->wm_use_drop_shadow = TRUE;
361 }
362
363 if ($this->wm_font_path != '')
364 {
365 $this->wm_use_truetype = TRUE;
366 }
367
368 return TRUE;
369 }
370
371 // --------------------------------------------------------------------
372
373 /**
374 * Image Resize
375 *
376 * This is a wrapper function that chooses the proper
377 * resize function based on the protocol specified
378 *
379 * @access public
380 * @return bool
381 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200382 public function resize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 $protocol = 'image_process_'.$this->image_library;
385
Derek Jones1322ec52009-03-11 17:01:14 +0000386 if (preg_match('/gd2$/i', $protocol))
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
388 $protocol = 'image_process_gd';
389 }
390
391 return $this->$protocol('resize');
392 }
393
394 // --------------------------------------------------------------------
395
396 /**
397 * Image Crop
398 *
399 * This is a wrapper function that chooses the proper
400 * cropping function based on the protocol specified
401 *
402 * @access public
403 * @return bool
404 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200405 public function crop()
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
407 $protocol = 'image_process_'.$this->image_library;
408
Derek Jones1322ec52009-03-11 17:01:14 +0000409 if (preg_match('/gd2$/i', $protocol))
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
411 $protocol = 'image_process_gd';
412 }
413
414 return $this->$protocol('crop');
415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Image Rotate
421 *
422 * This is a wrapper function that chooses the proper
423 * rotation function based on the protocol specified
424 *
425 * @access public
426 * @return bool
427 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200428 public function rotate()
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
430 // Allowed rotation values
431 $degs = array(90, 180, 270, 'vrt', 'hor');
432
Derek Allardd9c7f032008-12-01 20:18:00 +0000433 if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
435 $this->set_error('imglib_rotation_angle_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500436 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
438
439 // Reassign the width and height
440 if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
441 {
442 $this->width = $this->orig_height;
443 $this->height = $this->orig_width;
444 }
445 else
446 {
447 $this->width = $this->orig_width;
448 $this->height = $this->orig_height;
449 }
450
451
452 // Choose resizing function
453 if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm')
454 {
455 $protocol = 'image_process_'.$this->image_library;
456
457 return $this->$protocol('rotate');
458 }
459
460 if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
461 {
462 return $this->image_mirror_gd();
463 }
464 else
465 {
466 return $this->image_rotate_gd();
467 }
468 }
469
470 // --------------------------------------------------------------------
471
472 /**
473 * Image Process Using GD/GD2
474 *
475 * This function will resize or crop
476 *
477 * @access public
478 * @param string
479 * @return bool
480 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200481 public function image_process_gd($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
483 $v2_override = FALSE;
484
485 // If the target width/height match the source, AND if the new file name is not equal to the old file name
486 // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
487 if ($this->dynamic_output === FALSE)
488 {
489 if ($this->orig_width == $this->width AND $this->orig_height == $this->height)
490 {
Barry Mienydd671972010-10-04 16:33:58 +0200491 if ($this->source_image != $this->new_image)
492 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 if (@copy($this->full_src_path, $this->full_dst_path))
494 {
Derek Jones172e1612009-10-13 14:32:48 +0000495 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 }
497 }
498
499 return TRUE;
500 }
501 }
502
503 // Let's set up our values based on the action
504 if ($action == 'crop')
505 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500506 // Reassign the source width/height if cropping
507 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 $this->orig_height = $this->height;
509
510 // GD 2.0 has a cropping bug so we'll test for it
511 if ($this->gd_version() !== FALSE)
512 {
513 $gd_version = str_replace('0', '', $this->gd_version());
514 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
515 }
516 }
517 else
518 {
519 // If resizing the x/y axis must be zero
520 $this->x_axis = 0;
521 $this->y_axis = 0;
522 }
523
Derek Jones4b9c6292011-07-01 17:40:48 -0500524 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 if ( ! ($src_img = $this->image_create_gd()))
526 {
527 return FALSE;
528 }
529
Derek Jones4b9c6292011-07-01 17:40:48 -0500530 // Create The Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500532 // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
533 // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
534 // below should that ever prove inaccurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500536 // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200537 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
539 $create = 'imagecreatetruecolor';
540 $copy = 'imagecopyresampled';
541 }
542 else
543 {
544 $create = 'imagecreate';
545 $copy = 'imagecopyresized';
546 }
547
548 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500549
550 if ($this->image_type == 3) // png we can actually preserve transparency
551 {
552 imagealphablending($dst_img, FALSE);
553 imagesavealpha($dst_img, TRUE);
554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
557
Derek Jones4b9c6292011-07-01 17:40:48 -0500558 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 if ($this->dynamic_output == TRUE)
560 {
561 $this->image_display_gd($dst_img);
562 }
563 else
564 {
565 // Or save it
566 if ( ! $this->image_save_gd($dst_img))
567 {
568 return FALSE;
569 }
570 }
571
Derek Jones4b9c6292011-07-01 17:40:48 -0500572 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 imagedestroy($dst_img);
574 imagedestroy($src_img);
575
576 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000577 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000578
579 return TRUE;
580 }
581
582 // --------------------------------------------------------------------
583
584 /**
585 * Image Process Using ImageMagick
586 *
587 * This function will resize, crop or rotate
588 *
589 * @access public
590 * @param string
591 * @return bool
592 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200593 public function image_process_imagemagick($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500595 // Do we have a vaild library path?
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 if ($this->library_path == '')
597 {
598 $this->set_error('imglib_libpath_invalid');
599 return FALSE;
600 }
601
Derek Jones1322ec52009-03-11 17:01:14 +0000602 if ( ! preg_match("/convert$/i", $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
Derek Jones1322ec52009-03-11 17:01:14 +0000604 $this->library_path = rtrim($this->library_path, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000605
606 $this->library_path .= 'convert';
607 }
608
609 // Execute the command
610 $cmd = $this->library_path." -quality ".$this->quality;
611
612 if ($action == 'crop')
613 {
614 $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
615 }
616 elseif ($action == 'rotate')
617 {
618 switch ($this->rotation_angle)
619 {
Barry Mienydd671972010-10-04 16:33:58 +0200620 case 'hor' : $angle = '-flop';
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 break;
Barry Mienydd671972010-10-04 16:33:58 +0200622 case 'vrt' : $angle = '-flip';
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 break;
624 default : $angle = '-rotate '.$this->rotation_angle;
625 break;
626 }
627
628 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
629 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500630 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
632 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
633 }
634
635 $retval = 1;
636
637 @exec($cmd, $output, $retval);
638
639 // Did it work?
640 if ($retval > 0)
641 {
642 $this->set_error('imglib_image_process_failed');
643 return FALSE;
644 }
645
646 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000647 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000648
649 return TRUE;
650 }
651
652 // --------------------------------------------------------------------
653
654 /**
655 * Image Process Using NetPBM
656 *
657 * This function will resize, crop or rotate
658 *
659 * @access public
660 * @param string
661 * @return bool
662 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200663 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 {
665 if ($this->library_path == '')
666 {
667 $this->set_error('imglib_libpath_invalid');
668 return FALSE;
669 }
670
Derek Jones4b9c6292011-07-01 17:40:48 -0500671 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 switch ($this->image_type)
673 {
674 case 1 :
675 $cmd_in = 'giftopnm';
676 $cmd_out = 'ppmtogif';
677 break;
678 case 2 :
679 $cmd_in = 'jpegtopnm';
680 $cmd_out = 'ppmtojpeg';
681 break;
682 case 3 :
683 $cmd_in = 'pngtopnm';
684 $cmd_out = 'ppmtopng';
685 break;
686 }
687
688 if ($action == 'crop')
689 {
690 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
691 }
692 elseif ($action == 'rotate')
693 {
694 switch ($this->rotation_angle)
695 {
696 case 90 : $angle = 'r270';
697 break;
698 case 180 : $angle = 'r180';
699 break;
Barry Mienydd671972010-10-04 16:33:58 +0200700 case 270 : $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 break;
702 case 'vrt' : $angle = 'tb';
703 break;
704 case 'hor' : $angle = 'lr';
705 break;
706 }
707
708 $cmd_inner = 'pnmflip -'.$angle.' ';
709 }
710 else // Resize
711 {
712 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
713 }
714
715 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
716
717 $retval = 1;
718
719 @exec($cmd, $output, $retval);
720
Derek Jones4b9c6292011-07-01 17:40:48 -0500721 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 if ($retval > 0)
723 {
724 $this->set_error('imglib_image_process_failed');
725 return FALSE;
726 }
727
728 // With NetPBM we have to create a temporary image.
729 // If you try manipulating the original it fails so
730 // we have to rename the temp file.
731 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
732 unlink ($this->dest_folder.'netpbm.tmp');
Derek Jones172e1612009-10-13 14:32:48 +0000733 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000734
735 return TRUE;
736 }
737
738 // --------------------------------------------------------------------
739
740 /**
741 * Image Rotate Using GD
742 *
743 * @access public
744 * @return bool
745 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200746 public function image_rotate_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500748 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 if ( ! ($src_img = $this->image_create_gd()))
750 {
751 return FALSE;
752 }
753
754 // Set the background color
755 // This won't work with transparent PNG files so we are
756 // going to have to figure out how to determine the color
757 // of the alpha channel in a future release.
758
759 $white = imagecolorallocate($src_img, 255, 255, 255);
760
Derek Jones4b9c6292011-07-01 17:40:48 -0500761 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
763
Derek Jones4b9c6292011-07-01 17:40:48 -0500764 // Save the Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 if ($this->dynamic_output == TRUE)
766 {
767 $this->image_display_gd($dst_img);
768 }
769 else
770 {
771 // Or save it
772 if ( ! $this->image_save_gd($dst_img))
773 {
774 return FALSE;
775 }
776 }
777
Derek Jones4b9c6292011-07-01 17:40:48 -0500778 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 imagedestroy($dst_img);
780 imagedestroy($src_img);
781
782 // Set the file to 777
783
Derek Jones172e1612009-10-13 14:32:48 +0000784 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000785
Pascal Kriete8761ef52011-02-14 13:13:52 -0500786 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 }
788
789 // --------------------------------------------------------------------
790
791 /**
792 * Create Mirror Image using GD
793 *
794 * This function will flip horizontal or vertical
795 *
796 * @access public
797 * @return bool
798 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200799 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 {
801 if ( ! $src_img = $this->image_create_gd())
802 {
803 return FALSE;
804 }
805
Derek Jones4b9c6292011-07-01 17:40:48 -0500806 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 $height = $this->orig_height;
808
809 if ($this->rotation_angle == 'hor')
810 {
811 for ($i = 0; $i < $height; $i++)
812 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500813 $left = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 $right = $width-1;
815
816 while ($left < $right)
817 {
818 $cl = imagecolorat($src_img, $left, $i);
819 $cr = imagecolorat($src_img, $right, $i);
820
821 imagesetpixel($src_img, $left, $i, $cr);
822 imagesetpixel($src_img, $right, $i, $cl);
823
824 $left++;
825 $right--;
826 }
827 }
828 }
829 else
830 {
831 for ($i = 0; $i < $width; $i++)
832 {
833 $top = 0;
834 $bot = $height-1;
835
836 while ($top < $bot)
837 {
838 $ct = imagecolorat($src_img, $i, $top);
839 $cb = imagecolorat($src_img, $i, $bot);
840
841 imagesetpixel($src_img, $i, $top, $cb);
842 imagesetpixel($src_img, $i, $bot, $ct);
843
844 $top++;
845 $bot--;
846 }
847 }
848 }
849
Derek Jones4b9c6292011-07-01 17:40:48 -0500850 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 if ($this->dynamic_output == TRUE)
852 {
853 $this->image_display_gd($src_img);
854 }
855 else
856 {
857 // Or save it
858 if ( ! $this->image_save_gd($src_img))
859 {
860 return FALSE;
861 }
862 }
863
Derek Jones4b9c6292011-07-01 17:40:48 -0500864 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 imagedestroy($src_img);
866
867 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000868 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000869
870 return TRUE;
871 }
872
873 // --------------------------------------------------------------------
874
875 /**
876 * Image Watermark
877 *
878 * This is a wrapper function that chooses the type
879 * of watermarking based on the specified preference.
880 *
881 * @access public
882 * @param string
883 * @return bool
884 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200885 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 {
887 if ($this->wm_type == 'overlay')
888 {
889 return $this->overlay_watermark();
890 }
891 else
892 {
893 return $this->text_watermark();
894 }
895 }
896
897 // --------------------------------------------------------------------
898
899 /**
900 * Watermark - Graphic Version
901 *
902 * @access public
903 * @return bool
904 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200905 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 {
907 if ( ! function_exists('imagecolortransparent'))
908 {
909 $this->set_error('imglib_gd_required');
910 return FALSE;
911 }
912
Derek Jones4b9c6292011-07-01 17:40:48 -0500913 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 $this->get_image_properties();
915
Derek Jones4b9c6292011-07-01 17:40:48 -0500916 // Fetch watermark image properties
Barry Mienydd671972010-10-04 16:33:58 +0200917 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 $wm_img_type = $props['image_type'];
919 $wm_width = $props['width'];
920 $wm_height = $props['height'];
921
Derek Jones4b9c6292011-07-01 17:40:48 -0500922 // Create two image resources
923 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 $src_img = $this->image_create_gd($this->full_src_path);
925
926 // Reverse the offset if necessary
927 // When the image is positioned at the bottom
928 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -0500929 // further down. We want the reverse, so we'll
930 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 // offset when the image is at the right
932
933 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
934 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
935
936 if ($this->wm_vrt_alignment == 'B')
937 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
938
939 if ($this->wm_hor_alignment == 'R')
940 $this->wm_hor_offset = $this->wm_hor_offset * -1;
941
Derek Jones4b9c6292011-07-01 17:40:48 -0500942 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 $x_axis = $this->wm_hor_offset + $this->wm_padding;
944 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
945
Derek Jones4b9c6292011-07-01 17:40:48 -0500946 // Set the vertical position
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 switch ($this->wm_vrt_alignment)
948 {
949 case 'T':
950 break;
951 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
952 break;
953 case 'B': $y_axis += $this->orig_height - $wm_height;
954 break;
955 }
956
Derek Jones4b9c6292011-07-01 17:40:48 -0500957 // Set the horizontal position
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 switch ($this->wm_hor_alignment)
959 {
960 case 'L':
961 break;
962 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
963 break;
964 case 'R': $x_axis += $this->orig_width - $wm_width;
965 break;
966 }
967
Derek Jones4b9c6292011-07-01 17:40:48 -0500968 // Build the finalized image
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
970 {
971 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200972 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000973
974 // Set RGB values for text and shadow
975 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
976 $alpha = ($rgba & 0x7F000000) >> 24;
977
978 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
979 if ($alpha > 0)
980 {
981 // copy the image directly, the image's alpha transparency being the sole determinant of blending
982 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
983 }
984 else
985 {
986 // set our RGB value from above to be transparent and merge the images with the specified opacity
987 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
988 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
989 }
990
Derek Jones4b9c6292011-07-01 17:40:48 -0500991 // Output the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 if ($this->dynamic_output == TRUE)
993 {
994 $this->image_display_gd($src_img);
995 }
996 else
997 {
998 if ( ! $this->image_save_gd($src_img))
999 {
1000 return FALSE;
1001 }
1002 }
1003
1004 imagedestroy($src_img);
1005 imagedestroy($wm_img);
1006
1007 return TRUE;
1008 }
1009
1010 // --------------------------------------------------------------------
1011
1012 /**
1013 * Watermark - Text Version
1014 *
1015 * @access public
1016 * @return bool
1017 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001018 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 {
1020 if ( ! ($src_img = $this->image_create_gd()))
1021 {
1022 return FALSE;
1023 }
1024
1025 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
1026 {
1027 $this->set_error('imglib_missing_font');
1028 return FALSE;
1029 }
1030
Derek Jones4b9c6292011-07-01 17:40:48 -05001031 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 $this->get_image_properties();
1033
1034 // Set RGB values for text and shadow
1035 $this->wm_font_color = str_replace('#', '', $this->wm_font_color);
1036 $this->wm_shadow_color = str_replace('#', '', $this->wm_shadow_color);
1037
1038 $R1 = hexdec(substr($this->wm_font_color, 0, 2));
1039 $G1 = hexdec(substr($this->wm_font_color, 2, 2));
1040 $B1 = hexdec(substr($this->wm_font_color, 4, 2));
1041
1042 $R2 = hexdec(substr($this->wm_shadow_color, 0, 2));
1043 $G2 = hexdec(substr($this->wm_shadow_color, 2, 2));
1044 $B2 = hexdec(substr($this->wm_shadow_color, 4, 2));
1045
1046 $txt_color = imagecolorclosest($src_img, $R1, $G1, $B1);
1047 $drp_color = imagecolorclosest($src_img, $R2, $G2, $B2);
1048
1049 // Reverse the vertical offset
1050 // When the image is positioned at the bottom
1051 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -05001052 // further down. We want the reverse, so we'll
1053 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 // offset flips itself automatically
1055
1056 if ($this->wm_vrt_alignment == 'B')
1057 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1058
1059 if ($this->wm_hor_alignment == 'R')
1060 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1061
1062 // Set font width and height
1063 // These are calculated differently depending on
1064 // whether we are using the true type font or not
1065 if ($this->wm_use_truetype == TRUE)
1066 {
1067 if ($this->wm_font_size == '')
1068 $this->wm_font_size = '17';
1069
Derek Jones4b9c6292011-07-01 17:40:48 -05001070 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 $fontheight = $this->wm_font_size;
1072 $this->wm_vrt_offset += $this->wm_font_size;
1073 }
1074 else
1075 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001076 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 $fontheight = imagefontheight($this->wm_font_size);
1078 }
1079
1080 // Set base X and Y axis values
1081 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1082 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1083
1084 // Set verticle alignment
1085 if ($this->wm_use_drop_shadow == FALSE)
1086 $this->wm_shadow_distance = 0;
1087
1088 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1089 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1090
1091 switch ($this->wm_vrt_alignment)
1092 {
1093 case "T" :
1094 break;
1095 case "M": $y_axis += ($this->orig_height/2)+($fontheight/2);
1096 break;
1097 case "B": $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
1098 break;
1099 }
1100
1101 $x_shad = $x_axis + $this->wm_shadow_distance;
1102 $y_shad = $y_axis + $this->wm_shadow_distance;
1103
1104 // Set horizontal alignment
1105 switch ($this->wm_hor_alignment)
1106 {
1107 case "L":
1108 break;
1109 case "R":
1110 if ($this->wm_use_drop_shadow)
1111 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1112 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1113 break;
1114 case "C":
1115 if ($this->wm_use_drop_shadow)
1116 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
Derek Jones4b9c6292011-07-01 17:40:48 -05001117 $x_axis += floor(($this->orig_width -$fontwidth*strlen($this->wm_text))/2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 break;
1119 }
1120
Derek Jones4b9c6292011-07-01 17:40:48 -05001121 // Add the text to the source image
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 if ($this->wm_use_truetype)
1123 {
1124 if ($this->wm_use_drop_shadow)
1125 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1126 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1127 }
1128 else
1129 {
1130 if ($this->wm_use_drop_shadow)
1131 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1132 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1133 }
1134
Derek Jones4b9c6292011-07-01 17:40:48 -05001135 // Output the final image
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 if ($this->dynamic_output == TRUE)
1137 {
1138 $this->image_display_gd($src_img);
1139 }
1140 else
1141 {
1142 $this->image_save_gd($src_img);
1143 }
1144
1145 imagedestroy($src_img);
1146
1147 return TRUE;
1148 }
1149
1150 // --------------------------------------------------------------------
1151
1152 /**
1153 * Create Image - GD
1154 *
1155 * This simply creates an image resource handle
1156 * based on the type of image being processed
1157 *
1158 * @access public
1159 * @param string
1160 * @return resource
1161 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001162 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 {
1164 if ($path == '')
1165 $path = $this->full_src_path;
1166
1167 if ($image_type == '')
1168 $image_type = $this->image_type;
1169
1170
1171 switch ($image_type)
1172 {
1173 case 1 :
1174 if ( ! function_exists('imagecreatefromgif'))
1175 {
1176 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1177 return FALSE;
1178 }
1179
1180 return imagecreatefromgif($path);
1181 break;
1182 case 2 :
1183 if ( ! function_exists('imagecreatefromjpeg'))
1184 {
1185 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1186 return FALSE;
1187 }
1188
1189 return imagecreatefromjpeg($path);
1190 break;
1191 case 3 :
1192 if ( ! function_exists('imagecreatefrompng'))
1193 {
1194 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1195 return FALSE;
1196 }
1197
1198 return imagecreatefrompng($path);
1199 break;
1200
1201 }
1202
1203 $this->set_error(array('imglib_unsupported_imagecreate'));
1204 return FALSE;
1205 }
1206
1207 // --------------------------------------------------------------------
1208
1209 /**
1210 * Write image file to disk - GD
1211 *
1212 * Takes an image resource as input and writes the file
1213 * to the specified destination
1214 *
1215 * @access public
1216 * @param resource
1217 * @return bool
1218 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001219 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 {
1221 switch ($this->image_type)
1222 {
1223 case 1 :
1224 if ( ! function_exists('imagegif'))
1225 {
1226 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1227 return FALSE;
1228 }
1229
Derek Jones541ddbd2008-12-09 15:25:31 +00001230 if ( ! @imagegif($resource, $this->full_dst_path))
1231 {
1232 $this->set_error('imglib_save_failed');
1233 return FALSE;
1234 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 break;
1236 case 2 :
1237 if ( ! function_exists('imagejpeg'))
1238 {
1239 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1240 return FALSE;
1241 }
1242
Derek Jones541ddbd2008-12-09 15:25:31 +00001243 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1244 {
1245 $this->set_error('imglib_save_failed');
1246 return FALSE;
1247 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 break;
1249 case 3 :
1250 if ( ! function_exists('imagepng'))
1251 {
1252 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1253 return FALSE;
1254 }
1255
Derek Jones541ddbd2008-12-09 15:25:31 +00001256 if ( ! @imagepng($resource, $this->full_dst_path))
1257 {
1258 $this->set_error('imglib_save_failed');
1259 return FALSE;
1260 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 break;
1262 default :
1263 $this->set_error(array('imglib_unsupported_imagecreate'));
1264 return FALSE;
1265 break;
1266 }
1267
1268 return TRUE;
1269 }
1270
1271 // --------------------------------------------------------------------
1272
1273 /**
1274 * Dynamically outputs an image
1275 *
1276 * @access public
1277 * @param resource
1278 * @return void
1279 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001280 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 {
1282 header("Content-Disposition: filename={$this->source_image};");
1283 header("Content-Type: {$this->mime_type}");
1284 header('Content-Transfer-Encoding: binary');
1285 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1286
1287 switch ($this->image_type)
1288 {
Barry Mienydd671972010-10-04 16:33:58 +02001289 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 break;
1291 case 2 : imagejpeg($resource, '', $this->quality);
1292 break;
1293 case 3 : imagepng($resource);
1294 break;
1295 default : echo 'Unable to display the image';
1296 break;
1297 }
1298 }
1299
1300 // --------------------------------------------------------------------
1301
1302 /**
1303 * Re-proportion Image Width/Height
1304 *
1305 * When creating thumbs, the desired width/height
1306 * can end up warping the image due to an incorrect
1307 * ratio between the full-sized image and the thumb.
1308 *
1309 * This function lets us re-proportion the width/height
1310 * if users choose to maintain the aspect ratio when resizing.
1311 *
1312 * @access public
1313 * @return void
1314 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001315 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001316 {
1317 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
1318 return;
1319
1320 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
1321 return;
1322
1323 $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
1324 $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
1325
1326 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
1327
1328 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
1329 {
1330 $this->master_dim = ($ratio < 0) ? 'width' : 'height';
1331 }
1332
1333 if (($this->width != $new_width) AND ($this->height != $new_height))
1334 {
1335 if ($this->master_dim == 'height')
1336 {
1337 $this->width = $new_width;
1338 }
1339 else
1340 {
1341 $this->height = $new_height;
1342 }
1343 }
1344 }
1345
1346 // --------------------------------------------------------------------
1347
1348 /**
1349 * Get image properties
1350 *
1351 * A helper function that gets info about the file
1352 *
1353 * @access public
1354 * @param string
1355 * @return mixed
1356 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001357 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 {
1359 // For now we require GD but we should
1360 // find a way to determine this using IM or NetPBM
1361
1362 if ($path == '')
1363 $path = $this->full_src_path;
1364
1365 if ( ! file_exists($path))
1366 {
1367 $this->set_error('imglib_invalid_path');
1368 return FALSE;
1369 }
1370
Phil Sturgeon901998a2011-08-26 10:03:33 +01001371 $vals = getimagesize($path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001372
1373 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
1374
1375 $mime = (isset($types[$vals['2']])) ? 'image/'.$types[$vals['2']] : 'image/jpg';
1376
1377 if ($return == TRUE)
1378 {
1379 $v['width'] = $vals['0'];
1380 $v['height'] = $vals['1'];
1381 $v['image_type'] = $vals['2'];
1382 $v['size_str'] = $vals['3'];
1383 $v['mime_type'] = $mime;
1384
1385 return $v;
1386 }
1387
1388 $this->orig_width = $vals['0'];
1389 $this->orig_height = $vals['1'];
1390 $this->image_type = $vals['2'];
1391 $this->size_str = $vals['3'];
1392 $this->mime_type = $mime;
1393
1394 return TRUE;
1395 }
1396
1397 // --------------------------------------------------------------------
1398
1399 /**
1400 * Size calculator
1401 *
1402 * This function takes a known width x height and
Derek Jones4b9c6292011-07-01 17:40:48 -05001403 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 * new variable needs to be known
1405 *
1406 * $props = array(
Barry Mienydd671972010-10-04 16:33:58 +02001407 * 'width' => $width,
1408 * 'height' => $height,
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 * 'new_width' => 40,
1410 * 'new_height' => ''
Derek Jones4b9c6292011-07-01 17:40:48 -05001411 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001412 *
1413 * @access public
1414 * @param array
1415 * @return array
1416 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001417 public function size_calculator($vals)
Derek Allard2067d1a2008-11-13 22:59:24 +00001418 {
1419 if ( ! is_array($vals))
1420 {
1421 return;
1422 }
1423
1424 $allowed = array('new_width', 'new_height', 'width', 'height');
1425
1426 foreach ($allowed as $item)
1427 {
1428 if ( ! isset($vals[$item]) OR $vals[$item] == '')
1429 $vals[$item] = 0;
1430 }
1431
1432 if ($vals['width'] == 0 OR $vals['height'] == 0)
1433 {
1434 return $vals;
1435 }
1436
1437 if ($vals['new_width'] == 0)
1438 {
1439 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1440 }
1441 elseif ($vals['new_height'] == 0)
1442 {
1443 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1444 }
1445
1446 return $vals;
1447 }
1448
1449 // --------------------------------------------------------------------
1450
1451 /**
1452 * Explode source_image
1453 *
1454 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001455 * from the source_image. This function lets us deal with
1456 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001458 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 * $array['name'] = 'my.cool';
1460 *
1461 * @access public
1462 * @param array
1463 * @return array
1464 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001465 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001466 {
Derek Jones08cae632009-02-10 20:03:29 +00001467 $ext = strrchr($source_image, '.');
1468 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001469
Derek Jones08cae632009-02-10 20:03:29 +00001470 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 }
1472
1473 // --------------------------------------------------------------------
1474
1475 /**
1476 * Is GD Installed?
1477 *
1478 * @access public
1479 * @return bool
1480 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001481 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 {
1483 if ( ! extension_loaded('gd'))
1484 {
1485 if ( ! dl('gd.so'))
1486 {
1487 return FALSE;
1488 }
1489 }
1490
1491 return TRUE;
1492 }
1493
1494 // --------------------------------------------------------------------
1495
1496 /**
1497 * Get GD version
1498 *
1499 * @access public
1500 * @return mixed
1501 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001502 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 {
1504 if (function_exists('gd_info'))
1505 {
1506 $gd_version = @gd_info();
1507 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
1508
1509 return $gd_version;
1510 }
1511
1512 return FALSE;
1513 }
1514
1515 // --------------------------------------------------------------------
1516
1517 /**
1518 * Set error message
1519 *
1520 * @access public
1521 * @param string
1522 * @return void
1523 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001524 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 {
1526 $CI =& get_instance();
1527 $CI->lang->load('imglib');
1528
1529 if (is_array($msg))
1530 {
1531 foreach ($msg as $val)
1532 {
1533
1534 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1535 $this->error_msg[] = $msg;
1536 log_message('error', $msg);
1537 }
1538 }
1539 else
1540 {
1541 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1542 $this->error_msg[] = $msg;
1543 log_message('error', $msg);
1544 }
1545 }
1546
1547 // --------------------------------------------------------------------
1548
1549 /**
1550 * Show error messages
1551 *
1552 * @access public
1553 * @param string
1554 * @return string
1555 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001556 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001557 {
Andrey Andreev4eea9892011-12-19 12:05:41 +02001558 return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 }
1560
1561}
1562// END Image_lib Class
1563
1564/* End of file Image_lib.php */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001565/* Location: ./system/libraries/Image_lib.php */