blob: 70a127987b899108f4b619aed51e3e5427418e0a [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 *
114 * @access public
115 * @return void
116 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200117 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Michael Denniscb07a322011-08-20 23:40:59 -0700119 $props = array('library_path', 'source_image', 'new_image', 'width', 'height', 'rotation_angle', 'x_axis', 'y_axis', 'wm_text', 'wm_overlay_path', 'wm_font_path', 'wm_shadow_color', 'source_folder', 'dest_folder', 'mime_type', 'orig_width', 'orig_height', 'image_type', 'size_str', 'full_src_path', 'full_dst_path');
Derek Allard2067d1a2008-11-13 22:59:24 +0000120
121 foreach ($props as $val)
122 {
123 $this->$val = '';
124 }
125
Michael Denniscb07a322011-08-20 23:40:59 -0700126 $this->image_library = 'gd2';
127 $this->dynamic_output = FALSE;
128 $this->quality = '90';
129 $this->create_thumb = FALSE;
130 $this->thumb_marker = '_thumb';
131 $this->maintain_ratio = TRUE;
132 $this->master_dim = 'auto';
133 $this->wm_type = 'text';
134 $this->wm_x_transp = 4;
135 $this->wm_y_transp = 4;
136 $this->wm_font_size = 17;
137 $this->wm_vrt_alignment = 'B';
138 $this->wm_hor_alignment = 'C';
139 $this->wm_padding = 0;
140 $this->wm_hor_offset = 0;
141 $this->wm_vrt_offset = 0;
142 $this->wm_font_color = '#ffffff';
143 $this->wm_shadow_distance = 2;
144 $this->wm_opacity = 50;
145 $this->create_fnc = 'imagecreatetruecolor';
146 $this->copy_fnc = 'imagecopyresampled';
147 $this->error_msg = array();
148 $this->wm_use_drop_shadow = FALSE;
149 $this->wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * initialize image preferences
156 *
157 * @access public
158 * @param array
159 * @return bool
160 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200161 public function initialize($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
163 /*
164 * Convert array elements into class variables
165 */
166 if (count($props) > 0)
167 {
168 foreach ($props as $key => $val)
169 {
170 $this->$key = $val;
171 }
172 }
173
174 /*
175 * Is there a source image?
176 *
177 * If not, there's no reason to continue
178 *
179 */
180 if ($this->source_image == '')
181 {
182 $this->set_error('imglib_source_image_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500183 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185
186 /*
187 * Is getimagesize() Available?
188 *
189 * We use it to determine the image properties (width/height).
Derek Jones4b9c6292011-07-01 17:40:48 -0500190 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * properties using ImageMagick and NetPBM
192 *
193 */
194 if ( ! function_exists('getimagesize'))
195 {
196 $this->set_error('imglib_gd_required_for_props');
197 return FALSE;
198 }
199
200 $this->image_library = strtolower($this->image_library);
201
202 /*
203 * Set the full server path
204 *
205 * The source image may or may not contain a path.
206 * Either way, we'll try use realpath to generate the
207 * full server path in order to more reliably read it.
208 *
209 */
210 if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE)
211 {
212 $full_source_path = str_replace("\\", "/", realpath($this->source_image));
213 }
214 else
215 {
216 $full_source_path = $this->source_image;
217 }
218
219 $x = explode('/', $full_source_path);
220 $this->source_image = end($x);
221 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
222
223 // Set the Image Properties
224 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
225 {
Eric Barnesb1673362011-12-05 22:05:38 -0500226 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
228
229 /*
230 * Assign the "new" image name/path
231 *
232 * If the user has set a "new_image" name it means
233 * we are making a copy of the source image. If not
Derek Jones4b9c6292011-07-01 17:40:48 -0500234 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 * set the destination filename and path accordingly.
236 *
237 */
238 if ($this->new_image == '')
239 {
240 $this->dest_image = $this->source_image;
241 $this->dest_folder = $this->source_folder;
242 }
243 else
244 {
245 if (strpos($this->new_image, '/') === FALSE)
246 {
247 $this->dest_folder = $this->source_folder;
248 $this->dest_image = $this->new_image;
249 }
250 else
251 {
252 if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE)
253 {
254 $full_dest_path = str_replace("\\", "/", realpath($this->new_image));
255 }
256 else
257 {
258 $full_dest_path = $this->new_image;
259 }
260
261 // Is there a file name?
262 if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
263 {
264 $this->dest_folder = $full_dest_path.'/';
265 $this->dest_image = $this->source_image;
266 }
267 else
268 {
269 $x = explode('/', $full_dest_path);
270 $this->dest_image = end($x);
271 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
272 }
273 }
274 }
275
276 /*
277 * Compile the finalized filenames/paths
278 *
279 * We'll create two master strings containing the
280 * full server path to the source image and the
281 * full server path to the destination image.
282 * We'll also split the destination image name
283 * so we can insert the thumbnail marker if needed.
284 *
285 */
286 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
287 {
288 $this->thumb_marker = '';
289 }
290
291 $xp = $this->explode_name($this->dest_image);
292
293 $filename = $xp['name'];
294 $file_ext = $xp['ext'];
295
296 $this->full_src_path = $this->source_folder.$this->source_image;
297 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
298
299 /*
300 * Should we maintain image proportions?
301 *
302 * When creating thumbs or copies, the target width/height
303 * might not be in correct proportion with the source
Derek Jones4b9c6292011-07-01 17:40:48 -0500304 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 *
306 */
307 if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
308 {
309 $this->image_reproportion();
310 }
311
312 /*
313 * Was a width and height specified?
314 *
315 * If the destination width/height was
316 * not submitted we will use the values
317 * from the actual file
318 *
319 */
320 if ($this->width == '')
321 $this->width = $this->orig_width;
322
323 if ($this->height == '')
324 $this->height = $this->orig_height;
325
326 // Set the quality
327 $this->quality = trim(str_replace("%", "", $this->quality));
328
329 if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality))
330 $this->quality = 90;
331
332 // Set the x/y coordinates
333 $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis;
334 $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis;
335
336 // Watermark-related Stuff...
Andrey Andreeva92b9032011-12-24 19:05:58 +0200337 if ($this->wm_font_color != '' AND strlen($this->wm_font_color) === 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200339 $this->wm_font_color = '#'.$this->wm_font_color;
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
341
Andrey Andreeva92b9032011-12-24 19:05:58 +0200342 if ($this->wm_shadow_color != '' AND strlen($this->wm_shadow_color) === 6)
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200344 $this->wm_shadow_color = '#'.$this->wm_shadow_color;
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
346
347 if ($this->wm_overlay_path != '')
348 {
349 $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path));
350 }
351
352 if ($this->wm_shadow_color != '')
353 {
354 $this->wm_use_drop_shadow = TRUE;
355 }
356
357 if ($this->wm_font_path != '')
358 {
359 $this->wm_use_truetype = TRUE;
360 }
361
362 return TRUE;
363 }
364
365 // --------------------------------------------------------------------
366
367 /**
368 * Image Resize
369 *
370 * This is a wrapper function that chooses the proper
371 * resize function based on the protocol specified
372 *
373 * @access public
374 * @return bool
375 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200376 public function resize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200378 $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 return $this->$protocol('resize');
380 }
381
382 // --------------------------------------------------------------------
383
384 /**
385 * Image Crop
386 *
387 * This is a wrapper function that chooses the proper
388 * cropping function based on the protocol specified
389 *
390 * @access public
391 * @return bool
392 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200393 public function crop()
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200395 $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 return $this->$protocol('crop');
397 }
398
399 // --------------------------------------------------------------------
400
401 /**
402 * Image Rotate
403 *
404 * This is a wrapper function that chooses the proper
405 * rotation function based on the protocol specified
406 *
407 * @access public
408 * @return bool
409 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200410 public function rotate()
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
412 // Allowed rotation values
413 $degs = array(90, 180, 270, 'vrt', 'hor');
414
Derek Allardd9c7f032008-12-01 20:18:00 +0000415 if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 $this->set_error('imglib_rotation_angle_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500418 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
420
421 // Reassign the width and height
422 if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
423 {
424 $this->width = $this->orig_height;
425 $this->height = $this->orig_width;
426 }
427 else
428 {
429 $this->width = $this->orig_width;
430 $this->height = $this->orig_height;
431 }
432
433
434 // Choose resizing function
435 if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm')
436 {
437 $protocol = 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 return $this->$protocol('rotate');
439 }
440
441 if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
442 {
443 return $this->image_mirror_gd();
444 }
445 else
446 {
447 return $this->image_rotate_gd();
448 }
449 }
450
451 // --------------------------------------------------------------------
452
453 /**
454 * Image Process Using GD/GD2
455 *
456 * This function will resize or crop
457 *
458 * @access public
459 * @param string
460 * @return bool
461 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200462 public function image_process_gd($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
464 $v2_override = FALSE;
465
466 // If the target width/height match the source, AND if the new file name is not equal to the old file name
467 // 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 +0200468 if ($this->dynamic_output === FALSE AND $this->orig_width == $this->width AND $this->orig_height == $this->height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200470 if ($this->source_image != $this->new_image AND @copy($this->full_src_path, $this->full_dst_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200472 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200474
475 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
477
478 // Let's set up our values based on the action
479 if ($action == 'crop')
480 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500481 // Reassign the source width/height if cropping
482 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 $this->orig_height = $this->height;
484
485 // GD 2.0 has a cropping bug so we'll test for it
486 if ($this->gd_version() !== FALSE)
487 {
488 $gd_version = str_replace('0', '', $this->gd_version());
489 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
490 }
491 }
492 else
493 {
494 // If resizing the x/y axis must be zero
495 $this->x_axis = 0;
496 $this->y_axis = 0;
497 }
498
Derek Jones4b9c6292011-07-01 17:40:48 -0500499 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 if ( ! ($src_img = $this->image_create_gd()))
501 {
502 return FALSE;
503 }
504
Derek Jones4b9c6292011-07-01 17:40:48 -0500505 // Create The Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500507 // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
508 // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
509 // below should that ever prove inaccurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500511 // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200512 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
514 $create = 'imagecreatetruecolor';
515 $copy = 'imagecopyresampled';
516 }
517 else
518 {
519 $create = 'imagecreate';
520 $copy = 'imagecopyresized';
521 }
522
523 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500524
525 if ($this->image_type == 3) // png we can actually preserve transparency
526 {
527 imagealphablending($dst_img, FALSE);
528 imagesavealpha($dst_img, TRUE);
529 }
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
532
Derek Jones4b9c6292011-07-01 17:40:48 -0500533 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 if ($this->dynamic_output == TRUE)
535 {
536 $this->image_display_gd($dst_img);
537 }
538 else
539 {
540 // Or save it
541 if ( ! $this->image_save_gd($dst_img))
542 {
543 return FALSE;
544 }
545 }
546
Derek Jones4b9c6292011-07-01 17:40:48 -0500547 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 imagedestroy($dst_img);
549 imagedestroy($src_img);
550
551 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000552 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000553
554 return TRUE;
555 }
556
557 // --------------------------------------------------------------------
558
559 /**
560 * Image Process Using ImageMagick
561 *
562 * This function will resize, crop or rotate
563 *
564 * @access public
565 * @param string
566 * @return bool
567 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200568 public function image_process_imagemagick($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500570 // Do we have a vaild library path?
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 if ($this->library_path == '')
572 {
573 $this->set_error('imglib_libpath_invalid');
574 return FALSE;
575 }
576
Derek Jones1322ec52009-03-11 17:01:14 +0000577 if ( ! preg_match("/convert$/i", $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200579 $this->library_path = rtrim($this->library_path, '/').'/convert';
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 }
581
582 // Execute the command
583 $cmd = $this->library_path." -quality ".$this->quality;
584
585 if ($action == 'crop')
586 {
587 $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
588 }
589 elseif ($action == 'rotate')
590 {
591 switch ($this->rotation_angle)
592 {
Barry Mienydd671972010-10-04 16:33:58 +0200593 case 'hor' : $angle = '-flop';
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 break;
Barry Mienydd671972010-10-04 16:33:58 +0200595 case 'vrt' : $angle = '-flip';
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 break;
597 default : $angle = '-rotate '.$this->rotation_angle;
598 break;
599 }
600
601 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
602 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500603 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
605 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
606 }
607
608 $retval = 1;
609
610 @exec($cmd, $output, $retval);
611
612 // Did it work?
613 if ($retval > 0)
614 {
615 $this->set_error('imglib_image_process_failed');
616 return FALSE;
617 }
618
619 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000620 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000621
622 return TRUE;
623 }
624
625 // --------------------------------------------------------------------
626
627 /**
628 * Image Process Using NetPBM
629 *
630 * This function will resize, crop or rotate
631 *
632 * @access public
633 * @param string
634 * @return bool
635 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200636 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 {
638 if ($this->library_path == '')
639 {
640 $this->set_error('imglib_libpath_invalid');
641 return FALSE;
642 }
643
Derek Jones4b9c6292011-07-01 17:40:48 -0500644 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 switch ($this->image_type)
646 {
647 case 1 :
648 $cmd_in = 'giftopnm';
649 $cmd_out = 'ppmtogif';
650 break;
651 case 2 :
652 $cmd_in = 'jpegtopnm';
653 $cmd_out = 'ppmtojpeg';
654 break;
655 case 3 :
656 $cmd_in = 'pngtopnm';
657 $cmd_out = 'ppmtopng';
658 break;
659 }
660
661 if ($action == 'crop')
662 {
663 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
664 }
665 elseif ($action == 'rotate')
666 {
667 switch ($this->rotation_angle)
668 {
669 case 90 : $angle = 'r270';
670 break;
671 case 180 : $angle = 'r180';
672 break;
Barry Mienydd671972010-10-04 16:33:58 +0200673 case 270 : $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 break;
675 case 'vrt' : $angle = 'tb';
676 break;
677 case 'hor' : $angle = 'lr';
678 break;
679 }
680
681 $cmd_inner = 'pnmflip -'.$angle.' ';
682 }
683 else // Resize
684 {
685 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
686 }
687
688 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
689
690 $retval = 1;
691
692 @exec($cmd, $output, $retval);
693
Derek Jones4b9c6292011-07-01 17:40:48 -0500694 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 if ($retval > 0)
696 {
697 $this->set_error('imglib_image_process_failed');
698 return FALSE;
699 }
700
701 // With NetPBM we have to create a temporary image.
702 // If you try manipulating the original it fails so
703 // we have to rename the temp file.
704 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
705 unlink ($this->dest_folder.'netpbm.tmp');
Derek Jones172e1612009-10-13 14:32:48 +0000706 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000707
708 return TRUE;
709 }
710
711 // --------------------------------------------------------------------
712
713 /**
714 * Image Rotate Using GD
715 *
716 * @access public
717 * @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 *
769 * @access public
770 * @return bool
771 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200772 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 {
774 if ( ! $src_img = $this->image_create_gd())
775 {
776 return FALSE;
777 }
778
Derek Jones4b9c6292011-07-01 17:40:48 -0500779 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 $height = $this->orig_height;
781
782 if ($this->rotation_angle == 'hor')
783 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200784 for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 while ($left < $right)
787 {
788 $cl = imagecolorat($src_img, $left, $i);
789 $cr = imagecolorat($src_img, $right, $i);
790
791 imagesetpixel($src_img, $left, $i, $cr);
792 imagesetpixel($src_img, $right, $i, $cl);
793
794 $left++;
795 $right--;
796 }
797 }
798 }
799 else
800 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200801 for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 while ($top < $bot)
804 {
805 $ct = imagecolorat($src_img, $i, $top);
806 $cb = imagecolorat($src_img, $i, $bot);
807
808 imagesetpixel($src_img, $i, $top, $cb);
809 imagesetpixel($src_img, $i, $bot, $ct);
810
811 $top++;
812 $bot--;
813 }
814 }
815 }
816
Derek Jones4b9c6292011-07-01 17:40:48 -0500817 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 if ($this->dynamic_output == TRUE)
819 {
820 $this->image_display_gd($src_img);
821 }
822 else
823 {
824 // Or save it
825 if ( ! $this->image_save_gd($src_img))
826 {
827 return FALSE;
828 }
829 }
830
Derek Jones4b9c6292011-07-01 17:40:48 -0500831 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 imagedestroy($src_img);
833
834 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000835 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000836
837 return TRUE;
838 }
839
840 // --------------------------------------------------------------------
841
842 /**
843 * Image Watermark
844 *
845 * This is a wrapper function that chooses the type
846 * of watermarking based on the specified preference.
847 *
848 * @access public
849 * @param string
850 * @return bool
851 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200852 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
854 if ($this->wm_type == 'overlay')
855 {
856 return $this->overlay_watermark();
857 }
858 else
859 {
860 return $this->text_watermark();
861 }
862 }
863
864 // --------------------------------------------------------------------
865
866 /**
867 * Watermark - Graphic Version
868 *
869 * @access public
870 * @return bool
871 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200872 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 {
874 if ( ! function_exists('imagecolortransparent'))
875 {
876 $this->set_error('imglib_gd_required');
877 return FALSE;
878 }
879
Derek Jones4b9c6292011-07-01 17:40:48 -0500880 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 $this->get_image_properties();
882
Derek Jones4b9c6292011-07-01 17:40:48 -0500883 // Fetch watermark image properties
Barry Mienydd671972010-10-04 16:33:58 +0200884 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 $wm_img_type = $props['image_type'];
886 $wm_width = $props['width'];
887 $wm_height = $props['height'];
888
Derek Jones4b9c6292011-07-01 17:40:48 -0500889 // Create two image resources
890 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 $src_img = $this->image_create_gd($this->full_src_path);
892
893 // Reverse the offset if necessary
894 // When the image is positioned at the bottom
895 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -0500896 // further down. We want the reverse, so we'll
897 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 // offset when the image is at the right
899
900 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
901 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
902
903 if ($this->wm_vrt_alignment == 'B')
904 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
905
906 if ($this->wm_hor_alignment == 'R')
907 $this->wm_hor_offset = $this->wm_hor_offset * -1;
908
Derek Jones4b9c6292011-07-01 17:40:48 -0500909 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 $x_axis = $this->wm_hor_offset + $this->wm_padding;
911 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
912
Derek Jones4b9c6292011-07-01 17:40:48 -0500913 // Set the vertical position
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 switch ($this->wm_vrt_alignment)
915 {
916 case 'T':
917 break;
918 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
919 break;
920 case 'B': $y_axis += $this->orig_height - $wm_height;
921 break;
922 }
923
Derek Jones4b9c6292011-07-01 17:40:48 -0500924 // Set the horizontal position
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 switch ($this->wm_hor_alignment)
926 {
927 case 'L':
928 break;
929 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
930 break;
931 case 'R': $x_axis += $this->orig_width - $wm_width;
932 break;
933 }
934
Derek Jones4b9c6292011-07-01 17:40:48 -0500935 // Build the finalized image
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
937 {
938 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200939 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000940
941 // Set RGB values for text and shadow
942 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
943 $alpha = ($rgba & 0x7F000000) >> 24;
944
945 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
946 if ($alpha > 0)
947 {
948 // copy the image directly, the image's alpha transparency being the sole determinant of blending
949 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
950 }
951 else
952 {
953 // set our RGB value from above to be transparent and merge the images with the specified opacity
954 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
955 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
956 }
957
Derek Jones4b9c6292011-07-01 17:40:48 -0500958 // Output the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 if ($this->dynamic_output == TRUE)
960 {
961 $this->image_display_gd($src_img);
962 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200963 elseif ( ! $this->image_save_gd($src_img))
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200965 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 }
967
968 imagedestroy($src_img);
969 imagedestroy($wm_img);
970
971 return TRUE;
972 }
973
974 // --------------------------------------------------------------------
975
976 /**
977 * Watermark - Text Version
978 *
979 * @access public
980 * @return bool
981 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200982 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
984 if ( ! ($src_img = $this->image_create_gd()))
985 {
986 return FALSE;
987 }
988
989 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
990 {
991 $this->set_error('imglib_missing_font');
992 return FALSE;
993 }
994
Derek Jones4b9c6292011-07-01 17:40:48 -0500995 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 $this->get_image_properties();
997
998 // Set RGB values for text and shadow
999 $this->wm_font_color = str_replace('#', '', $this->wm_font_color);
1000 $this->wm_shadow_color = str_replace('#', '', $this->wm_shadow_color);
1001
1002 $R1 = hexdec(substr($this->wm_font_color, 0, 2));
1003 $G1 = hexdec(substr($this->wm_font_color, 2, 2));
1004 $B1 = hexdec(substr($this->wm_font_color, 4, 2));
1005
1006 $R2 = hexdec(substr($this->wm_shadow_color, 0, 2));
1007 $G2 = hexdec(substr($this->wm_shadow_color, 2, 2));
1008 $B2 = hexdec(substr($this->wm_shadow_color, 4, 2));
1009
1010 $txt_color = imagecolorclosest($src_img, $R1, $G1, $B1);
1011 $drp_color = imagecolorclosest($src_img, $R2, $G2, $B2);
1012
1013 // Reverse the vertical offset
1014 // When the image is positioned at the bottom
1015 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -05001016 // further down. We want the reverse, so we'll
1017 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 // offset flips itself automatically
1019
1020 if ($this->wm_vrt_alignment == 'B')
1021 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1022
1023 if ($this->wm_hor_alignment == 'R')
1024 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1025
1026 // Set font width and height
1027 // These are calculated differently depending on
1028 // whether we are using the true type font or not
1029 if ($this->wm_use_truetype == TRUE)
1030 {
1031 if ($this->wm_font_size == '')
Andrey Andreeva92b9032011-12-24 19:05:58 +02001032 {
1033 $this->wm_font_size = 17;
1034 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001035
Derek Jones4b9c6292011-07-01 17:40:48 -05001036 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 $fontheight = $this->wm_font_size;
1038 $this->wm_vrt_offset += $this->wm_font_size;
1039 }
1040 else
1041 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001042 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 $fontheight = imagefontheight($this->wm_font_size);
1044 }
1045
1046 // Set base X and Y axis values
1047 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1048 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1049
1050 // Set verticle alignment
1051 if ($this->wm_use_drop_shadow == FALSE)
1052 $this->wm_shadow_distance = 0;
1053
1054 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1055 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1056
1057 switch ($this->wm_vrt_alignment)
1058 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001059 case 'T':
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001061 case 'M': $y_axis += ($this->orig_height/2)+($fontheight/2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001063 case 'B': $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 break;
1065 }
1066
1067 $x_shad = $x_axis + $this->wm_shadow_distance;
1068 $y_shad = $y_axis + $this->wm_shadow_distance;
1069
1070 // Set horizontal alignment
1071 switch ($this->wm_hor_alignment)
1072 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001073 case 'L':
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001075 case 'R':
1076 if ($this->wm_use_drop_shadow)
1077 {
1078 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1079 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1080 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001082 case 'C':
1083 if ($this->wm_use_drop_shadow)
1084 {
1085 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1086 $x_axis += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1087 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 break;
1089 }
1090
Derek Jones4b9c6292011-07-01 17:40:48 -05001091 // Add the text to the source image
Andrey Andreeva92b9032011-12-24 19:05:58 +02001092 if ($this->wm_use_truetype AND $this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001094 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1095 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 +00001096 }
Andrey Andreeva92b9032011-12-24 19:05:58 +02001097 elseif ($this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001099 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1100 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 }
1102
Derek Jones4b9c6292011-07-01 17:40:48 -05001103 // Output the final image
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 if ($this->dynamic_output == TRUE)
1105 {
1106 $this->image_display_gd($src_img);
1107 }
1108 else
1109 {
1110 $this->image_save_gd($src_img);
1111 }
1112
1113 imagedestroy($src_img);
1114
1115 return TRUE;
1116 }
1117
1118 // --------------------------------------------------------------------
1119
1120 /**
1121 * Create Image - GD
1122 *
1123 * This simply creates an image resource handle
1124 * based on the type of image being processed
1125 *
1126 * @access public
1127 * @param string
1128 * @return resource
1129 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001130 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 {
1132 if ($path == '')
1133 $path = $this->full_src_path;
1134
1135 if ($image_type == '')
1136 $image_type = $this->image_type;
1137
1138
1139 switch ($image_type)
1140 {
1141 case 1 :
1142 if ( ! function_exists('imagecreatefromgif'))
1143 {
1144 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1145 return FALSE;
1146 }
1147
1148 return imagecreatefromgif($path);
1149 break;
1150 case 2 :
1151 if ( ! function_exists('imagecreatefromjpeg'))
1152 {
1153 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1154 return FALSE;
1155 }
1156
1157 return imagecreatefromjpeg($path);
1158 break;
1159 case 3 :
1160 if ( ! function_exists('imagecreatefrompng'))
1161 {
1162 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1163 return FALSE;
1164 }
1165
1166 return imagecreatefrompng($path);
1167 break;
1168
1169 }
1170
1171 $this->set_error(array('imglib_unsupported_imagecreate'));
1172 return FALSE;
1173 }
1174
1175 // --------------------------------------------------------------------
1176
1177 /**
1178 * Write image file to disk - GD
1179 *
1180 * Takes an image resource as input and writes the file
1181 * to the specified destination
1182 *
1183 * @access public
1184 * @param resource
1185 * @return bool
1186 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001187 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 {
1189 switch ($this->image_type)
1190 {
1191 case 1 :
1192 if ( ! function_exists('imagegif'))
1193 {
1194 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1195 return FALSE;
1196 }
1197
Derek Jones541ddbd2008-12-09 15:25:31 +00001198 if ( ! @imagegif($resource, $this->full_dst_path))
1199 {
1200 $this->set_error('imglib_save_failed');
1201 return FALSE;
1202 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 break;
1204 case 2 :
1205 if ( ! function_exists('imagejpeg'))
1206 {
1207 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1208 return FALSE;
1209 }
1210
Derek Jones541ddbd2008-12-09 15:25:31 +00001211 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1212 {
1213 $this->set_error('imglib_save_failed');
1214 return FALSE;
1215 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 break;
1217 case 3 :
1218 if ( ! function_exists('imagepng'))
1219 {
1220 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1221 return FALSE;
1222 }
1223
Derek Jones541ddbd2008-12-09 15:25:31 +00001224 if ( ! @imagepng($resource, $this->full_dst_path))
1225 {
1226 $this->set_error('imglib_save_failed');
1227 return FALSE;
1228 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 break;
1230 default :
1231 $this->set_error(array('imglib_unsupported_imagecreate'));
1232 return FALSE;
1233 break;
1234 }
1235
1236 return TRUE;
1237 }
1238
1239 // --------------------------------------------------------------------
1240
1241 /**
1242 * Dynamically outputs an image
1243 *
1244 * @access public
1245 * @param resource
1246 * @return void
1247 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001248 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 {
1250 header("Content-Disposition: filename={$this->source_image};");
1251 header("Content-Type: {$this->mime_type}");
1252 header('Content-Transfer-Encoding: binary');
1253 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1254
1255 switch ($this->image_type)
1256 {
Barry Mienydd671972010-10-04 16:33:58 +02001257 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 break;
1259 case 2 : imagejpeg($resource, '', $this->quality);
1260 break;
1261 case 3 : imagepng($resource);
1262 break;
1263 default : echo 'Unable to display the image';
1264 break;
1265 }
1266 }
1267
1268 // --------------------------------------------------------------------
1269
1270 /**
1271 * Re-proportion Image Width/Height
1272 *
1273 * When creating thumbs, the desired width/height
1274 * can end up warping the image due to an incorrect
1275 * ratio between the full-sized image and the thumb.
1276 *
1277 * This function lets us re-proportion the width/height
1278 * if users choose to maintain the aspect ratio when resizing.
1279 *
1280 * @access public
1281 * @return void
1282 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001283 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 {
1285 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
1286 return;
1287
1288 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
1289 return;
1290
1291 $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
1292 $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
1293
1294 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
1295
1296 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
1297 {
1298 $this->master_dim = ($ratio < 0) ? 'width' : 'height';
1299 }
1300
1301 if (($this->width != $new_width) AND ($this->height != $new_height))
1302 {
1303 if ($this->master_dim == 'height')
1304 {
1305 $this->width = $new_width;
1306 }
1307 else
1308 {
1309 $this->height = $new_height;
1310 }
1311 }
1312 }
1313
1314 // --------------------------------------------------------------------
1315
1316 /**
1317 * Get image properties
1318 *
1319 * A helper function that gets info about the file
1320 *
1321 * @access public
1322 * @param string
1323 * @return mixed
1324 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001325 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 {
1327 // For now we require GD but we should
1328 // find a way to determine this using IM or NetPBM
1329
1330 if ($path == '')
1331 $path = $this->full_src_path;
1332
1333 if ( ! file_exists($path))
1334 {
1335 $this->set_error('imglib_invalid_path');
1336 return FALSE;
1337 }
1338
Phil Sturgeon901998a2011-08-26 10:03:33 +01001339 $vals = getimagesize($path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
Andrey Andreeva92b9032011-12-24 19:05:58 +02001341 $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001342
1343 if ($return == TRUE)
1344 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001345 return array(
1346 'width' => $vals[0],
1347 'height' => $vals[1],
1348 'image_type' => $vals[2],
1349 'size_str' => $vals[3],
1350 'mime_type' => $mime
1351 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 }
1353
Andrey Andreeva92b9032011-12-24 19:05:58 +02001354 $this->orig_width = $vals[0];
1355 $this->orig_height = $vals[1];
1356 $this->image_type = $vals[2];
1357 $this->size_str = $vals[3];
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 $this->mime_type = $mime;
1359
1360 return TRUE;
1361 }
1362
1363 // --------------------------------------------------------------------
1364
1365 /**
1366 * Size calculator
1367 *
1368 * This function takes a known width x height and
Derek Jones4b9c6292011-07-01 17:40:48 -05001369 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 * new variable needs to be known
1371 *
1372 * $props = array(
Barry Mienydd671972010-10-04 16:33:58 +02001373 * 'width' => $width,
1374 * 'height' => $height,
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 * 'new_width' => 40,
1376 * 'new_height' => ''
Derek Jones4b9c6292011-07-01 17:40:48 -05001377 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 *
1379 * @access public
1380 * @param array
1381 * @return array
1382 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001383 public function size_calculator($vals)
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 {
1385 if ( ! is_array($vals))
1386 {
1387 return;
1388 }
1389
1390 $allowed = array('new_width', 'new_height', 'width', 'height');
1391
1392 foreach ($allowed as $item)
1393 {
1394 if ( ! isset($vals[$item]) OR $vals[$item] == '')
1395 $vals[$item] = 0;
1396 }
1397
1398 if ($vals['width'] == 0 OR $vals['height'] == 0)
1399 {
1400 return $vals;
1401 }
1402
1403 if ($vals['new_width'] == 0)
1404 {
1405 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1406 }
1407 elseif ($vals['new_height'] == 0)
1408 {
1409 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1410 }
1411
1412 return $vals;
1413 }
1414
1415 // --------------------------------------------------------------------
1416
1417 /**
1418 * Explode source_image
1419 *
1420 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001421 * from the source_image. This function lets us deal with
1422 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001423 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001424 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001425 * $array['name'] = 'my.cool';
1426 *
1427 * @access public
1428 * @param array
1429 * @return array
1430 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001431 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001432 {
Derek Jones08cae632009-02-10 20:03:29 +00001433 $ext = strrchr($source_image, '.');
1434 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001435
Derek Jones08cae632009-02-10 20:03:29 +00001436 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001437 }
1438
1439 // --------------------------------------------------------------------
1440
1441 /**
1442 * Is GD Installed?
1443 *
1444 * @access public
1445 * @return bool
1446 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001447 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 {
1449 if ( ! extension_loaded('gd'))
1450 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001451 /* As it is stated in the PHP manual, dl() is not always available
1452 * and even if so - it could generate an E_WARNING message on failure
1453 */
1454 return (function_exists('dl') AND @dl('gd.so'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 }
1456
1457 return TRUE;
1458 }
1459
1460 // --------------------------------------------------------------------
1461
1462 /**
1463 * Get GD version
1464 *
1465 * @access public
1466 * @return mixed
1467 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001468 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 {
1470 if (function_exists('gd_info'))
1471 {
1472 $gd_version = @gd_info();
1473 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
1474
1475 return $gd_version;
1476 }
1477
1478 return FALSE;
1479 }
1480
1481 // --------------------------------------------------------------------
1482
1483 /**
1484 * Set error message
1485 *
1486 * @access public
1487 * @param string
1488 * @return void
1489 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001490 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001491 {
1492 $CI =& get_instance();
1493 $CI->lang->load('imglib');
1494
1495 if (is_array($msg))
1496 {
1497 foreach ($msg as $val)
1498 {
1499
1500 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1501 $this->error_msg[] = $msg;
1502 log_message('error', $msg);
1503 }
1504 }
1505 else
1506 {
1507 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1508 $this->error_msg[] = $msg;
1509 log_message('error', $msg);
1510 }
1511 }
1512
1513 // --------------------------------------------------------------------
1514
1515 /**
1516 * Show error messages
1517 *
1518 * @access public
1519 * @param string
1520 * @return string
1521 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001522 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 {
Andrey Andreev4eea9892011-12-19 12:05:41 +02001524 return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 }
1526
1527}
1528// END Image_lib Class
1529
1530/* End of file Image_lib.php */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001531/* Location: ./system/libraries/Image_lib.php */