blob: 2737503a8db406a76596c67778978e04c4e4af42 [file] [log] [blame]
Andrey Andreeva92b9032011-12-24 19:05:58 +02001<?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 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 * @return void
115 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200116 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
Michael Denniscb07a322011-08-20 23:40:59 -0700118 $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 +0000119
120 foreach ($props as $val)
121 {
122 $this->$val = '';
123 }
124
Michael Denniscb07a322011-08-20 23:40:59 -0700125 $this->image_library = 'gd2';
126 $this->dynamic_output = FALSE;
127 $this->quality = '90';
128 $this->create_thumb = FALSE;
129 $this->thumb_marker = '_thumb';
130 $this->maintain_ratio = TRUE;
131 $this->master_dim = 'auto';
132 $this->wm_type = 'text';
133 $this->wm_x_transp = 4;
134 $this->wm_y_transp = 4;
135 $this->wm_font_size = 17;
136 $this->wm_vrt_alignment = 'B';
137 $this->wm_hor_alignment = 'C';
138 $this->wm_padding = 0;
139 $this->wm_hor_offset = 0;
140 $this->wm_vrt_offset = 0;
141 $this->wm_font_color = '#ffffff';
142 $this->wm_shadow_distance = 2;
143 $this->wm_opacity = 50;
144 $this->create_fnc = 'imagecreatetruecolor';
145 $this->copy_fnc = 'imagecopyresampled';
146 $this->error_msg = array();
147 $this->wm_use_drop_shadow = FALSE;
148 $this->wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * initialize image preferences
155 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 * @param array
157 * @return bool
158 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200159 public function initialize($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
161 /*
162 * Convert array elements into class variables
163 */
164 if (count($props) > 0)
165 {
166 foreach ($props as $key => $val)
167 {
168 $this->$key = $val;
169 }
170 }
171
172 /*
173 * Is there a source image?
174 *
175 * If not, there's no reason to continue
176 *
177 */
178 if ($this->source_image == '')
179 {
180 $this->set_error('imglib_source_image_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500181 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
183
184 /*
185 * Is getimagesize() Available?
186 *
187 * We use it to determine the image properties (width/height).
Derek Jones4b9c6292011-07-01 17:40:48 -0500188 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * properties using ImageMagick and NetPBM
190 *
191 */
192 if ( ! function_exists('getimagesize'))
193 {
194 $this->set_error('imglib_gd_required_for_props');
195 return FALSE;
196 }
197
198 $this->image_library = strtolower($this->image_library);
199
200 /*
201 * Set the full server path
202 *
203 * The source image may or may not contain a path.
204 * Either way, we'll try use realpath to generate the
205 * full server path in order to more reliably read it.
206 *
207 */
208 if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE)
209 {
210 $full_source_path = str_replace("\\", "/", realpath($this->source_image));
211 }
212 else
213 {
214 $full_source_path = $this->source_image;
215 }
216
217 $x = explode('/', $full_source_path);
218 $this->source_image = end($x);
219 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
220
221 // Set the Image Properties
222 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
223 {
Eric Barnesb1673362011-12-05 22:05:38 -0500224 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
226
227 /*
228 * Assign the "new" image name/path
229 *
230 * If the user has set a "new_image" name it means
231 * we are making a copy of the source image. If not
Derek Jones4b9c6292011-07-01 17:40:48 -0500232 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 * set the destination filename and path accordingly.
234 *
235 */
236 if ($this->new_image == '')
237 {
238 $this->dest_image = $this->source_image;
239 $this->dest_folder = $this->source_folder;
240 }
241 else
242 {
243 if (strpos($this->new_image, '/') === FALSE)
244 {
245 $this->dest_folder = $this->source_folder;
246 $this->dest_image = $this->new_image;
247 }
248 else
249 {
250 if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE)
251 {
252 $full_dest_path = str_replace("\\", "/", realpath($this->new_image));
253 }
254 else
255 {
256 $full_dest_path = $this->new_image;
257 }
258
259 // Is there a file name?
260 if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
261 {
262 $this->dest_folder = $full_dest_path.'/';
263 $this->dest_image = $this->source_image;
264 }
265 else
266 {
267 $x = explode('/', $full_dest_path);
268 $this->dest_image = end($x);
269 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
270 }
271 }
272 }
273
274 /*
275 * Compile the finalized filenames/paths
276 *
277 * We'll create two master strings containing the
278 * full server path to the source image and the
279 * full server path to the destination image.
280 * We'll also split the destination image name
281 * so we can insert the thumbnail marker if needed.
282 *
283 */
284 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
285 {
286 $this->thumb_marker = '';
287 }
288
289 $xp = $this->explode_name($this->dest_image);
290
291 $filename = $xp['name'];
292 $file_ext = $xp['ext'];
293
294 $this->full_src_path = $this->source_folder.$this->source_image;
295 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
296
297 /*
298 * Should we maintain image proportions?
299 *
300 * When creating thumbs or copies, the target width/height
301 * might not be in correct proportion with the source
Derek Jones4b9c6292011-07-01 17:40:48 -0500302 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 *
304 */
305 if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
306 {
307 $this->image_reproportion();
308 }
309
310 /*
311 * Was a width and height specified?
312 *
313 * If the destination width/height was
314 * not submitted we will use the values
315 * from the actual file
316 *
317 */
318 if ($this->width == '')
319 $this->width = $this->orig_width;
320
321 if ($this->height == '')
322 $this->height = $this->orig_height;
323
324 // Set the quality
325 $this->quality = trim(str_replace("%", "", $this->quality));
326
327 if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality))
328 $this->quality = 90;
329
330 // Set the x/y coordinates
331 $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis;
332 $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis;
333
334 // Watermark-related Stuff...
Andrey Andreeva92b9032011-12-24 19:05:58 +0200335 if ($this->wm_font_color != '' AND strlen($this->wm_font_color) === 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200337 $this->wm_font_color = '#'.$this->wm_font_color;
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339
Andrey Andreeva92b9032011-12-24 19:05:58 +0200340 if ($this->wm_shadow_color != '' AND strlen($this->wm_shadow_color) === 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200342 $this->wm_shadow_color = '#'.$this->wm_shadow_color;
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 }
344
345 if ($this->wm_overlay_path != '')
346 {
347 $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path));
348 }
349
350 if ($this->wm_shadow_color != '')
351 {
352 $this->wm_use_drop_shadow = TRUE;
353 }
354
355 if ($this->wm_font_path != '')
356 {
357 $this->wm_use_truetype = TRUE;
358 }
359
360 return TRUE;
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * Image Resize
367 *
368 * This is a wrapper function that chooses the proper
369 * resize function based on the protocol specified
370 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 * @return bool
372 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200373 public function resize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200375 $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 return $this->$protocol('resize');
377 }
378
379 // --------------------------------------------------------------------
380
381 /**
382 * Image Crop
383 *
384 * This is a wrapper function that chooses the proper
385 * cropping function based on the protocol specified
386 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 * @return bool
388 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200389 public function crop()
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200391 $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 return $this->$protocol('crop');
393 }
394
395 // --------------------------------------------------------------------
396
397 /**
398 * Image Rotate
399 *
400 * This is a wrapper function that chooses the proper
401 * rotation function based on the protocol specified
402 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * @return bool
404 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200405 public function rotate()
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
407 // Allowed rotation values
408 $degs = array(90, 180, 270, 'vrt', 'hor');
409
Derek Allardd9c7f032008-12-01 20:18:00 +0000410 if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
412 $this->set_error('imglib_rotation_angle_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500413 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
415
416 // Reassign the width and height
417 if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
418 {
419 $this->width = $this->orig_height;
420 $this->height = $this->orig_width;
421 }
422 else
423 {
424 $this->width = $this->orig_width;
425 $this->height = $this->orig_height;
426 }
427
428
429 // Choose resizing function
430 if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm')
431 {
432 $protocol = 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 return $this->$protocol('rotate');
434 }
435
436 if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
437 {
438 return $this->image_mirror_gd();
439 }
440 else
441 {
442 return $this->image_rotate_gd();
443 }
444 }
445
446 // --------------------------------------------------------------------
447
448 /**
449 * Image Process Using GD/GD2
450 *
451 * This function will resize or crop
452 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 * @param string
454 * @return bool
455 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200456 public function image_process_gd($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
458 $v2_override = FALSE;
459
460 // If the target width/height match the source, AND if the new file name is not equal to the old file name
461 // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
Andrey Andreeva92b9032011-12-24 19:05:58 +0200462 if ($this->dynamic_output === FALSE AND $this->orig_width == $this->width AND $this->orig_height == $this->height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200464 if ($this->source_image != $this->new_image AND @copy($this->full_src_path, $this->full_dst_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200466 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200468
469 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
471
472 // Let's set up our values based on the action
473 if ($action == 'crop')
474 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500475 // Reassign the source width/height if cropping
476 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 $this->orig_height = $this->height;
478
479 // GD 2.0 has a cropping bug so we'll test for it
480 if ($this->gd_version() !== FALSE)
481 {
482 $gd_version = str_replace('0', '', $this->gd_version());
483 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
484 }
485 }
486 else
487 {
488 // If resizing the x/y axis must be zero
489 $this->x_axis = 0;
490 $this->y_axis = 0;
491 }
492
Derek Jones4b9c6292011-07-01 17:40:48 -0500493 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 if ( ! ($src_img = $this->image_create_gd()))
495 {
496 return FALSE;
497 }
498
Derek Jones4b9c6292011-07-01 17:40:48 -0500499 // Create The Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500501 // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
502 // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
503 // below should that ever prove inaccurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500505 // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200506 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
508 $create = 'imagecreatetruecolor';
509 $copy = 'imagecopyresampled';
510 }
511 else
512 {
513 $create = 'imagecreate';
514 $copy = 'imagecopyresized';
515 }
516
517 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500518
519 if ($this->image_type == 3) // png we can actually preserve transparency
520 {
521 imagealphablending($dst_img, FALSE);
522 imagesavealpha($dst_img, TRUE);
523 }
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
526
Derek Jones4b9c6292011-07-01 17:40:48 -0500527 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 if ($this->dynamic_output == TRUE)
529 {
530 $this->image_display_gd($dst_img);
531 }
532 else
533 {
534 // Or save it
535 if ( ! $this->image_save_gd($dst_img))
536 {
537 return FALSE;
538 }
539 }
540
Derek Jones4b9c6292011-07-01 17:40:48 -0500541 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 imagedestroy($dst_img);
543 imagedestroy($src_img);
544
545 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000546 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000547
548 return TRUE;
549 }
550
551 // --------------------------------------------------------------------
552
553 /**
554 * Image Process Using ImageMagick
555 *
556 * This function will resize, crop or rotate
557 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 * @param string
559 * @return bool
560 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200561 public function image_process_imagemagick($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500563 // Do we have a vaild library path?
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 if ($this->library_path == '')
565 {
566 $this->set_error('imglib_libpath_invalid');
567 return FALSE;
568 }
569
Derek Jones1322ec52009-03-11 17:01:14 +0000570 if ( ! preg_match("/convert$/i", $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200572 $this->library_path = rtrim($this->library_path, '/').'/convert';
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 }
574
575 // Execute the command
576 $cmd = $this->library_path." -quality ".$this->quality;
577
578 if ($action == 'crop')
579 {
580 $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
581 }
582 elseif ($action == 'rotate')
583 {
584 switch ($this->rotation_angle)
585 {
Barry Mienydd671972010-10-04 16:33:58 +0200586 case 'hor' : $angle = '-flop';
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 break;
Barry Mienydd671972010-10-04 16:33:58 +0200588 case 'vrt' : $angle = '-flip';
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 break;
590 default : $angle = '-rotate '.$this->rotation_angle;
591 break;
592 }
593
594 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
595 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500596 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
598 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
599 }
600
601 $retval = 1;
602
603 @exec($cmd, $output, $retval);
604
605 // Did it work?
606 if ($retval > 0)
607 {
608 $this->set_error('imglib_image_process_failed');
609 return FALSE;
610 }
611
612 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000613 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000614
615 return TRUE;
616 }
617
618 // --------------------------------------------------------------------
619
620 /**
621 * Image Process Using NetPBM
622 *
623 * This function will resize, crop or rotate
624 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 * @param string
626 * @return bool
627 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200628 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 {
630 if ($this->library_path == '')
631 {
632 $this->set_error('imglib_libpath_invalid');
633 return FALSE;
634 }
635
Derek Jones4b9c6292011-07-01 17:40:48 -0500636 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 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;
Barry Mienydd671972010-10-04 16:33:58 +0200665 case 270 : $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 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
Derek Jones4b9c6292011-07-01 17:40:48 -0500686 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 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 Jones172e1612009-10-13 14:32:48 +0000698 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000699
700 return TRUE;
701 }
702
703 // --------------------------------------------------------------------
704
705 /**
706 * Image Rotate Using GD
707 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 * @return bool
709 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200710 public function image_rotate_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500712 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 if ( ! ($src_img = $this->image_create_gd()))
714 {
715 return FALSE;
716 }
717
718 // Set the background color
719 // This won't work with transparent PNG files so we are
720 // going to have to figure out how to determine the color
721 // of the alpha channel in a future release.
722
723 $white = imagecolorallocate($src_img, 255, 255, 255);
724
Derek Jones4b9c6292011-07-01 17:40:48 -0500725 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
727
Derek Jones4b9c6292011-07-01 17:40:48 -0500728 // Save the Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 if ($this->dynamic_output == TRUE)
730 {
731 $this->image_display_gd($dst_img);
732 }
733 else
734 {
735 // Or save it
736 if ( ! $this->image_save_gd($dst_img))
737 {
738 return FALSE;
739 }
740 }
741
Derek Jones4b9c6292011-07-01 17:40:48 -0500742 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 imagedestroy($dst_img);
744 imagedestroy($src_img);
745
746 // Set the file to 777
747
Derek Jones172e1612009-10-13 14:32:48 +0000748 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000749
Pascal Kriete8761ef52011-02-14 13:13:52 -0500750 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 }
752
753 // --------------------------------------------------------------------
754
755 /**
756 * Create Mirror Image using GD
757 *
758 * This function will flip horizontal or vertical
759 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 * @return bool
761 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200762 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 {
764 if ( ! $src_img = $this->image_create_gd())
765 {
766 return FALSE;
767 }
768
Derek Jones4b9c6292011-07-01 17:40:48 -0500769 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 $height = $this->orig_height;
771
772 if ($this->rotation_angle == 'hor')
773 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200774 for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 while ($left < $right)
777 {
778 $cl = imagecolorat($src_img, $left, $i);
779 $cr = imagecolorat($src_img, $right, $i);
780
781 imagesetpixel($src_img, $left, $i, $cr);
782 imagesetpixel($src_img, $right, $i, $cl);
783
784 $left++;
785 $right--;
786 }
787 }
788 }
789 else
790 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200791 for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 while ($top < $bot)
794 {
795 $ct = imagecolorat($src_img, $i, $top);
796 $cb = imagecolorat($src_img, $i, $bot);
797
798 imagesetpixel($src_img, $i, $top, $cb);
799 imagesetpixel($src_img, $i, $bot, $ct);
800
801 $top++;
802 $bot--;
803 }
804 }
805 }
806
Derek Jones4b9c6292011-07-01 17:40:48 -0500807 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 if ($this->dynamic_output == TRUE)
809 {
810 $this->image_display_gd($src_img);
811 }
812 else
813 {
814 // Or save it
815 if ( ! $this->image_save_gd($src_img))
816 {
817 return FALSE;
818 }
819 }
820
Derek Jones4b9c6292011-07-01 17:40:48 -0500821 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 imagedestroy($src_img);
823
824 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000825 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000826
827 return TRUE;
828 }
829
830 // --------------------------------------------------------------------
831
832 /**
833 * Image Watermark
834 *
835 * This is a wrapper function that chooses the type
836 * of watermarking based on the specified preference.
837 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 * @param string
839 * @return bool
840 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200841 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 {
843 if ($this->wm_type == 'overlay')
844 {
845 return $this->overlay_watermark();
846 }
847 else
848 {
849 return $this->text_watermark();
850 }
851 }
852
853 // --------------------------------------------------------------------
854
855 /**
856 * Watermark - Graphic Version
857 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 * @return bool
859 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200860 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 {
862 if ( ! function_exists('imagecolortransparent'))
863 {
864 $this->set_error('imglib_gd_required');
865 return FALSE;
866 }
867
Derek Jones4b9c6292011-07-01 17:40:48 -0500868 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 $this->get_image_properties();
870
Derek Jones4b9c6292011-07-01 17:40:48 -0500871 // Fetch watermark image properties
Barry Mienydd671972010-10-04 16:33:58 +0200872 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 $wm_img_type = $props['image_type'];
874 $wm_width = $props['width'];
875 $wm_height = $props['height'];
876
Derek Jones4b9c6292011-07-01 17:40:48 -0500877 // Create two image resources
878 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 $src_img = $this->image_create_gd($this->full_src_path);
880
881 // Reverse the offset if necessary
882 // When the image is positioned at the bottom
883 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -0500884 // further down. We want the reverse, so we'll
885 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 // offset when the image is at the right
887
888 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
889 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
890
891 if ($this->wm_vrt_alignment == 'B')
892 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
893
894 if ($this->wm_hor_alignment == 'R')
895 $this->wm_hor_offset = $this->wm_hor_offset * -1;
896
Derek Jones4b9c6292011-07-01 17:40:48 -0500897 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 $x_axis = $this->wm_hor_offset + $this->wm_padding;
899 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
900
Derek Jones4b9c6292011-07-01 17:40:48 -0500901 // Set the vertical position
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 switch ($this->wm_vrt_alignment)
903 {
904 case 'T':
905 break;
906 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
907 break;
908 case 'B': $y_axis += $this->orig_height - $wm_height;
909 break;
910 }
911
Derek Jones4b9c6292011-07-01 17:40:48 -0500912 // Set the horizontal position
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 switch ($this->wm_hor_alignment)
914 {
915 case 'L':
916 break;
917 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
918 break;
919 case 'R': $x_axis += $this->orig_width - $wm_width;
920 break;
921 }
922
Derek Jones4b9c6292011-07-01 17:40:48 -0500923 // Build the finalized image
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
925 {
926 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200927 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000928
929 // Set RGB values for text and shadow
930 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
931 $alpha = ($rgba & 0x7F000000) >> 24;
932
933 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
934 if ($alpha > 0)
935 {
936 // copy the image directly, the image's alpha transparency being the sole determinant of blending
937 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
938 }
939 else
940 {
941 // set our RGB value from above to be transparent and merge the images with the specified opacity
942 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
943 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
944 }
945
Derek Jones4b9c6292011-07-01 17:40:48 -0500946 // Output the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 if ($this->dynamic_output == TRUE)
948 {
949 $this->image_display_gd($src_img);
950 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200951 elseif ( ! $this->image_save_gd($src_img))
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200953 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 }
955
956 imagedestroy($src_img);
957 imagedestroy($wm_img);
958
959 return TRUE;
960 }
961
962 // --------------------------------------------------------------------
963
964 /**
965 * Watermark - Text Version
966 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 * @return bool
968 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200969 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 {
971 if ( ! ($src_img = $this->image_create_gd()))
972 {
973 return FALSE;
974 }
975
976 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
977 {
978 $this->set_error('imglib_missing_font');
979 return FALSE;
980 }
981
Derek Jones4b9c6292011-07-01 17:40:48 -0500982 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 $this->get_image_properties();
984
985 // Set RGB values for text and shadow
986 $this->wm_font_color = str_replace('#', '', $this->wm_font_color);
987 $this->wm_shadow_color = str_replace('#', '', $this->wm_shadow_color);
988
989 $R1 = hexdec(substr($this->wm_font_color, 0, 2));
990 $G1 = hexdec(substr($this->wm_font_color, 2, 2));
991 $B1 = hexdec(substr($this->wm_font_color, 4, 2));
992
993 $R2 = hexdec(substr($this->wm_shadow_color, 0, 2));
994 $G2 = hexdec(substr($this->wm_shadow_color, 2, 2));
995 $B2 = hexdec(substr($this->wm_shadow_color, 4, 2));
996
997 $txt_color = imagecolorclosest($src_img, $R1, $G1, $B1);
998 $drp_color = imagecolorclosest($src_img, $R2, $G2, $B2);
999
1000 // Reverse the vertical offset
1001 // When the image is positioned at the bottom
1002 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -05001003 // further down. We want the reverse, so we'll
1004 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 // offset flips itself automatically
1006
1007 if ($this->wm_vrt_alignment == 'B')
1008 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1009
1010 if ($this->wm_hor_alignment == 'R')
1011 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1012
1013 // Set font width and height
1014 // These are calculated differently depending on
1015 // whether we are using the true type font or not
1016 if ($this->wm_use_truetype == TRUE)
1017 {
1018 if ($this->wm_font_size == '')
Andrey Andreeva92b9032011-12-24 19:05:58 +02001019 {
1020 $this->wm_font_size = 17;
1021 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001022
Derek Jones4b9c6292011-07-01 17:40:48 -05001023 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 $fontheight = $this->wm_font_size;
1025 $this->wm_vrt_offset += $this->wm_font_size;
1026 }
1027 else
1028 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001029 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 $fontheight = imagefontheight($this->wm_font_size);
1031 }
1032
1033 // Set base X and Y axis values
1034 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1035 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1036
1037 // Set verticle alignment
1038 if ($this->wm_use_drop_shadow == FALSE)
1039 $this->wm_shadow_distance = 0;
1040
1041 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1042 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1043
1044 switch ($this->wm_vrt_alignment)
1045 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001046 case 'T':
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001048 case 'M': $y_axis += ($this->orig_height/2)+($fontheight/2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001050 case 'B': $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 break;
1052 }
1053
1054 $x_shad = $x_axis + $this->wm_shadow_distance;
1055 $y_shad = $y_axis + $this->wm_shadow_distance;
1056
1057 // Set horizontal alignment
1058 switch ($this->wm_hor_alignment)
1059 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001060 case 'L':
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001062 case 'R':
1063 if ($this->wm_use_drop_shadow)
1064 {
1065 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1066 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1067 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001069 case 'C':
1070 if ($this->wm_use_drop_shadow)
1071 {
1072 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1073 $x_axis += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1074 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 break;
1076 }
1077
Derek Jones4b9c6292011-07-01 17:40:48 -05001078 // Add the text to the source image
Andrey Andreeva92b9032011-12-24 19:05:58 +02001079 if ($this->wm_use_truetype AND $this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001081 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1082 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 }
Andrey Andreeva92b9032011-12-24 19:05:58 +02001084 elseif ($this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001086 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1087 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 }
1089
Derek Jones4b9c6292011-07-01 17:40:48 -05001090 // Output the final image
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 if ($this->dynamic_output == TRUE)
1092 {
1093 $this->image_display_gd($src_img);
1094 }
1095 else
1096 {
1097 $this->image_save_gd($src_img);
1098 }
1099
1100 imagedestroy($src_img);
1101
1102 return TRUE;
1103 }
1104
1105 // --------------------------------------------------------------------
1106
1107 /**
1108 * Create Image - GD
1109 *
1110 * This simply creates an image resource handle
1111 * based on the type of image being processed
1112 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 * @param string
1114 * @return resource
1115 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001116 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 {
1118 if ($path == '')
1119 $path = $this->full_src_path;
1120
1121 if ($image_type == '')
1122 $image_type = $this->image_type;
1123
1124
1125 switch ($image_type)
1126 {
1127 case 1 :
1128 if ( ! function_exists('imagecreatefromgif'))
1129 {
1130 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1131 return FALSE;
1132 }
1133
1134 return imagecreatefromgif($path);
1135 break;
1136 case 2 :
1137 if ( ! function_exists('imagecreatefromjpeg'))
1138 {
1139 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1140 return FALSE;
1141 }
1142
1143 return imagecreatefromjpeg($path);
1144 break;
1145 case 3 :
1146 if ( ! function_exists('imagecreatefrompng'))
1147 {
1148 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1149 return FALSE;
1150 }
1151
1152 return imagecreatefrompng($path);
1153 break;
1154
1155 }
1156
1157 $this->set_error(array('imglib_unsupported_imagecreate'));
1158 return FALSE;
1159 }
1160
1161 // --------------------------------------------------------------------
1162
1163 /**
1164 * Write image file to disk - GD
1165 *
1166 * Takes an image resource as input and writes the file
1167 * to the specified destination
1168 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 * @param resource
1170 * @return bool
1171 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001172 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 {
1174 switch ($this->image_type)
1175 {
1176 case 1 :
1177 if ( ! function_exists('imagegif'))
1178 {
1179 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1180 return FALSE;
1181 }
1182
Derek Jones541ddbd2008-12-09 15:25:31 +00001183 if ( ! @imagegif($resource, $this->full_dst_path))
1184 {
1185 $this->set_error('imglib_save_failed');
1186 return FALSE;
1187 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 break;
1189 case 2 :
1190 if ( ! function_exists('imagejpeg'))
1191 {
1192 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1193 return FALSE;
1194 }
1195
Derek Jones541ddbd2008-12-09 15:25:31 +00001196 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1197 {
1198 $this->set_error('imglib_save_failed');
1199 return FALSE;
1200 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 break;
1202 case 3 :
1203 if ( ! function_exists('imagepng'))
1204 {
1205 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1206 return FALSE;
1207 }
1208
Derek Jones541ddbd2008-12-09 15:25:31 +00001209 if ( ! @imagepng($resource, $this->full_dst_path))
1210 {
1211 $this->set_error('imglib_save_failed');
1212 return FALSE;
1213 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 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 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 * @param resource
1230 * @return void
1231 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001232 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 {
1234 header("Content-Disposition: filename={$this->source_image};");
1235 header("Content-Type: {$this->mime_type}");
1236 header('Content-Transfer-Encoding: binary');
1237 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1238
1239 switch ($this->image_type)
1240 {
Barry Mienydd671972010-10-04 16:33:58 +02001241 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 break;
1243 case 2 : imagejpeg($resource, '', $this->quality);
1244 break;
1245 case 3 : imagepng($resource);
1246 break;
1247 default : echo 'Unable to display the image';
1248 break;
1249 }
1250 }
1251
1252 // --------------------------------------------------------------------
1253
1254 /**
1255 * Re-proportion Image Width/Height
1256 *
1257 * When creating thumbs, the desired width/height
1258 * can end up warping the image due to an incorrect
1259 * ratio between the full-sized image and the thumb.
1260 *
1261 * This function lets us re-proportion the width/height
1262 * if users choose to maintain the aspect ratio when resizing.
1263 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 * @return void
1265 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001266 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 {
1268 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
1269 return;
1270
1271 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
1272 return;
1273
1274 $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
1275 $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
1276
1277 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
1278
1279 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
1280 {
1281 $this->master_dim = ($ratio < 0) ? 'width' : 'height';
1282 }
1283
1284 if (($this->width != $new_width) AND ($this->height != $new_height))
1285 {
1286 if ($this->master_dim == 'height')
1287 {
1288 $this->width = $new_width;
1289 }
1290 else
1291 {
1292 $this->height = $new_height;
1293 }
1294 }
1295 }
1296
1297 // --------------------------------------------------------------------
1298
1299 /**
1300 * Get image properties
1301 *
1302 * A helper function that gets info about the file
1303 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 * @param string
1305 * @return mixed
1306 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001307 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 {
1309 // For now we require GD but we should
1310 // find a way to determine this using IM or NetPBM
1311
1312 if ($path == '')
1313 $path = $this->full_src_path;
1314
1315 if ( ! file_exists($path))
1316 {
1317 $this->set_error('imglib_invalid_path');
1318 return FALSE;
1319 }
1320
Phil Sturgeon901998a2011-08-26 10:03:33 +01001321 $vals = getimagesize($path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
Andrey Andreeva92b9032011-12-24 19:05:58 +02001323 $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001324
1325 if ($return == TRUE)
1326 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001327 return array(
1328 'width' => $vals[0],
1329 'height' => $vals[1],
1330 'image_type' => $vals[2],
1331 'size_str' => $vals[3],
1332 'mime_type' => $mime
1333 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 }
1335
Andrey Andreeva92b9032011-12-24 19:05:58 +02001336 $this->orig_width = $vals[0];
1337 $this->orig_height = $vals[1];
1338 $this->image_type = $vals[2];
1339 $this->size_str = $vals[3];
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 $this->mime_type = $mime;
1341
1342 return TRUE;
1343 }
1344
1345 // --------------------------------------------------------------------
1346
1347 /**
1348 * Size calculator
1349 *
1350 * This function takes a known width x height and
Derek Jones4b9c6292011-07-01 17:40:48 -05001351 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 * new variable needs to be known
1353 *
1354 * $props = array(
Barry Mienydd671972010-10-04 16:33:58 +02001355 * 'width' => $width,
1356 * 'height' => $height,
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 * 'new_width' => 40,
1358 * 'new_height' => ''
Derek Jones4b9c6292011-07-01 17:40:48 -05001359 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 * @param array
1362 * @return array
1363 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001364 public function size_calculator($vals)
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 {
1366 if ( ! is_array($vals))
1367 {
1368 return;
1369 }
1370
1371 $allowed = array('new_width', 'new_height', 'width', 'height');
1372
1373 foreach ($allowed as $item)
1374 {
1375 if ( ! isset($vals[$item]) OR $vals[$item] == '')
1376 $vals[$item] = 0;
1377 }
1378
1379 if ($vals['width'] == 0 OR $vals['height'] == 0)
1380 {
1381 return $vals;
1382 }
1383
1384 if ($vals['new_width'] == 0)
1385 {
1386 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1387 }
1388 elseif ($vals['new_height'] == 0)
1389 {
1390 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1391 }
1392
1393 return $vals;
1394 }
1395
1396 // --------------------------------------------------------------------
1397
1398 /**
1399 * Explode source_image
1400 *
1401 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001402 * from the source_image. This function lets us deal with
1403 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001405 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 * $array['name'] = 'my.cool';
1407 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 * @param array
1409 * @return array
1410 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001411 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001412 {
Derek Jones08cae632009-02-10 20:03:29 +00001413 $ext = strrchr($source_image, '.');
1414 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001415
Derek Jones08cae632009-02-10 20:03:29 +00001416 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 }
1418
1419 // --------------------------------------------------------------------
1420
1421 /**
1422 * Is GD Installed?
1423 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 * @return bool
1425 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001426 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 {
1428 if ( ! extension_loaded('gd'))
1429 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001430 /* As it is stated in the PHP manual, dl() is not always available
1431 * and even if so - it could generate an E_WARNING message on failure
1432 */
1433 return (function_exists('dl') AND @dl('gd.so'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001434 }
1435
1436 return TRUE;
1437 }
1438
1439 // --------------------------------------------------------------------
1440
1441 /**
1442 * Get GD version
1443 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001444 * @return mixed
1445 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001446 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 {
1448 if (function_exists('gd_info'))
1449 {
1450 $gd_version = @gd_info();
1451 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
1452
1453 return $gd_version;
1454 }
1455
1456 return FALSE;
1457 }
1458
1459 // --------------------------------------------------------------------
1460
1461 /**
1462 * Set error message
1463 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 * @param string
1465 * @return void
1466 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001467 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 {
1469 $CI =& get_instance();
1470 $CI->lang->load('imglib');
1471
1472 if (is_array($msg))
1473 {
1474 foreach ($msg as $val)
1475 {
1476
1477 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1478 $this->error_msg[] = $msg;
1479 log_message('error', $msg);
1480 }
1481 }
1482 else
1483 {
1484 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1485 $this->error_msg[] = $msg;
1486 log_message('error', $msg);
1487 }
1488 }
1489
1490 // --------------------------------------------------------------------
1491
1492 /**
1493 * Show error messages
1494 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001495 * @param string
1496 * @return string
1497 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001498 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 {
Andrey Andreev4eea9892011-12-19 12:05:41 +02001500 return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001501 }
1502
1503}
1504// END Image_lib Class
1505
1506/* End of file Image_lib.php */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001507/* Location: ./system/libraries/Image_lib.php */