blob: 39f9887f1941a8752a2730a6e49bc32e6701adce [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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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 {
Andrey Andreev8323ae62011-12-31 18:39:10 +0200172 if (preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches))
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200173 {
Andrey Andreevcf2ba9e2012-01-01 21:57:13 +0200174 /* $matches[1] contains our hex color value, but it might be
Andrey Andreev8323ae62011-12-31 18:39:10 +0200175 * both in the full 6-length format or the shortened 3-length
176 * value.
Andrey Andreevbb96c8b2011-12-31 18:48:39 +0200177 * We'll later need the full version, so we keep it if it's
Andrey Andreev8323ae62011-12-31 18:39:10 +0200178 * already there and if not - we'll convert to it. We can
179 * access string characters by their index as in an array,
180 * so we'll do that and use concatenation to form the final
181 * value:
182 */
Andrey Andreev665af0c2011-12-30 14:39:29 +0200183 $val = (strlen($matches[1]) === 6)
184 ? '#'.$matches[1]
185 : '#'.$matches[1][0].$matches[1][0].$matches[1][1].$matches[1][1].$matches[1][2].$matches[1][2];
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200186 }
187 else
188 {
189 continue;
190 }
191 }
192
193 $this->$key = $val;
194 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
196 }
197
198 /*
199 * Is there a source image?
200 *
201 * If not, there's no reason to continue
202 *
203 */
204 if ($this->source_image == '')
205 {
206 $this->set_error('imglib_source_image_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500207 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
209
210 /*
211 * Is getimagesize() Available?
212 *
213 * We use it to determine the image properties (width/height).
Derek Jones4b9c6292011-07-01 17:40:48 -0500214 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 * properties using ImageMagick and NetPBM
216 *
217 */
218 if ( ! function_exists('getimagesize'))
219 {
220 $this->set_error('imglib_gd_required_for_props');
221 return FALSE;
222 }
223
224 $this->image_library = strtolower($this->image_library);
225
226 /*
227 * Set the full server path
228 *
229 * The source image may or may not contain a path.
230 * Either way, we'll try use realpath to generate the
231 * full server path in order to more reliably read it.
232 *
233 */
234 if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE)
235 {
236 $full_source_path = str_replace("\\", "/", realpath($this->source_image));
237 }
238 else
239 {
240 $full_source_path = $this->source_image;
241 }
242
243 $x = explode('/', $full_source_path);
244 $this->source_image = end($x);
245 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
246
247 // Set the Image Properties
248 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
249 {
Eric Barnesb1673362011-12-05 22:05:38 -0500250 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 }
252
253 /*
254 * Assign the "new" image name/path
255 *
256 * If the user has set a "new_image" name it means
257 * we are making a copy of the source image. If not
Derek Jones4b9c6292011-07-01 17:40:48 -0500258 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 * set the destination filename and path accordingly.
260 *
261 */
262 if ($this->new_image == '')
263 {
264 $this->dest_image = $this->source_image;
265 $this->dest_folder = $this->source_folder;
266 }
267 else
268 {
269 if (strpos($this->new_image, '/') === FALSE)
270 {
271 $this->dest_folder = $this->source_folder;
272 $this->dest_image = $this->new_image;
273 }
274 else
275 {
276 if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE)
277 {
278 $full_dest_path = str_replace("\\", "/", realpath($this->new_image));
279 }
280 else
281 {
282 $full_dest_path = $this->new_image;
283 }
284
285 // Is there a file name?
286 if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
287 {
288 $this->dest_folder = $full_dest_path.'/';
289 $this->dest_image = $this->source_image;
290 }
291 else
292 {
293 $x = explode('/', $full_dest_path);
294 $this->dest_image = end($x);
295 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
296 }
297 }
298 }
299
300 /*
301 * Compile the finalized filenames/paths
302 *
303 * We'll create two master strings containing the
304 * full server path to the source image and the
305 * full server path to the destination image.
306 * We'll also split the destination image name
307 * so we can insert the thumbnail marker if needed.
308 *
309 */
310 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
311 {
312 $this->thumb_marker = '';
313 }
314
315 $xp = $this->explode_name($this->dest_image);
316
317 $filename = $xp['name'];
318 $file_ext = $xp['ext'];
319
320 $this->full_src_path = $this->source_folder.$this->source_image;
321 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
322
323 /*
324 * Should we maintain image proportions?
325 *
326 * When creating thumbs or copies, the target width/height
327 * might not be in correct proportion with the source
Derek Jones4b9c6292011-07-01 17:40:48 -0500328 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 *
330 */
331 if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
332 {
333 $this->image_reproportion();
334 }
335
336 /*
337 * Was a width and height specified?
338 *
339 * If the destination width/height was
340 * not submitted we will use the values
341 * from the actual file
342 *
343 */
344 if ($this->width == '')
345 $this->width = $this->orig_width;
346
347 if ($this->height == '')
348 $this->height = $this->orig_height;
349
350 // Set the quality
351 $this->quality = trim(str_replace("%", "", $this->quality));
352
353 if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality))
354 $this->quality = 90;
355
356 // Set the x/y coordinates
357 $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis;
358 $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis;
359
360 // Watermark-related Stuff...
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 if ($this->wm_overlay_path != '')
362 {
363 $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path));
364 }
365
366 if ($this->wm_shadow_color != '')
367 {
368 $this->wm_use_drop_shadow = TRUE;
369 }
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200370 elseif ($this->wm_use_drop_shadow == TRUE AND $this->wm_shadow_color == '')
371 {
372 $this->wm_use_drop_shadow = FALSE;
373 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000374
375 if ($this->wm_font_path != '')
376 {
377 $this->wm_use_truetype = TRUE;
378 }
379
380 return TRUE;
381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * Image Resize
387 *
388 * This is a wrapper function that chooses the proper
389 * resize function based on the protocol specified
390 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 * @return bool
392 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200393 public function resize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
Andrey Andreev1a9f52c2012-01-07 01:06:34 +0200395 $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 return $this->$protocol('resize');
397 }
398
399 // --------------------------------------------------------------------
400
401 /**
402 * Image Crop
403 *
404 * This is a wrapper function that chooses the proper
405 * cropping function based on the protocol specified
406 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 * @return bool
408 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200409 public function crop()
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200411 $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 return $this->$protocol('crop');
413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * Image Rotate
419 *
420 * This is a wrapper function that chooses the proper
421 * rotation function based on the protocol specified
422 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 * @return bool
424 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200425 public function rotate()
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
427 // Allowed rotation values
428 $degs = array(90, 180, 270, 'vrt', 'hor');
429
Derek Allardd9c7f032008-12-01 20:18:00 +0000430 if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
432 $this->set_error('imglib_rotation_angle_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500433 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
435
436 // Reassign the width and height
437 if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
438 {
439 $this->width = $this->orig_height;
440 $this->height = $this->orig_width;
441 }
442 else
443 {
444 $this->width = $this->orig_width;
445 $this->height = $this->orig_height;
446 }
447
448
449 // Choose resizing function
450 if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm')
451 {
452 $protocol = 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 return $this->$protocol('rotate');
454 }
455
456 if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
457 {
458 return $this->image_mirror_gd();
459 }
460 else
461 {
462 return $this->image_rotate_gd();
463 }
464 }
465
466 // --------------------------------------------------------------------
467
468 /**
469 * Image Process Using GD/GD2
470 *
471 * This function will resize or crop
472 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 * @param string
474 * @return bool
475 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200476 public function image_process_gd($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
478 $v2_override = FALSE;
479
480 // If the target width/height match the source, AND if the new file name is not equal to the old file name
481 // 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 +0200482 if ($this->dynamic_output === FALSE AND $this->orig_width == $this->width AND $this->orig_height == $this->height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200484 if ($this->source_image != $this->new_image AND @copy($this->full_src_path, $this->full_dst_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200486 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200488
489 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
491
492 // Let's set up our values based on the action
493 if ($action == 'crop')
494 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500495 // Reassign the source width/height if cropping
496 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 $this->orig_height = $this->height;
498
499 // GD 2.0 has a cropping bug so we'll test for it
500 if ($this->gd_version() !== FALSE)
501 {
502 $gd_version = str_replace('0', '', $this->gd_version());
503 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
504 }
505 }
506 else
507 {
508 // If resizing the x/y axis must be zero
509 $this->x_axis = 0;
510 $this->y_axis = 0;
511 }
512
Derek Jones4b9c6292011-07-01 17:40:48 -0500513 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 if ( ! ($src_img = $this->image_create_gd()))
515 {
516 return FALSE;
517 }
518
Derek Jones4b9c6292011-07-01 17:40:48 -0500519 // Create The Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500521 // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
522 // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
523 // below should that ever prove inaccurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500525 // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200526 if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 {
528 $create = 'imagecreatetruecolor';
529 $copy = 'imagecopyresampled';
530 }
531 else
532 {
533 $create = 'imagecreate';
534 $copy = 'imagecopyresized';
535 }
536
537 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500538
539 if ($this->image_type == 3) // png we can actually preserve transparency
540 {
541 imagealphablending($dst_img, FALSE);
542 imagesavealpha($dst_img, TRUE);
543 }
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
546
Derek Jones4b9c6292011-07-01 17:40:48 -0500547 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 if ($this->dynamic_output == TRUE)
549 {
550 $this->image_display_gd($dst_img);
551 }
552 else
553 {
554 // Or save it
555 if ( ! $this->image_save_gd($dst_img))
556 {
557 return FALSE;
558 }
559 }
560
Derek Jones4b9c6292011-07-01 17:40:48 -0500561 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 imagedestroy($dst_img);
563 imagedestroy($src_img);
564
565 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000566 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000567
568 return TRUE;
569 }
570
571 // --------------------------------------------------------------------
572
573 /**
574 * Image Process Using ImageMagick
575 *
576 * This function will resize, crop or rotate
577 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 * @param string
579 * @return bool
580 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200581 public function image_process_imagemagick($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500583 // Do we have a vaild library path?
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 if ($this->library_path == '')
585 {
586 $this->set_error('imglib_libpath_invalid');
587 return FALSE;
588 }
589
Derek Jones1322ec52009-03-11 17:01:14 +0000590 if ( ! preg_match("/convert$/i", $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200592 $this->library_path = rtrim($this->library_path, '/').'/convert';
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 }
594
595 // Execute the command
596 $cmd = $this->library_path." -quality ".$this->quality;
597
598 if ($action == 'crop')
599 {
600 $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
601 }
602 elseif ($action == 'rotate')
603 {
604 switch ($this->rotation_angle)
605 {
Barry Mienydd671972010-10-04 16:33:58 +0200606 case 'hor' : $angle = '-flop';
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 break;
Barry Mienydd671972010-10-04 16:33:58 +0200608 case 'vrt' : $angle = '-flip';
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 break;
610 default : $angle = '-rotate '.$this->rotation_angle;
611 break;
612 }
613
614 $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
615 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500616 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 {
618 $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
619 }
620
621 $retval = 1;
622
623 @exec($cmd, $output, $retval);
624
625 // Did it work?
626 if ($retval > 0)
627 {
628 $this->set_error('imglib_image_process_failed');
629 return FALSE;
630 }
631
632 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000633 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000634
635 return TRUE;
636 }
637
638 // --------------------------------------------------------------------
639
640 /**
641 * Image Process Using NetPBM
642 *
643 * This function will resize, crop or rotate
644 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 * @param string
646 * @return bool
647 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200648 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
650 if ($this->library_path == '')
651 {
652 $this->set_error('imglib_libpath_invalid');
653 return FALSE;
654 }
655
Derek Jones4b9c6292011-07-01 17:40:48 -0500656 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 switch ($this->image_type)
658 {
659 case 1 :
660 $cmd_in = 'giftopnm';
661 $cmd_out = 'ppmtogif';
662 break;
663 case 2 :
664 $cmd_in = 'jpegtopnm';
665 $cmd_out = 'ppmtojpeg';
666 break;
667 case 3 :
668 $cmd_in = 'pngtopnm';
669 $cmd_out = 'ppmtopng';
670 break;
671 }
672
673 if ($action == 'crop')
674 {
675 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
676 }
677 elseif ($action == 'rotate')
678 {
679 switch ($this->rotation_angle)
680 {
681 case 90 : $angle = 'r270';
682 break;
683 case 180 : $angle = 'r180';
684 break;
Barry Mienydd671972010-10-04 16:33:58 +0200685 case 270 : $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 break;
687 case 'vrt' : $angle = 'tb';
688 break;
689 case 'hor' : $angle = 'lr';
690 break;
691 }
692
693 $cmd_inner = 'pnmflip -'.$angle.' ';
694 }
695 else // Resize
696 {
697 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
698 }
699
700 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
701
702 $retval = 1;
703
704 @exec($cmd, $output, $retval);
705
Derek Jones4b9c6292011-07-01 17:40:48 -0500706 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 if ($retval > 0)
708 {
709 $this->set_error('imglib_image_process_failed');
710 return FALSE;
711 }
712
713 // With NetPBM we have to create a temporary image.
714 // If you try manipulating the original it fails so
715 // we have to rename the temp file.
716 copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
717 unlink ($this->dest_folder.'netpbm.tmp');
Derek Jones172e1612009-10-13 14:32:48 +0000718 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000719
720 return TRUE;
721 }
722
723 // --------------------------------------------------------------------
724
725 /**
726 * Image Rotate Using GD
727 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 * @return bool
729 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200730 public function image_rotate_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500732 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 if ( ! ($src_img = $this->image_create_gd()))
734 {
735 return FALSE;
736 }
737
738 // Set the background color
739 // This won't work with transparent PNG files so we are
740 // going to have to figure out how to determine the color
741 // of the alpha channel in a future release.
742
743 $white = imagecolorallocate($src_img, 255, 255, 255);
744
Derek Jones4b9c6292011-07-01 17:40:48 -0500745 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
747
Derek Jones4b9c6292011-07-01 17:40:48 -0500748 // Save the Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 if ($this->dynamic_output == TRUE)
750 {
751 $this->image_display_gd($dst_img);
752 }
753 else
754 {
755 // Or save it
756 if ( ! $this->image_save_gd($dst_img))
757 {
758 return FALSE;
759 }
760 }
761
Derek Jones4b9c6292011-07-01 17:40:48 -0500762 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 imagedestroy($dst_img);
764 imagedestroy($src_img);
765
766 // Set the file to 777
767
Derek Jones172e1612009-10-13 14:32:48 +0000768 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000769
Pascal Kriete8761ef52011-02-14 13:13:52 -0500770 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 }
772
773 // --------------------------------------------------------------------
774
775 /**
776 * Create Mirror Image using GD
777 *
778 * This function will flip horizontal or vertical
779 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 * @return bool
781 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200782 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 {
784 if ( ! $src_img = $this->image_create_gd())
785 {
786 return FALSE;
787 }
788
Derek Jones4b9c6292011-07-01 17:40:48 -0500789 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 $height = $this->orig_height;
791
792 if ($this->rotation_angle == 'hor')
793 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200794 for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 while ($left < $right)
797 {
798 $cl = imagecolorat($src_img, $left, $i);
799 $cr = imagecolorat($src_img, $right, $i);
800
801 imagesetpixel($src_img, $left, $i, $cr);
802 imagesetpixel($src_img, $right, $i, $cl);
803
804 $left++;
805 $right--;
806 }
807 }
808 }
809 else
810 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200811 for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 while ($top < $bot)
814 {
815 $ct = imagecolorat($src_img, $i, $top);
816 $cb = imagecolorat($src_img, $i, $bot);
817
818 imagesetpixel($src_img, $i, $top, $cb);
819 imagesetpixel($src_img, $i, $bot, $ct);
820
821 $top++;
822 $bot--;
823 }
824 }
825 }
826
Derek Jones4b9c6292011-07-01 17:40:48 -0500827 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 if ($this->dynamic_output == TRUE)
829 {
830 $this->image_display_gd($src_img);
831 }
832 else
833 {
834 // Or save it
835 if ( ! $this->image_save_gd($src_img))
836 {
837 return FALSE;
838 }
839 }
840
Derek Jones4b9c6292011-07-01 17:40:48 -0500841 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 imagedestroy($src_img);
843
844 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000845 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000846
847 return TRUE;
848 }
849
850 // --------------------------------------------------------------------
851
852 /**
853 * Image Watermark
854 *
855 * This is a wrapper function that chooses the type
856 * of watermarking based on the specified preference.
857 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 * @param string
859 * @return bool
860 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200861 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 {
863 if ($this->wm_type == 'overlay')
864 {
865 return $this->overlay_watermark();
866 }
867 else
868 {
869 return $this->text_watermark();
870 }
871 }
872
873 // --------------------------------------------------------------------
874
875 /**
876 * Watermark - Graphic Version
877 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 * @return bool
879 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200880 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 {
882 if ( ! function_exists('imagecolortransparent'))
883 {
884 $this->set_error('imglib_gd_required');
885 return FALSE;
886 }
887
Derek Jones4b9c6292011-07-01 17:40:48 -0500888 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 $this->get_image_properties();
890
Derek Jones4b9c6292011-07-01 17:40:48 -0500891 // Fetch watermark image properties
Barry Mienydd671972010-10-04 16:33:58 +0200892 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 $wm_img_type = $props['image_type'];
894 $wm_width = $props['width'];
895 $wm_height = $props['height'];
896
Derek Jones4b9c6292011-07-01 17:40:48 -0500897 // Create two image resources
898 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 $src_img = $this->image_create_gd($this->full_src_path);
900
901 // Reverse the offset if necessary
902 // When the image is positioned at the bottom
903 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -0500904 // further down. We want the reverse, so we'll
905 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 // offset when the image is at the right
907
908 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
909 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
910
911 if ($this->wm_vrt_alignment == 'B')
912 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
913
914 if ($this->wm_hor_alignment == 'R')
915 $this->wm_hor_offset = $this->wm_hor_offset * -1;
916
Derek Jones4b9c6292011-07-01 17:40:48 -0500917 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 $x_axis = $this->wm_hor_offset + $this->wm_padding;
919 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
920
Derek Jones4b9c6292011-07-01 17:40:48 -0500921 // Set the vertical position
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 switch ($this->wm_vrt_alignment)
923 {
924 case 'T':
925 break;
926 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
927 break;
928 case 'B': $y_axis += $this->orig_height - $wm_height;
929 break;
930 }
931
Derek Jones4b9c6292011-07-01 17:40:48 -0500932 // Set the horizontal position
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 switch ($this->wm_hor_alignment)
934 {
935 case 'L':
936 break;
937 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
938 break;
939 case 'R': $x_axis += $this->orig_width - $wm_width;
940 break;
941 }
942
Derek Jones4b9c6292011-07-01 17:40:48 -0500943 // Build the finalized image
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 if ($wm_img_type == 3 AND function_exists('imagealphablending'))
945 {
946 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200947 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000948
949 // Set RGB values for text and shadow
950 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
951 $alpha = ($rgba & 0x7F000000) >> 24;
952
953 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
954 if ($alpha > 0)
955 {
956 // copy the image directly, the image's alpha transparency being the sole determinant of blending
957 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
958 }
959 else
960 {
961 // set our RGB value from above to be transparent and merge the images with the specified opacity
962 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
963 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
964 }
965
Derek Jones4b9c6292011-07-01 17:40:48 -0500966 // Output the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 if ($this->dynamic_output == TRUE)
968 {
969 $this->image_display_gd($src_img);
970 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200971 elseif ( ! $this->image_save_gd($src_img))
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200973 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 }
975
976 imagedestroy($src_img);
977 imagedestroy($wm_img);
978
979 return TRUE;
980 }
981
982 // --------------------------------------------------------------------
983
984 /**
985 * Watermark - Text Version
986 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 * @return bool
988 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200989 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 {
991 if ( ! ($src_img = $this->image_create_gd()))
992 {
993 return FALSE;
994 }
995
996 if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
997 {
998 $this->set_error('imglib_missing_font');
999 return FALSE;
1000 }
1001
Derek Jones4b9c6292011-07-01 17:40:48 -05001002 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 $this->get_image_properties();
1004
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 // Reverse the vertical offset
1006 // When the image is positioned at the bottom
1007 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -05001008 // further down. We want the reverse, so we'll
1009 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 // offset flips itself automatically
1011
1012 if ($this->wm_vrt_alignment == 'B')
1013 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
1014
1015 if ($this->wm_hor_alignment == 'R')
1016 $this->wm_hor_offset = $this->wm_hor_offset * -1;
1017
1018 // Set font width and height
1019 // These are calculated differently depending on
1020 // whether we are using the true type font or not
1021 if ($this->wm_use_truetype == TRUE)
1022 {
1023 if ($this->wm_font_size == '')
Andrey Andreeva92b9032011-12-24 19:05:58 +02001024 {
1025 $this->wm_font_size = 17;
1026 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001027
Derek Jones4b9c6292011-07-01 17:40:48 -05001028 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 $fontheight = $this->wm_font_size;
1030 $this->wm_vrt_offset += $this->wm_font_size;
1031 }
1032 else
1033 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001034 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 $fontheight = imagefontheight($this->wm_font_size);
1036 }
1037
1038 // Set base X and Y axis values
1039 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1040 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1041
1042 // Set verticle alignment
1043 if ($this->wm_use_drop_shadow == FALSE)
1044 $this->wm_shadow_distance = 0;
1045
1046 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1047 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1048
1049 switch ($this->wm_vrt_alignment)
1050 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001051 case 'T':
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001053 case 'M': $y_axis += ($this->orig_height/2)+($fontheight/2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001055 case 'B': $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 break;
1057 }
1058
1059 $x_shad = $x_axis + $this->wm_shadow_distance;
1060 $y_shad = $y_axis + $this->wm_shadow_distance;
1061
1062 // Set horizontal alignment
1063 switch ($this->wm_hor_alignment)
1064 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001065 case 'L':
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001067 case 'R':
1068 if ($this->wm_use_drop_shadow)
1069 {
1070 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1071 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1072 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001074 case 'C':
1075 if ($this->wm_use_drop_shadow)
1076 {
1077 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1078 $x_axis += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1079 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 break;
1081 }
1082
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001083 if ($this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 {
Andrey Andreev8323ae62011-12-31 18:39:10 +02001085 /* Set RGB values for text and shadow
1086 *
1087 * First character is #, so we don't really need it.
1088 * Get the rest of the string and split it into 2-length
1089 * hex values:
1090 */
1091 $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2);
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001092 $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
Andrey Andreev8323ae62011-12-31 18:39:10 +02001093 $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2);
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001094 $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[2]), hexdec($drp_color[3]));
1095
1096 // Add the text to the source image
1097 if ($this->wm_use_truetype)
1098 {
1099 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1100 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1101 }
1102 else
1103 {
1104 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1105 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1106 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 }
1108
Derek Jones4b9c6292011-07-01 17:40:48 -05001109 // Output the final image
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 if ($this->dynamic_output == TRUE)
1111 {
1112 $this->image_display_gd($src_img);
1113 }
1114 else
1115 {
1116 $this->image_save_gd($src_img);
1117 }
1118
1119 imagedestroy($src_img);
1120
1121 return TRUE;
1122 }
1123
1124 // --------------------------------------------------------------------
1125
1126 /**
1127 * Create Image - GD
1128 *
1129 * This simply creates an image resource handle
1130 * based on the type of image being processed
1131 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 * @param string
1133 * @return resource
1134 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001135 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
1137 if ($path == '')
1138 $path = $this->full_src_path;
1139
1140 if ($image_type == '')
1141 $image_type = $this->image_type;
1142
1143
1144 switch ($image_type)
1145 {
1146 case 1 :
1147 if ( ! function_exists('imagecreatefromgif'))
1148 {
1149 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1150 return FALSE;
1151 }
1152
1153 return imagecreatefromgif($path);
1154 break;
1155 case 2 :
1156 if ( ! function_exists('imagecreatefromjpeg'))
1157 {
1158 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1159 return FALSE;
1160 }
1161
1162 return imagecreatefromjpeg($path);
1163 break;
1164 case 3 :
1165 if ( ! function_exists('imagecreatefrompng'))
1166 {
1167 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1168 return FALSE;
1169 }
1170
1171 return imagecreatefrompng($path);
1172 break;
1173
1174 }
1175
1176 $this->set_error(array('imglib_unsupported_imagecreate'));
1177 return FALSE;
1178 }
1179
1180 // --------------------------------------------------------------------
1181
1182 /**
1183 * Write image file to disk - GD
1184 *
1185 * Takes an image resource as input and writes the file
1186 * to the specified destination
1187 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 * @param resource
1189 * @return bool
1190 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001191 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 {
1193 switch ($this->image_type)
1194 {
1195 case 1 :
1196 if ( ! function_exists('imagegif'))
1197 {
1198 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1199 return FALSE;
1200 }
1201
Derek Jones541ddbd2008-12-09 15:25:31 +00001202 if ( ! @imagegif($resource, $this->full_dst_path))
1203 {
1204 $this->set_error('imglib_save_failed');
1205 return FALSE;
1206 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 break;
1208 case 2 :
1209 if ( ! function_exists('imagejpeg'))
1210 {
1211 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1212 return FALSE;
1213 }
1214
Derek Jones541ddbd2008-12-09 15:25:31 +00001215 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1216 {
1217 $this->set_error('imglib_save_failed');
1218 return FALSE;
1219 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 break;
1221 case 3 :
1222 if ( ! function_exists('imagepng'))
1223 {
1224 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1225 return FALSE;
1226 }
1227
Derek Jones541ddbd2008-12-09 15:25:31 +00001228 if ( ! @imagepng($resource, $this->full_dst_path))
1229 {
1230 $this->set_error('imglib_save_failed');
1231 return FALSE;
1232 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 break;
1234 default :
1235 $this->set_error(array('imglib_unsupported_imagecreate'));
1236 return FALSE;
1237 break;
1238 }
1239
1240 return TRUE;
1241 }
1242
1243 // --------------------------------------------------------------------
1244
1245 /**
1246 * Dynamically outputs an image
1247 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 * @param resource
1249 * @return void
1250 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001251 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 {
1253 header("Content-Disposition: filename={$this->source_image};");
1254 header("Content-Type: {$this->mime_type}");
1255 header('Content-Transfer-Encoding: binary');
1256 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1257
1258 switch ($this->image_type)
1259 {
Barry Mienydd671972010-10-04 16:33:58 +02001260 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 break;
1262 case 2 : imagejpeg($resource, '', $this->quality);
1263 break;
1264 case 3 : imagepng($resource);
1265 break;
1266 default : echo 'Unable to display the image';
1267 break;
1268 }
1269 }
1270
1271 // --------------------------------------------------------------------
1272
1273 /**
1274 * Re-proportion Image Width/Height
1275 *
1276 * When creating thumbs, the desired width/height
1277 * can end up warping the image due to an incorrect
1278 * ratio between the full-sized image and the thumb.
1279 *
1280 * This function lets us re-proportion the width/height
1281 * if users choose to maintain the aspect ratio when resizing.
1282 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 * @return void
1284 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001285 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 {
1287 if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
1288 return;
1289
1290 if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
1291 return;
1292
1293 $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
1294 $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
1295
1296 $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
1297
1298 if ($this->master_dim != 'width' AND $this->master_dim != 'height')
1299 {
1300 $this->master_dim = ($ratio < 0) ? 'width' : 'height';
1301 }
1302
1303 if (($this->width != $new_width) AND ($this->height != $new_height))
1304 {
1305 if ($this->master_dim == 'height')
1306 {
1307 $this->width = $new_width;
1308 }
1309 else
1310 {
1311 $this->height = $new_height;
1312 }
1313 }
1314 }
1315
1316 // --------------------------------------------------------------------
1317
1318 /**
1319 * Get image properties
1320 *
1321 * A helper function that gets info about the file
1322 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 * @param string
1324 * @return mixed
1325 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001326 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 {
1328 // For now we require GD but we should
1329 // find a way to determine this using IM or NetPBM
1330
1331 if ($path == '')
1332 $path = $this->full_src_path;
1333
1334 if ( ! file_exists($path))
1335 {
1336 $this->set_error('imglib_invalid_path');
1337 return FALSE;
1338 }
1339
Phil Sturgeon901998a2011-08-26 10:03:33 +01001340 $vals = getimagesize($path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
Andrey Andreeva92b9032011-12-24 19:05:58 +02001342 $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001343
1344 if ($return == TRUE)
1345 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001346 return array(
1347 'width' => $vals[0],
1348 'height' => $vals[1],
1349 'image_type' => $vals[2],
1350 'size_str' => $vals[3],
1351 'mime_type' => $mime
1352 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 }
1354
Andrey Andreeva92b9032011-12-24 19:05:58 +02001355 $this->orig_width = $vals[0];
1356 $this->orig_height = $vals[1];
1357 $this->image_type = $vals[2];
1358 $this->size_str = $vals[3];
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 $this->mime_type = $mime;
1360
1361 return TRUE;
1362 }
1363
1364 // --------------------------------------------------------------------
1365
1366 /**
1367 * Size calculator
1368 *
1369 * This function takes a known width x height and
Derek Jones4b9c6292011-07-01 17:40:48 -05001370 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 * new variable needs to be known
1372 *
1373 * $props = array(
Barry Mienydd671972010-10-04 16:33:58 +02001374 * 'width' => $width,
1375 * 'height' => $height,
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 * 'new_width' => 40,
1377 * 'new_height' => ''
Derek Jones4b9c6292011-07-01 17:40:48 -05001378 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 * @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 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 * @param array
1428 * @return array
1429 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001430 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001431 {
Derek Jones08cae632009-02-10 20:03:29 +00001432 $ext = strrchr($source_image, '.');
1433 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001434
Derek Jones08cae632009-02-10 20:03:29 +00001435 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 }
1437
1438 // --------------------------------------------------------------------
1439
1440 /**
1441 * Is GD Installed?
1442 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001443 * @return bool
1444 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001445 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 {
1447 if ( ! extension_loaded('gd'))
1448 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001449 /* As it is stated in the PHP manual, dl() is not always available
1450 * and even if so - it could generate an E_WARNING message on failure
1451 */
1452 return (function_exists('dl') AND @dl('gd.so'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001453 }
1454
1455 return TRUE;
1456 }
1457
1458 // --------------------------------------------------------------------
1459
1460 /**
1461 * Get GD version
1462 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001463 * @return mixed
1464 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001465 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001466 {
1467 if (function_exists('gd_info'))
1468 {
1469 $gd_version = @gd_info();
1470 $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
1471
1472 return $gd_version;
1473 }
1474
1475 return FALSE;
1476 }
1477
1478 // --------------------------------------------------------------------
1479
1480 /**
1481 * Set error message
1482 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001483 * @param string
1484 * @return void
1485 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001486 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001487 {
1488 $CI =& get_instance();
1489 $CI->lang->load('imglib');
1490
1491 if (is_array($msg))
1492 {
1493 foreach ($msg as $val)
1494 {
1495
1496 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1497 $this->error_msg[] = $msg;
1498 log_message('error', $msg);
1499 }
1500 }
1501 else
1502 {
1503 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1504 $this->error_msg[] = $msg;
1505 log_message('error', $msg);
1506 }
1507 }
1508
1509 // --------------------------------------------------------------------
1510
1511 /**
1512 * Show error messages
1513 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001514 * @param string
1515 * @return string
1516 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001517 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 {
Andrey Andreev4eea9892011-12-19 12:05:41 +02001519 return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 }
1521
1522}
1523// END Image_lib Class
1524
1525/* End of file Image_lib.php */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001526/* Location: ./system/libraries/Image_lib.php */