blob: 72621c9b4f8670bd3158890c35ff73474d92f260 [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
Andrey Andreev64dbdfb2011-12-30 14:14:07 +020070 protected $wm_font_color = '#ffffff'; // Text color
71 protected $wm_shadow_color = ''; // Dropshadow color
Andrey Andreev3a459572011-12-21 11:23:11 +020072 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();
Andrey Andreev64dbdfb2011-12-30 14:14:07 +020088 protected $wm_use_drop_shadow = FALSE;
Andrey Andreev3a459572011-12-21 11:23:11 +020089 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 {
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200168 if (property_exists($this, $key))
169 {
170 if (in_array($key, array('wm_font_color', 'wm_shadow_color')))
171 {
172 if ($val != '' AND preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches))
173 {
Andrey Andreeva948f072011-12-30 14:22:50 +0200174 $val = (strlen($matches[1]) === 6) ? '#'.$val : '#'.$val[0].$val[0].$val[1].$val[1].$val[2].$val[2];
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200175 }
176 else
177 {
178 continue;
179 }
180 }
181
182 $this->$key = $val;
183 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185 }
186
187 /*
188 * Is there a source image?
189 *
190 * If not, there's no reason to continue
191 *
192 */
193 if ($this->source_image == '')
194 {
195 $this->set_error('imglib_source_image_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500196 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
198
199 /*
200 * Is getimagesize() Available?
201 *
202 * We use it to determine the image properties (width/height).
Derek Jones4b9c6292011-07-01 17:40:48 -0500203 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 * properties using ImageMagick and NetPBM
205 *
206 */
207 if ( ! function_exists('getimagesize'))
208 {
209 $this->set_error('imglib_gd_required_for_props');
210 return FALSE;
211 }
212
213 $this->image_library = strtolower($this->image_library);
214
215 /*
216 * Set the full server path
217 *
218 * The source image may or may not contain a path.
219 * Either way, we'll try use realpath to generate the
220 * full server path in order to more reliably read it.
221 *
222 */
223 if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE)
224 {
225 $full_source_path = str_replace("\\", "/", realpath($this->source_image));
226 }
227 else
228 {
229 $full_source_path = $this->source_image;
230 }
231
232 $x = explode('/', $full_source_path);
233 $this->source_image = end($x);
234 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
235
236 // Set the Image Properties
237 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
238 {
Eric Barnesb1673362011-12-05 22:05:38 -0500239 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241
242 /*
243 * Assign the "new" image name/path
244 *
245 * If the user has set a "new_image" name it means
246 * we are making a copy of the source image. If not
Derek Jones4b9c6292011-07-01 17:40:48 -0500247 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 * set the destination filename and path accordingly.
249 *
250 */
251 if ($this->new_image == '')
252 {
253 $this->dest_image = $this->source_image;
254 $this->dest_folder = $this->source_folder;
255 }
256 else
257 {
258 if (strpos($this->new_image, '/') === FALSE)
259 {
260 $this->dest_folder = $this->source_folder;
261 $this->dest_image = $this->new_image;
262 }
263 else
264 {
265 if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE)
266 {
267 $full_dest_path = str_replace("\\", "/", realpath($this->new_image));
268 }
269 else
270 {
271 $full_dest_path = $this->new_image;
272 }
273
274 // Is there a file name?
275 if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
276 {
277 $this->dest_folder = $full_dest_path.'/';
278 $this->dest_image = $this->source_image;
279 }
280 else
281 {
282 $x = explode('/', $full_dest_path);
283 $this->dest_image = end($x);
284 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
285 }
286 }
287 }
288
289 /*
290 * Compile the finalized filenames/paths
291 *
292 * We'll create two master strings containing the
293 * full server path to the source image and the
294 * full server path to the destination image.
295 * We'll also split the destination image name
296 * so we can insert the thumbnail marker if needed.
297 *
298 */
299 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
300 {
301 $this->thumb_marker = '';
302 }
303
304 $xp = $this->explode_name($this->dest_image);
305
306 $filename = $xp['name'];
307 $file_ext = $xp['ext'];
308
309 $this->full_src_path = $this->source_folder.$this->source_image;
310 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
311
312 /*
313 * Should we maintain image proportions?
314 *
315 * When creating thumbs or copies, the target width/height
316 * might not be in correct proportion with the source
Derek Jones4b9c6292011-07-01 17:40:48 -0500317 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 *
319 */
320 if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
321 {
322 $this->image_reproportion();
323 }
324
325 /*
326 * Was a width and height specified?
327 *
328 * If the destination width/height was
329 * not submitted we will use the values
330 * from the actual file
331 *
332 */
333 if ($this->width == '')
334 $this->width = $this->orig_width;
335
336 if ($this->height == '')
337 $this->height = $this->orig_height;
338
339 // Set the quality
340 $this->quality = trim(str_replace("%", "", $this->quality));
341
342 if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality))
343 $this->quality = 90;
344
345 // Set the x/y coordinates
346 $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis;
347 $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis;
348
349 // Watermark-related Stuff...
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 if ($this->wm_overlay_path != '')
351 {
352 $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path));
353 }
354
355 if ($this->wm_shadow_color != '')
356 {
357 $this->wm_use_drop_shadow = TRUE;
358 }
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200359 elseif ($this->wm_use_drop_shadow == TRUE AND $this->wm_shadow_color == '')
360 {
361 $this->wm_use_drop_shadow = FALSE;
362 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000363
364 if ($this->wm_font_path != '')
365 {
366 $this->wm_use_truetype = TRUE;
367 }
368
369 return TRUE;
370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * Image Resize
376 *
377 * This is a wrapper function that chooses the proper
378 * resize function based on the protocol specified
379 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 * @return bool
381 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200382 public function resize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200384 $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 return $this->$protocol('resize');
386 }
387
388 // --------------------------------------------------------------------
389
390 /**
391 * Image Crop
392 *
393 * This is a wrapper function that chooses the proper
394 * cropping function based on the protocol specified
395 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * @return bool
397 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200398 public function crop()
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200400 $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 return $this->$protocol('crop');
402 }
403
404 // --------------------------------------------------------------------
405
406 /**
407 * Image Rotate
408 *
409 * This is a wrapper function that chooses the proper
410 * rotation function based on the protocol specified
411 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * @return bool
413 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200414 public function rotate()
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
416 // Allowed rotation values
417 $degs = array(90, 180, 270, 'vrt', 'hor');
418
Derek Allardd9c7f032008-12-01 20:18:00 +0000419 if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
421 $this->set_error('imglib_rotation_angle_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500422 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 }
424
425 // Reassign the width and height
426 if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
427 {
428 $this->width = $this->orig_height;
429 $this->height = $this->orig_width;
430 }
431 else
432 {
433 $this->width = $this->orig_width;
434 $this->height = $this->orig_height;
435 }
436
437
438 // Choose resizing function
439 if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm')
440 {
441 $protocol = 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 return $this->$protocol('rotate');
443 }
444
445 if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
446 {
447 return $this->image_mirror_gd();
448 }
449 else
450 {
451 return $this->image_rotate_gd();
452 }
453 }
454
455 // --------------------------------------------------------------------
456
457 /**
458 * Image Process Using GD/GD2
459 *
460 * This function will resize or crop
461 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 * @param string
463 * @return bool
464 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200465 public function image_process_gd($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
467 $v2_override = FALSE;
468
469 // If the target width/height match the source, AND if the new file name is not equal to the old file name
470 // 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 +0200471 if ($this->dynamic_output === FALSE AND $this->orig_width == $this->width AND $this->orig_height == $this->height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200473 if ($this->source_image != $this->new_image AND @copy($this->full_src_path, $this->full_dst_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200475 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200477
478 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480
481 // Let's set up our values based on the action
482 if ($action == 'crop')
483 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500484 // Reassign the source width/height if cropping
485 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 $this->orig_height = $this->height;
487
488 // GD 2.0 has a cropping bug so we'll test for it
489 if ($this->gd_version() !== FALSE)
490 {
491 $gd_version = str_replace('0', '', $this->gd_version());
492 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
493 }
494 }
495 else
496 {
497 // If resizing the x/y axis must be zero
498 $this->x_axis = 0;
499 $this->y_axis = 0;
500 }
501
Derek Jones4b9c6292011-07-01 17:40:48 -0500502 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 if ( ! ($src_img = $this->image_create_gd()))
504 {
505 return FALSE;
506 }
507
Derek Jones4b9c6292011-07-01 17:40:48 -0500508 // Create The Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500510 // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
511 // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
512 // below should that ever prove inaccurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500514 // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200515 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
517 $create = 'imagecreatetruecolor';
518 $copy = 'imagecopyresampled';
519 }
520 else
521 {
522 $create = 'imagecreate';
523 $copy = 'imagecopyresized';
524 }
525
526 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500527
528 if ($this->image_type == 3) // png we can actually preserve transparency
529 {
530 imagealphablending($dst_img, FALSE);
531 imagesavealpha($dst_img, TRUE);
532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
535
Derek Jones4b9c6292011-07-01 17:40:48 -0500536 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 if ($this->dynamic_output == TRUE)
538 {
539 $this->image_display_gd($dst_img);
540 }
541 else
542 {
543 // Or save it
544 if ( ! $this->image_save_gd($dst_img))
545 {
546 return FALSE;
547 }
548 }
549
Derek Jones4b9c6292011-07-01 17:40:48 -0500550 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 imagedestroy($dst_img);
552 imagedestroy($src_img);
553
554 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000555 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000556
557 return TRUE;
558 }
559
560 // --------------------------------------------------------------------
561
562 /**
563 * Image Process Using ImageMagick
564 *
565 * This function will resize, crop or rotate
566 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 * @param string
568 * @return bool
569 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200570 public function image_process_imagemagick($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500572 // Do we have a vaild library path?
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 if ($this->library_path == '')
574 {
575 $this->set_error('imglib_libpath_invalid');
576 return FALSE;
577 }
578
Derek Jones1322ec52009-03-11 17:01:14 +0000579 if ( ! preg_match("/convert$/i", $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200581 $this->library_path = rtrim($this->library_path, '/').'/convert';
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 }
583
584 // Execute the command
585 $cmd = $this->library_path." -quality ".$this->quality;
586
587 if ($action == 'crop')
588 {
589 $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
590 }
591 elseif ($action == 'rotate')
592 {
593 switch ($this->rotation_angle)
594 {
Barry Mienydd671972010-10-04 16:33:58 +0200595 case 'hor' : $angle = '-flop';
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 break;
Barry Mienydd671972010-10-04 16:33:58 +0200597 case 'vrt' : $angle = '-flip';
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 break;
599 default : $angle = '-rotate '.$this->rotation_angle;
600 break;
601 }
602
603 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
604 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500605 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
607 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
608 }
609
610 $retval = 1;
611
612 @exec($cmd, $output, $retval);
613
614 // Did it work?
615 if ($retval > 0)
616 {
617 $this->set_error('imglib_image_process_failed');
618 return FALSE;
619 }
620
621 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000622 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000623
624 return TRUE;
625 }
626
627 // --------------------------------------------------------------------
628
629 /**
630 * Image Process Using NetPBM
631 *
632 * This function will resize, crop or rotate
633 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 * @param string
635 * @return bool
636 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200637 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
639 if ($this->library_path == '')
640 {
641 $this->set_error('imglib_libpath_invalid');
642 return FALSE;
643 }
644
Derek Jones4b9c6292011-07-01 17:40:48 -0500645 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 switch ($this->image_type)
647 {
648 case 1 :
649 $cmd_in = 'giftopnm';
650 $cmd_out = 'ppmtogif';
651 break;
652 case 2 :
653 $cmd_in = 'jpegtopnm';
654 $cmd_out = 'ppmtojpeg';
655 break;
656 case 3 :
657 $cmd_in = 'pngtopnm';
658 $cmd_out = 'ppmtopng';
659 break;
660 }
661
662 if ($action == 'crop')
663 {
664 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
665 }
666 elseif ($action == 'rotate')
667 {
668 switch ($this->rotation_angle)
669 {
670 case 90 : $angle = 'r270';
671 break;
672 case 180 : $angle = 'r180';
673 break;
Barry Mienydd671972010-10-04 16:33:58 +0200674 case 270 : $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 break;
676 case 'vrt' : $angle = 'tb';
677 break;
678 case 'hor' : $angle = 'lr';
679 break;
680 }
681
682 $cmd_inner = 'pnmflip -'.$angle.' ';
683 }
684 else // Resize
685 {
686 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
687 }
688
689 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
690
691 $retval = 1;
692
693 @exec($cmd, $output, $retval);
694
Derek Jones4b9c6292011-07-01 17:40:48 -0500695 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 if ($retval > 0)
697 {
698 $this->set_error('imglib_image_process_failed');
699 return FALSE;
700 }
701
702 // With NetPBM we have to create a temporary image.
703 // If you try manipulating the original it fails so
704 // we have to rename the temp file.
705 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
706 unlink ($this->dest_folder.'netpbm.tmp');
Derek Jones172e1612009-10-13 14:32:48 +0000707 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000708
709 return TRUE;
710 }
711
712 // --------------------------------------------------------------------
713
714 /**
715 * Image Rotate Using GD
716 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 * @return bool
718 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200719 public function image_rotate_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500721 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 if ( ! ($src_img = $this->image_create_gd()))
723 {
724 return FALSE;
725 }
726
727 // Set the background color
728 // This won't work with transparent PNG files so we are
729 // going to have to figure out how to determine the color
730 // of the alpha channel in a future release.
731
732 $white = imagecolorallocate($src_img, 255, 255, 255);
733
Derek Jones4b9c6292011-07-01 17:40:48 -0500734 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
736
Derek Jones4b9c6292011-07-01 17:40:48 -0500737 // Save the Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 if ($this->dynamic_output == TRUE)
739 {
740 $this->image_display_gd($dst_img);
741 }
742 else
743 {
744 // Or save it
745 if ( ! $this->image_save_gd($dst_img))
746 {
747 return FALSE;
748 }
749 }
750
Derek Jones4b9c6292011-07-01 17:40:48 -0500751 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 imagedestroy($dst_img);
753 imagedestroy($src_img);
754
755 // Set the file to 777
756
Derek Jones172e1612009-10-13 14:32:48 +0000757 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000758
Pascal Kriete8761ef52011-02-14 13:13:52 -0500759 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 }
761
762 // --------------------------------------------------------------------
763
764 /**
765 * Create Mirror Image using GD
766 *
767 * This function will flip horizontal or vertical
768 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 * @return bool
770 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200771 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 {
773 if ( ! $src_img = $this->image_create_gd())
774 {
775 return FALSE;
776 }
777
Derek Jones4b9c6292011-07-01 17:40:48 -0500778 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 $height = $this->orig_height;
780
781 if ($this->rotation_angle == 'hor')
782 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200783 for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 while ($left < $right)
786 {
787 $cl = imagecolorat($src_img, $left, $i);
788 $cr = imagecolorat($src_img, $right, $i);
789
790 imagesetpixel($src_img, $left, $i, $cr);
791 imagesetpixel($src_img, $right, $i, $cl);
792
793 $left++;
794 $right--;
795 }
796 }
797 }
798 else
799 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200800 for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 while ($top < $bot)
803 {
804 $ct = imagecolorat($src_img, $i, $top);
805 $cb = imagecolorat($src_img, $i, $bot);
806
807 imagesetpixel($src_img, $i, $top, $cb);
808 imagesetpixel($src_img, $i, $bot, $ct);
809
810 $top++;
811 $bot--;
812 }
813 }
814 }
815
Derek Jones4b9c6292011-07-01 17:40:48 -0500816 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 if ($this->dynamic_output == TRUE)
818 {
819 $this->image_display_gd($src_img);
820 }
821 else
822 {
823 // Or save it
824 if ( ! $this->image_save_gd($src_img))
825 {
826 return FALSE;
827 }
828 }
829
Derek Jones4b9c6292011-07-01 17:40:48 -0500830 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 imagedestroy($src_img);
832
833 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000834 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000835
836 return TRUE;
837 }
838
839 // --------------------------------------------------------------------
840
841 /**
842 * Image Watermark
843 *
844 * This is a wrapper function that chooses the type
845 * of watermarking based on the specified preference.
846 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 * @param string
848 * @return bool
849 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200850 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 {
852 if ($this->wm_type == 'overlay')
853 {
854 return $this->overlay_watermark();
855 }
856 else
857 {
858 return $this->text_watermark();
859 }
860 }
861
862 // --------------------------------------------------------------------
863
864 /**
865 * Watermark - Graphic Version
866 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 * @return bool
868 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200869 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 {
871 if ( ! function_exists('imagecolortransparent'))
872 {
873 $this->set_error('imglib_gd_required');
874 return FALSE;
875 }
876
Derek Jones4b9c6292011-07-01 17:40:48 -0500877 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 $this->get_image_properties();
879
Derek Jones4b9c6292011-07-01 17:40:48 -0500880 // Fetch watermark image properties
Barry Mienydd671972010-10-04 16:33:58 +0200881 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 $wm_img_type = $props['image_type'];
883 $wm_width = $props['width'];
884 $wm_height = $props['height'];
885
Derek Jones4b9c6292011-07-01 17:40:48 -0500886 // Create two image resources
887 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 $src_img = $this->image_create_gd($this->full_src_path);
889
890 // Reverse the offset if necessary
891 // When the image is positioned at the bottom
892 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -0500893 // further down. We want the reverse, so we'll
894 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 // offset when the image is at the right
896
897 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
898 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
899
900 if ($this->wm_vrt_alignment == 'B')
901 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
902
903 if ($this->wm_hor_alignment == 'R')
904 $this->wm_hor_offset = $this->wm_hor_offset * -1;
905
Derek Jones4b9c6292011-07-01 17:40:48 -0500906 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 $x_axis = $this->wm_hor_offset + $this->wm_padding;
908 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
909
Derek Jones4b9c6292011-07-01 17:40:48 -0500910 // Set the vertical position
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 switch ($this->wm_vrt_alignment)
912 {
913 case 'T':
914 break;
915 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
916 break;
917 case 'B': $y_axis += $this->orig_height - $wm_height;
918 break;
919 }
920
Derek Jones4b9c6292011-07-01 17:40:48 -0500921 // Set the horizontal position
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 switch ($this->wm_hor_alignment)
923 {
924 case 'L':
925 break;
926 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
927 break;
928 case 'R': $x_axis += $this->orig_width - $wm_width;
929 break;
930 }
931
Derek Jones4b9c6292011-07-01 17:40:48 -0500932 // Build the finalized image
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
934 {
935 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200936 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000937
938 // Set RGB values for text and shadow
939 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
940 $alpha = ($rgba & 0x7F000000) >> 24;
941
942 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
943 if ($alpha > 0)
944 {
945 // copy the image directly, the image's alpha transparency being the sole determinant of blending
946 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
947 }
948 else
949 {
950 // set our RGB value from above to be transparent and merge the images with the specified opacity
951 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
952 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
953 }
954
Derek Jones4b9c6292011-07-01 17:40:48 -0500955 // Output the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 if ($this->dynamic_output == TRUE)
957 {
958 $this->image_display_gd($src_img);
959 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200960 elseif ( ! $this->image_save_gd($src_img))
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200962 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 }
964
965 imagedestroy($src_img);
966 imagedestroy($wm_img);
967
968 return TRUE;
969 }
970
971 // --------------------------------------------------------------------
972
973 /**
974 * Watermark - Text Version
975 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 * @return bool
977 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200978 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 {
980 if ( ! ($src_img = $this->image_create_gd()))
981 {
982 return FALSE;
983 }
984
985 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
986 {
987 $this->set_error('imglib_missing_font');
988 return FALSE;
989 }
990
Derek Jones4b9c6292011-07-01 17:40:48 -0500991 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 $this->get_image_properties();
993
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 // Reverse the vertical offset
995 // When the image is positioned at the bottom
996 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -0500997 // further down. We want the reverse, so we'll
998 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 // offset flips itself automatically
1000
1001 if ($this->wm_vrt_alignment == 'B')
1002 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1003
1004 if ($this->wm_hor_alignment == 'R')
1005 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1006
1007 // Set font width and height
1008 // These are calculated differently depending on
1009 // whether we are using the true type font or not
1010 if ($this->wm_use_truetype == TRUE)
1011 {
1012 if ($this->wm_font_size == '')
Andrey Andreeva92b9032011-12-24 19:05:58 +02001013 {
1014 $this->wm_font_size = 17;
1015 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001016
Derek Jones4b9c6292011-07-01 17:40:48 -05001017 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 $fontheight = $this->wm_font_size;
1019 $this->wm_vrt_offset += $this->wm_font_size;
1020 }
1021 else
1022 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001023 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 $fontheight = imagefontheight($this->wm_font_size);
1025 }
1026
1027 // Set base X and Y axis values
1028 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1029 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1030
1031 // Set verticle alignment
1032 if ($this->wm_use_drop_shadow == FALSE)
1033 $this->wm_shadow_distance = 0;
1034
1035 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1036 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1037
1038 switch ($this->wm_vrt_alignment)
1039 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001040 case 'T':
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001042 case 'M': $y_axis += ($this->orig_height/2)+($fontheight/2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001044 case 'B': $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 break;
1046 }
1047
1048 $x_shad = $x_axis + $this->wm_shadow_distance;
1049 $y_shad = $y_axis + $this->wm_shadow_distance;
1050
1051 // Set horizontal alignment
1052 switch ($this->wm_hor_alignment)
1053 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001054 case 'L':
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001056 case 'R':
1057 if ($this->wm_use_drop_shadow)
1058 {
1059 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1060 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1061 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001063 case 'C':
1064 if ($this->wm_use_drop_shadow)
1065 {
1066 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1067 $x_axis += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1068 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 break;
1070 }
1071
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001072 if ($this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 {
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001074 // Set RGB values for text and shadow
1075 $txt_color = str_split(substr($this->wm_font_color, 1, 6));
1076 $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
1077 $drp_color = str_split(substr($this->wm_shadow_color, 1, 6));
1078 $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[2]), hexdec($drp_color[3]));
1079
1080 // Add the text to the source image
1081 if ($this->wm_use_truetype)
1082 {
1083 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1084 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1085 }
1086 else
1087 {
1088 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1089 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1090 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 }
1092
Derek Jones4b9c6292011-07-01 17:40:48 -05001093 // Output the final image
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 if ($this->dynamic_output == TRUE)
1095 {
1096 $this->image_display_gd($src_img);
1097 }
1098 else
1099 {
1100 $this->image_save_gd($src_img);
1101 }
1102
1103 imagedestroy($src_img);
1104
1105 return TRUE;
1106 }
1107
1108 // --------------------------------------------------------------------
1109
1110 /**
1111 * Create Image - GD
1112 *
1113 * This simply creates an image resource handle
1114 * based on the type of image being processed
1115 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 * @param string
1117 * @return resource
1118 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001119 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 {
1121 if ($path == '')
1122 $path = $this->full_src_path;
1123
1124 if ($image_type == '')
1125 $image_type = $this->image_type;
1126
1127
1128 switch ($image_type)
1129 {
1130 case 1 :
1131 if ( ! function_exists('imagecreatefromgif'))
1132 {
1133 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1134 return FALSE;
1135 }
1136
1137 return imagecreatefromgif($path);
1138 break;
1139 case 2 :
1140 if ( ! function_exists('imagecreatefromjpeg'))
1141 {
1142 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1143 return FALSE;
1144 }
1145
1146 return imagecreatefromjpeg($path);
1147 break;
1148 case 3 :
1149 if ( ! function_exists('imagecreatefrompng'))
1150 {
1151 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1152 return FALSE;
1153 }
1154
1155 return imagecreatefrompng($path);
1156 break;
1157
1158 }
1159
1160 $this->set_error(array('imglib_unsupported_imagecreate'));
1161 return FALSE;
1162 }
1163
1164 // --------------------------------------------------------------------
1165
1166 /**
1167 * Write image file to disk - GD
1168 *
1169 * Takes an image resource as input and writes the file
1170 * to the specified destination
1171 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 * @param resource
1173 * @return bool
1174 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001175 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 {
1177 switch ($this->image_type)
1178 {
1179 case 1 :
1180 if ( ! function_exists('imagegif'))
1181 {
1182 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1183 return FALSE;
1184 }
1185
Derek Jones541ddbd2008-12-09 15:25:31 +00001186 if ( ! @imagegif($resource, $this->full_dst_path))
1187 {
1188 $this->set_error('imglib_save_failed');
1189 return FALSE;
1190 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 break;
1192 case 2 :
1193 if ( ! function_exists('imagejpeg'))
1194 {
1195 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1196 return FALSE;
1197 }
1198
Derek Jones541ddbd2008-12-09 15:25:31 +00001199 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1200 {
1201 $this->set_error('imglib_save_failed');
1202 return FALSE;
1203 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 break;
1205 case 3 :
1206 if ( ! function_exists('imagepng'))
1207 {
1208 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1209 return FALSE;
1210 }
1211
Derek Jones541ddbd2008-12-09 15:25:31 +00001212 if ( ! @imagepng($resource, $this->full_dst_path))
1213 {
1214 $this->set_error('imglib_save_failed');
1215 return FALSE;
1216 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 break;
1218 default :
1219 $this->set_error(array('imglib_unsupported_imagecreate'));
1220 return FALSE;
1221 break;
1222 }
1223
1224 return TRUE;
1225 }
1226
1227 // --------------------------------------------------------------------
1228
1229 /**
1230 * Dynamically outputs an image
1231 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 * @param resource
1233 * @return void
1234 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001235 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 {
1237 header("Content-Disposition: filename={$this->source_image};");
1238 header("Content-Type: {$this->mime_type}");
1239 header('Content-Transfer-Encoding: binary');
1240 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1241
1242 switch ($this->image_type)
1243 {
Barry Mienydd671972010-10-04 16:33:58 +02001244 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 break;
1246 case 2 : imagejpeg($resource, '', $this->quality);
1247 break;
1248 case 3 : imagepng($resource);
1249 break;
1250 default : echo 'Unable to display the image';
1251 break;
1252 }
1253 }
1254
1255 // --------------------------------------------------------------------
1256
1257 /**
1258 * Re-proportion Image Width/Height
1259 *
1260 * When creating thumbs, the desired width/height
1261 * can end up warping the image due to an incorrect
1262 * ratio between the full-sized image and the thumb.
1263 *
1264 * This function lets us re-proportion the width/height
1265 * if users choose to maintain the aspect ratio when resizing.
1266 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 * @return void
1268 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001269 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 {
1271 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
1272 return;
1273
1274 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
1275 return;
1276
1277 $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
1278 $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
1279
1280 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
1281
1282 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
1283 {
1284 $this->master_dim = ($ratio < 0) ? 'width' : 'height';
1285 }
1286
1287 if (($this->width != $new_width) AND ($this->height != $new_height))
1288 {
1289 if ($this->master_dim == 'height')
1290 {
1291 $this->width = $new_width;
1292 }
1293 else
1294 {
1295 $this->height = $new_height;
1296 }
1297 }
1298 }
1299
1300 // --------------------------------------------------------------------
1301
1302 /**
1303 * Get image properties
1304 *
1305 * A helper function that gets info about the file
1306 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 * @param string
1308 * @return mixed
1309 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001310 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 {
1312 // For now we require GD but we should
1313 // find a way to determine this using IM or NetPBM
1314
1315 if ($path == '')
1316 $path = $this->full_src_path;
1317
1318 if ( ! file_exists($path))
1319 {
1320 $this->set_error('imglib_invalid_path');
1321 return FALSE;
1322 }
1323
Phil Sturgeon901998a2011-08-26 10:03:33 +01001324 $vals = getimagesize($path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
Andrey Andreeva92b9032011-12-24 19:05:58 +02001326 $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001327
1328 if ($return == TRUE)
1329 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001330 return array(
1331 'width' => $vals[0],
1332 'height' => $vals[1],
1333 'image_type' => $vals[2],
1334 'size_str' => $vals[3],
1335 'mime_type' => $mime
1336 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 }
1338
Andrey Andreeva92b9032011-12-24 19:05:58 +02001339 $this->orig_width = $vals[0];
1340 $this->orig_height = $vals[1];
1341 $this->image_type = $vals[2];
1342 $this->size_str = $vals[3];
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 $this->mime_type = $mime;
1344
1345 return TRUE;
1346 }
1347
1348 // --------------------------------------------------------------------
1349
1350 /**
1351 * Size calculator
1352 *
1353 * This function takes a known width x height and
Derek Jones4b9c6292011-07-01 17:40:48 -05001354 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 * new variable needs to be known
1356 *
1357 * $props = array(
Barry Mienydd671972010-10-04 16:33:58 +02001358 * 'width' => $width,
1359 * 'height' => $height,
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 * 'new_width' => 40,
1361 * 'new_height' => ''
Derek Jones4b9c6292011-07-01 17:40:48 -05001362 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 * @param array
1365 * @return array
1366 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001367 public function size_calculator($vals)
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 {
1369 if ( ! is_array($vals))
1370 {
1371 return;
1372 }
1373
1374 $allowed = array('new_width', 'new_height', 'width', 'height');
1375
1376 foreach ($allowed as $item)
1377 {
1378 if ( ! isset($vals[$item]) OR $vals[$item] == '')
1379 $vals[$item] = 0;
1380 }
1381
1382 if ($vals['width'] == 0 OR $vals['height'] == 0)
1383 {
1384 return $vals;
1385 }
1386
1387 if ($vals['new_width'] == 0)
1388 {
1389 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1390 }
1391 elseif ($vals['new_height'] == 0)
1392 {
1393 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1394 }
1395
1396 return $vals;
1397 }
1398
1399 // --------------------------------------------------------------------
1400
1401 /**
1402 * Explode source_image
1403 *
1404 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001405 * from the source_image. This function lets us deal with
1406 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001407 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001408 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 * $array['name'] = 'my.cool';
1410 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 * @param array
1412 * @return array
1413 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001414 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 {
Derek Jones08cae632009-02-10 20:03:29 +00001416 $ext = strrchr($source_image, '.');
1417 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001418
Derek Jones08cae632009-02-10 20:03:29 +00001419 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 }
1421
1422 // --------------------------------------------------------------------
1423
1424 /**
1425 * Is GD Installed?
1426 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 * @return bool
1428 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001429 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 {
1431 if ( ! extension_loaded('gd'))
1432 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001433 /* As it is stated in the PHP manual, dl() is not always available
1434 * and even if so - it could generate an E_WARNING message on failure
1435 */
1436 return (function_exists('dl') AND @dl('gd.so'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001437 }
1438
1439 return TRUE;
1440 }
1441
1442 // --------------------------------------------------------------------
1443
1444 /**
1445 * Get GD version
1446 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 * @return mixed
1448 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001449 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001450 {
1451 if (function_exists('gd_info'))
1452 {
1453 $gd_version = @gd_info();
1454 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
1455
1456 return $gd_version;
1457 }
1458
1459 return FALSE;
1460 }
1461
1462 // --------------------------------------------------------------------
1463
1464 /**
1465 * Set error message
1466 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001467 * @param string
1468 * @return void
1469 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001470 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 {
1472 $CI =& get_instance();
1473 $CI->lang->load('imglib');
1474
1475 if (is_array($msg))
1476 {
1477 foreach ($msg as $val)
1478 {
1479
1480 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1481 $this->error_msg[] = $msg;
1482 log_message('error', $msg);
1483 }
1484 }
1485 else
1486 {
1487 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1488 $this->error_msg[] = $msg;
1489 log_message('error', $msg);
1490 }
1491 }
1492
1493 // --------------------------------------------------------------------
1494
1495 /**
1496 * Show error messages
1497 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001498 * @param string
1499 * @return string
1500 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001501 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 {
Andrey Andreev4eea9892011-12-19 12:05:41 +02001503 return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001504 }
1505
1506}
1507// END Image_lib Class
1508
1509/* End of file Image_lib.php */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001510/* Location: ./system/libraries/Image_lib.php */