blob: 8430d60df105c9a324c1d071a4fb0b1584a7a416 [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
Greg Akera9263282010-11-10 15:26:43 -060091 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
93 if (count($props) > 0)
94 {
95 $this->initialize($props);
96 }
97
Andrey Andreev8e70b792012-01-12 20:19:24 +020098 log_message('debug', 'Image Lib Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000099 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * Initialize image properties
105 *
106 * Resets values in case this class is used in a loop
107 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 * @return void
109 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200110 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Michael Denniscb07a322011-08-20 23:40:59 -0700112 $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 +0000113
114 foreach ($props as $val)
115 {
116 $this->$val = '';
117 }
118
Michael Denniscb07a322011-08-20 23:40:59 -0700119 $this->image_library = 'gd2';
120 $this->dynamic_output = FALSE;
121 $this->quality = '90';
122 $this->create_thumb = FALSE;
123 $this->thumb_marker = '_thumb';
124 $this->maintain_ratio = TRUE;
125 $this->master_dim = 'auto';
126 $this->wm_type = 'text';
127 $this->wm_x_transp = 4;
128 $this->wm_y_transp = 4;
129 $this->wm_font_size = 17;
130 $this->wm_vrt_alignment = 'B';
131 $this->wm_hor_alignment = 'C';
132 $this->wm_padding = 0;
133 $this->wm_hor_offset = 0;
134 $this->wm_vrt_offset = 0;
135 $this->wm_font_color = '#ffffff';
136 $this->wm_shadow_distance = 2;
137 $this->wm_opacity = 50;
138 $this->create_fnc = 'imagecreatetruecolor';
139 $this->copy_fnc = 'imagecopyresampled';
140 $this->error_msg = array();
141 $this->wm_use_drop_shadow = FALSE;
142 $this->wm_use_truetype = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
144
145 // --------------------------------------------------------------------
146
147 /**
148 * initialize image preferences
149 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 * @param array
151 * @return bool
152 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200153 public function initialize($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200155 // Convert array elements into class variables
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 if (count($props) > 0)
157 {
158 foreach ($props as $key => $val)
159 {
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200160 if (property_exists($this, $key))
161 {
162 if (in_array($key, array('wm_font_color', 'wm_shadow_color')))
163 {
Andrey Andreev8323ae62011-12-31 18:39:10 +0200164 if (preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches))
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200165 {
Andrey Andreevcf2ba9e2012-01-01 21:57:13 +0200166 /* $matches[1] contains our hex color value, but it might be
Andrey Andreev8323ae62011-12-31 18:39:10 +0200167 * both in the full 6-length format or the shortened 3-length
168 * value.
Andrey Andreevbb96c8b2011-12-31 18:48:39 +0200169 * We'll later need the full version, so we keep it if it's
Andrey Andreev8323ae62011-12-31 18:39:10 +0200170 * already there and if not - we'll convert to it. We can
171 * access string characters by their index as in an array,
172 * so we'll do that and use concatenation to form the final
173 * value:
174 */
Andrey Andreev665af0c2011-12-30 14:39:29 +0200175 $val = (strlen($matches[1]) === 6)
176 ? '#'.$matches[1]
177 : '#'.$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 +0200178 }
179 else
180 {
181 continue;
182 }
183 }
184
185 $this->$key = $val;
186 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188 }
189
190 /*
191 * Is there a source image?
192 *
193 * If not, there's no reason to continue
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 */
195 if ($this->source_image == '')
196 {
197 $this->set_error('imglib_source_image_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500198 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200
201 /*
202 * Is getimagesize() Available?
203 *
204 * We use it to determine the image properties (width/height).
Derek Jones4b9c6292011-07-01 17:40:48 -0500205 * Note: We need to figure out how to determine image
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 * properties using ImageMagick and NetPBM
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 */
208 if ( ! function_exists('getimagesize'))
209 {
210 $this->set_error('imglib_gd_required_for_props');
211 return FALSE;
212 }
213
214 $this->image_library = strtolower($this->image_library);
215
216 /*
217 * Set the full server path
218 *
219 * The source image may or may not contain a path.
220 * Either way, we'll try use realpath to generate the
221 * full server path in order to more reliably read it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 */
Andrey Andreev8e70b792012-01-12 20:19:24 +0200223 if (function_exists('realpath') && @realpath($this->source_image) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200225 $full_source_path = str_replace('\\', '/', realpath($this->source_image));
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
227 else
228 {
229 $full_source_path = $this->source_image;
230 }
231
232 $x = explode('/', $full_source_path);
233 $this->source_image = end($x);
234 $this->source_folder = str_replace($this->source_image, '', $full_source_path);
235
236 // Set the Image Properties
237 if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
238 {
Eric Barnesb1673362011-12-05 22:05:38 -0500239 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241
242 /*
243 * Assign the "new" image name/path
244 *
245 * If the user has set a "new_image" name it means
246 * we are making a copy of the source image. If not
Derek Jones4b9c6292011-07-01 17:40:48 -0500247 * it means we are altering the original. We'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 * set the destination filename and path accordingly.
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 */
250 if ($this->new_image == '')
251 {
252 $this->dest_image = $this->source_image;
253 $this->dest_folder = $this->source_folder;
254 }
255 else
256 {
257 if (strpos($this->new_image, '/') === FALSE)
258 {
259 $this->dest_folder = $this->source_folder;
260 $this->dest_image = $this->new_image;
261 }
262 else
263 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200264 if (function_exists('realpath') && @realpath($this->new_image) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200266 $full_dest_path = str_replace('\\', '/', realpath($this->new_image));
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
268 else
269 {
270 $full_dest_path = $this->new_image;
271 }
272
273 // Is there a file name?
Andrey Andreev8e70b792012-01-12 20:19:24 +0200274 if ( ! preg_match('#\.(jpg|jpeg|gif|png)$#i', $full_dest_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 $this->dest_folder = $full_dest_path.'/';
277 $this->dest_image = $this->source_image;
278 }
279 else
280 {
281 $x = explode('/', $full_dest_path);
282 $this->dest_image = end($x);
283 $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
284 }
285 }
286 }
287
288 /*
289 * Compile the finalized filenames/paths
290 *
291 * We'll create two master strings containing the
292 * full server path to the source image and the
293 * full server path to the destination image.
294 * We'll also split the destination image name
295 * so we can insert the thumbnail marker if needed.
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 */
297 if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
298 {
299 $this->thumb_marker = '';
300 }
301
302 $xp = $this->explode_name($this->dest_image);
303
304 $filename = $xp['name'];
305 $file_ext = $xp['ext'];
306
307 $this->full_src_path = $this->source_folder.$this->source_image;
308 $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
309
310 /*
311 * Should we maintain image proportions?
312 *
313 * When creating thumbs or copies, the target width/height
314 * might not be in correct proportion with the source
Andrey Andreev8e70b792012-01-12 20:19:24 +0200315 * image's width/height. We'll recalculate it here.
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 */
Andrey Andreev8e70b792012-01-12 20:19:24 +0200317 if ($this->maintain_ratio === TRUE && ($this->width != 0 OR $this->height != 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 $this->image_reproportion();
320 }
321
322 /*
323 * Was a width and height specified?
324 *
325 * If the destination width/height was
326 * not submitted we will use the values
327 * from the actual file
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 */
329 if ($this->width == '')
Andrey Andreev8e70b792012-01-12 20:19:24 +0200330 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 $this->width = $this->orig_width;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200332 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000333
334 if ($this->height == '')
Andrey Andreev8e70b792012-01-12 20:19:24 +0200335 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 $this->height = $this->orig_height;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200337 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000338
339 // Set the quality
Andrey Andreev8e70b792012-01-12 20:19:24 +0200340 $this->quality = trim(str_replace('%', '', $this->quality));
Derek Allard2067d1a2008-11-13 22:59:24 +0000341
Andrey Andreev8e70b792012-01-12 20:19:24 +0200342 if ($this->quality == '' OR $this->quality == 0 OR ! preg_match('/^[0-9]+$/', $this->quality))
343 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 $this->quality = 90;
Andrey Andreev8e70b792012-01-12 20:19:24 +0200345 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000346
347 // Set the x/y coordinates
Andrey Andreev8e70b792012-01-12 20:19:24 +0200348 $this->x_axis = ($this->x_axis == '' OR ! preg_match('/^[0-9]+$/', $this->x_axis)) ? 0 : $this->x_axis;
349 $this->y_axis = ($this->y_axis == '' OR ! preg_match('/^[0-9]+$/', $this->y_axis)) ? 0 : $this->y_axis;
Derek Allard2067d1a2008-11-13 22:59:24 +0000350
351 // Watermark-related Stuff...
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 if ($this->wm_overlay_path != '')
353 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200354 $this->wm_overlay_path = str_replace('\\', '/', realpath($this->wm_overlay_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
356
357 if ($this->wm_shadow_color != '')
358 {
359 $this->wm_use_drop_shadow = TRUE;
360 }
Andrey Andreev8e70b792012-01-12 20:19:24 +0200361 elseif ($this->wm_use_drop_shadow == TRUE && $this->wm_shadow_color == '')
Andrey Andreev64dbdfb2011-12-30 14:14:07 +0200362 {
363 $this->wm_use_drop_shadow = FALSE;
364 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000365
366 if ($this->wm_font_path != '')
367 {
368 $this->wm_use_truetype = TRUE;
369 }
370
371 return TRUE;
372 }
373
374 // --------------------------------------------------------------------
375
376 /**
377 * Image Resize
378 *
379 * This is a wrapper function that chooses the proper
380 * resize function based on the protocol specified
381 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 * @return bool
383 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200384 public function resize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
Andrey Andreev1a9f52c2012-01-07 01:06:34 +0200386 $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 return $this->$protocol('resize');
388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * Image Crop
394 *
395 * This is a wrapper function that chooses the proper
396 * cropping function based on the protocol specified
397 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 * @return bool
399 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200400 public function crop()
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
Andrey Andreev5a67e3d2012-01-07 01:21:09 +0200402 $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 return $this->$protocol('crop');
404 }
405
406 // --------------------------------------------------------------------
407
408 /**
409 * Image Rotate
410 *
411 * This is a wrapper function that chooses the proper
412 * rotation function based on the protocol specified
413 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @return bool
415 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200416 public function rotate()
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
418 // Allowed rotation values
419 $degs = array(90, 180, 270, 'vrt', 'hor');
420
Derek Allardd9c7f032008-12-01 20:18:00 +0000421 if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs))
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 $this->set_error('imglib_rotation_angle_required');
Eric Barnesb1673362011-12-05 22:05:38 -0500424 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
426
427 // Reassign the width and height
428 if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
429 {
430 $this->width = $this->orig_height;
431 $this->height = $this->orig_width;
432 }
433 else
434 {
435 $this->width = $this->orig_width;
436 $this->height = $this->orig_height;
437 }
438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 // Choose resizing function
440 if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm')
441 {
442 $protocol = 'image_process_'.$this->image_library;
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 return $this->$protocol('rotate');
444 }
445
446 if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
447 {
448 return $this->image_mirror_gd();
449 }
450 else
451 {
452 return $this->image_rotate_gd();
453 }
454 }
455
456 // --------------------------------------------------------------------
457
458 /**
459 * Image Process Using GD/GD2
460 *
461 * This function will resize or crop
462 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 * @param string
464 * @return bool
465 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200466 public function image_process_gd($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
468 $v2_override = FALSE;
469
470 // If the target width/height match the source, AND if the new file name is not equal to the old file name
471 // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
Andrey Andreev8e70b792012-01-12 20:19:24 +0200472 if ($this->dynamic_output === FALSE && $this->orig_width == $this->width && $this->orig_height == $this->height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200474 if ($this->source_image != $this->new_image && @copy($this->full_src_path, $this->full_dst_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200476 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200478
479 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 }
481
482 // Let's set up our values based on the action
483 if ($action == 'crop')
484 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500485 // Reassign the source width/height if cropping
486 $this->orig_width = $this->width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 $this->orig_height = $this->height;
488
489 // GD 2.0 has a cropping bug so we'll test for it
490 if ($this->gd_version() !== FALSE)
491 {
492 $gd_version = str_replace('0', '', $this->gd_version());
493 $v2_override = ($gd_version == 2) ? TRUE : FALSE;
494 }
495 }
496 else
497 {
498 // If resizing the x/y axis must be zero
499 $this->x_axis = 0;
500 $this->y_axis = 0;
501 }
502
Derek Jones4b9c6292011-07-01 17:40:48 -0500503 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 if ( ! ($src_img = $this->image_create_gd()))
505 {
506 return FALSE;
507 }
508
Derek Jones4b9c6292011-07-01 17:40:48 -0500509 // Create The Image
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500511 // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
512 // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
513 // below should that ever prove inaccurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 //
Derek Jones4b9c6292011-07-01 17:40:48 -0500515 // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
Andrey Andreev8e70b792012-01-12 20:19:24 +0200516 if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 $create = 'imagecreatetruecolor';
519 $copy = 'imagecopyresampled';
520 }
521 else
522 {
523 $create = 'imagecreate';
524 $copy = 'imagecopyresized';
525 }
526
527 $dst_img = $create($this->width, $this->height);
Derek Jones595bfd12010-08-20 10:28:22 -0500528
529 if ($this->image_type == 3) // png we can actually preserve transparency
530 {
531 imagealphablending($dst_img, FALSE);
532 imagesavealpha($dst_img, TRUE);
533 }
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
536
Derek Jones4b9c6292011-07-01 17:40:48 -0500537 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 if ($this->dynamic_output == TRUE)
539 {
540 $this->image_display_gd($dst_img);
541 }
542 else
543 {
544 // Or save it
545 if ( ! $this->image_save_gd($dst_img))
546 {
547 return FALSE;
548 }
549 }
550
Derek Jones4b9c6292011-07-01 17:40:48 -0500551 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 imagedestroy($dst_img);
553 imagedestroy($src_img);
554
555 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000556 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000557
558 return TRUE;
559 }
560
561 // --------------------------------------------------------------------
562
563 /**
564 * Image Process Using ImageMagick
565 *
566 * This function will resize, crop or rotate
567 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 * @param string
569 * @return bool
570 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200571 public function image_process_imagemagick($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500573 // Do we have a vaild library path?
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 if ($this->library_path == '')
575 {
576 $this->set_error('imglib_libpath_invalid');
577 return FALSE;
578 }
579
Andrey Andreev8e70b792012-01-12 20:19:24 +0200580 if ( ! preg_match('/convert$/i', $this->library_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200582 $this->library_path = rtrim($this->library_path, '/').'/convert';
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 }
584
585 // Execute the command
586 $cmd = $this->library_path." -quality ".$this->quality;
587
588 if ($action == 'crop')
589 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200590 $cmd .= ' -crop '.$this->width.'x'.$this->height.'+'.$this->x_axis.'+'.$this->y_axis.' "'.$this->full_src_path.'" "'.$this->full_dst_path .'" 2>&1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 }
592 elseif ($action == 'rotate')
593 {
594 switch ($this->rotation_angle)
595 {
Barry Mienydd671972010-10-04 16:33:58 +0200596 case 'hor' : $angle = '-flop';
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 break;
Barry Mienydd671972010-10-04 16:33:58 +0200598 case 'vrt' : $angle = '-flip';
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 break;
600 default : $angle = '-rotate '.$this->rotation_angle;
601 break;
602 }
603
Andrey Andreev8e70b792012-01-12 20:19:24 +0200604 $cmd .= ' '.$angle.' "'.$this->full_src_path.'" "'.$this->full_dst_path.'" 2>&1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 }
Andrey Andreev8e70b792012-01-12 20:19:24 +0200606 else // Resize
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200608 $cmd .= ' -resize '.$this->width.'x'.$this->height.'" "'.$this->full_src_path.'" "'.$this->full_dst_path.'" 2>&1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 }
610
611 $retval = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 @exec($cmd, $output, $retval);
613
Andrey Andreev8e70b792012-01-12 20:19:24 +0200614 // Did it work?
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 if ($retval > 0)
616 {
617 $this->set_error('imglib_image_process_failed');
618 return FALSE;
619 }
620
621 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000622 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000623
624 return TRUE;
625 }
626
627 // --------------------------------------------------------------------
628
629 /**
630 * Image Process Using NetPBM
631 *
632 * This function will resize, crop or rotate
633 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 * @param string
635 * @return bool
636 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200637 public function image_process_netpbm($action = 'resize')
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
639 if ($this->library_path == '')
640 {
641 $this->set_error('imglib_libpath_invalid');
642 return FALSE;
643 }
644
Derek Jones4b9c6292011-07-01 17:40:48 -0500645 // Build the resizing command
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 switch ($this->image_type)
647 {
648 case 1 :
649 $cmd_in = 'giftopnm';
650 $cmd_out = 'ppmtogif';
651 break;
652 case 2 :
653 $cmd_in = 'jpegtopnm';
654 $cmd_out = 'ppmtojpeg';
655 break;
656 case 3 :
657 $cmd_in = 'pngtopnm';
658 $cmd_out = 'ppmtopng';
659 break;
660 }
661
662 if ($action == 'crop')
663 {
664 $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
665 }
666 elseif ($action == 'rotate')
667 {
668 switch ($this->rotation_angle)
669 {
670 case 90 : $angle = 'r270';
671 break;
672 case 180 : $angle = 'r180';
673 break;
Barry Mienydd671972010-10-04 16:33:58 +0200674 case 270 : $angle = 'r90';
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 break;
676 case 'vrt' : $angle = 'tb';
677 break;
678 case 'hor' : $angle = 'lr';
679 break;
680 }
681
682 $cmd_inner = 'pnmflip -'.$angle.' ';
683 }
684 else // Resize
685 {
686 $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
687 }
688
689 $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
690
691 $retval = 1;
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 @exec($cmd, $output, $retval);
693
Andrey Andreev8e70b792012-01-12 20:19:24 +0200694 // 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 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 * @return bool
717 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200718 public function image_rotate_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500720 // Create the image handle
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 if ( ! ($src_img = $this->image_create_gd()))
722 {
723 return FALSE;
724 }
725
726 // Set the background color
727 // This won't work with transparent PNG files so we are
728 // going to have to figure out how to determine the color
729 // of the alpha channel in a future release.
730
731 $white = imagecolorallocate($src_img, 255, 255, 255);
732
Andrey Andreev8e70b792012-01-12 20:19:24 +0200733 // Rotate it!
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
735
Andrey Andreev8e70b792012-01-12 20:19:24 +0200736 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 if ($this->dynamic_output == TRUE)
738 {
739 $this->image_display_gd($dst_img);
740 }
Andrey Andreev8e70b792012-01-12 20:19:24 +0200741 elseif ( ! $this->image_save_gd($dst_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200743 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 }
745
Andrey Andreev8e70b792012-01-12 20:19:24 +0200746 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 imagedestroy($dst_img);
748 imagedestroy($src_img);
749
750 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000751 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000752
Pascal Kriete8761ef52011-02-14 13:13:52 -0500753 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 }
755
756 // --------------------------------------------------------------------
757
758 /**
759 * Create Mirror Image using GD
760 *
761 * This function will flip horizontal or vertical
762 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 * @return bool
764 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200765 public function image_mirror_gd()
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 {
767 if ( ! $src_img = $this->image_create_gd())
768 {
769 return FALSE;
770 }
771
Derek Jones4b9c6292011-07-01 17:40:48 -0500772 $width = $this->orig_width;
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 $height = $this->orig_height;
774
775 if ($this->rotation_angle == 'hor')
776 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200777 for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 while ($left < $right)
780 {
781 $cl = imagecolorat($src_img, $left, $i);
782 $cr = imagecolorat($src_img, $right, $i);
783
784 imagesetpixel($src_img, $left, $i, $cr);
785 imagesetpixel($src_img, $right, $i, $cl);
786
787 $left++;
788 $right--;
789 }
790 }
791 }
792 else
793 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200794 for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 while ($top < $bot)
797 {
798 $ct = imagecolorat($src_img, $i, $top);
799 $cb = imagecolorat($src_img, $i, $bot);
800
801 imagesetpixel($src_img, $i, $top, $cb);
802 imagesetpixel($src_img, $i, $bot, $ct);
803
804 $top++;
805 $bot--;
806 }
807 }
808 }
809
Andrey Andreev8e70b792012-01-12 20:19:24 +0200810 // Show the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 if ($this->dynamic_output == TRUE)
812 {
813 $this->image_display_gd($src_img);
814 }
Andrey Andreev8e70b792012-01-12 20:19:24 +0200815 elseif ( ! $this->image_save_gd($src_img)) // ... or save it
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200817 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 }
819
Andrey Andreev8e70b792012-01-12 20:19:24 +0200820 // Kill the file handles
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 imagedestroy($src_img);
822
823 // Set the file to 777
Derek Jones172e1612009-10-13 14:32:48 +0000824 @chmod($this->full_dst_path, FILE_WRITE_MODE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000825
826 return TRUE;
827 }
828
829 // --------------------------------------------------------------------
830
831 /**
832 * Image Watermark
833 *
834 * This is a wrapper function that chooses the type
835 * of watermarking based on the specified preference.
836 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 * @param string
838 * @return bool
839 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200840 public function watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 {
Andrey Andreev8e70b792012-01-12 20:19:24 +0200842 return ($this->wm_type == 'overlay') ? $this->overlay_watermark() : $this->text_watermark();
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 }
844
845 // --------------------------------------------------------------------
846
847 /**
848 * Watermark - Graphic Version
849 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 * @return bool
851 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200852 public function overlay_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
854 if ( ! function_exists('imagecolortransparent'))
855 {
856 $this->set_error('imglib_gd_required');
857 return FALSE;
858 }
859
Andrey Andreev8e70b792012-01-12 20:19:24 +0200860 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 $this->get_image_properties();
862
Andrey Andreev8e70b792012-01-12 20:19:24 +0200863 // Fetch watermark image properties
Barry Mienydd671972010-10-04 16:33:58 +0200864 $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 $wm_img_type = $props['image_type'];
866 $wm_width = $props['width'];
867 $wm_height = $props['height'];
868
Andrey Andreev8e70b792012-01-12 20:19:24 +0200869 // Create two image resources
Derek Jones4b9c6292011-07-01 17:40:48 -0500870 $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 $src_img = $this->image_create_gd($this->full_src_path);
872
873 // Reverse the offset if necessary
874 // When the image is positioned at the bottom
875 // we don't want the vertical offset to push it
Andrey Andreev8e70b792012-01-12 20:19:24 +0200876 // further down. We want the reverse, so we'll
877 // invert the offset. Same with the horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 // offset when the image is at the right
879
Andrey Andreev8e70b792012-01-12 20:19:24 +0200880 $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
881 $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000882
883 if ($this->wm_vrt_alignment == 'B')
884 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
885
886 if ($this->wm_hor_alignment == 'R')
887 $this->wm_hor_offset = $this->wm_hor_offset * -1;
888
Andrey Andreev8e70b792012-01-12 20:19:24 +0200889 // Set the base x and y axis values
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 $x_axis = $this->wm_hor_offset + $this->wm_padding;
891 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
892
Andrey Andreev8e70b792012-01-12 20:19:24 +0200893 // Set the vertical position
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 switch ($this->wm_vrt_alignment)
895 {
896 case 'T':
897 break;
898 case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
899 break;
900 case 'B': $y_axis += $this->orig_height - $wm_height;
901 break;
902 }
903
Andrey Andreev8e70b792012-01-12 20:19:24 +0200904 // Set the horizontal position
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 switch ($this->wm_hor_alignment)
906 {
907 case 'L':
908 break;
909 case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
910 break;
911 case 'R': $x_axis += $this->orig_width - $wm_width;
912 break;
913 }
914
Derek Jones4b9c6292011-07-01 17:40:48 -0500915 // Build the finalized image
Andrey Andreev8e70b792012-01-12 20:19:24 +0200916 if ($wm_img_type == 3 && function_exists('imagealphablending'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 {
918 @imagealphablending($src_img, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200919 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000920
921 // Set RGB values for text and shadow
922 $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
923 $alpha = ($rgba & 0x7F000000) >> 24;
924
925 // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
926 if ($alpha > 0)
927 {
928 // copy the image directly, the image's alpha transparency being the sole determinant of blending
929 imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
930 }
931 else
932 {
933 // set our RGB value from above to be transparent and merge the images with the specified opacity
934 imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
935 imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
936 }
937
Derek Jones4b9c6292011-07-01 17:40:48 -0500938 // Output the image
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 if ($this->dynamic_output == TRUE)
940 {
941 $this->image_display_gd($src_img);
942 }
Andrey Andreeva92b9032011-12-24 19:05:58 +0200943 elseif ( ! $this->image_save_gd($src_img))
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 {
Andrey Andreeva92b9032011-12-24 19:05:58 +0200945 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
947
948 imagedestroy($src_img);
949 imagedestroy($wm_img);
950
951 return TRUE;
952 }
953
954 // --------------------------------------------------------------------
955
956 /**
957 * Watermark - Text Version
958 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 * @return bool
960 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200961 public function text_watermark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 {
963 if ( ! ($src_img = $this->image_create_gd()))
964 {
965 return FALSE;
966 }
967
Andrey Andreev8e70b792012-01-12 20:19:24 +0200968 if ($this->wm_use_truetype == TRUE && ! file_exists($this->wm_font_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
970 $this->set_error('imglib_missing_font');
971 return FALSE;
972 }
973
Andrey Andreev8e70b792012-01-12 20:19:24 +0200974 // Fetch source image properties
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 $this->get_image_properties();
976
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 // Reverse the vertical offset
978 // When the image is positioned at the bottom
979 // we don't want the vertical offset to push it
Derek Jones4b9c6292011-07-01 17:40:48 -0500980 // further down. We want the reverse, so we'll
Andrey Andreev8e70b792012-01-12 20:19:24 +0200981 // invert the offset. Note: The horizontal
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 // offset flips itself automatically
983
984 if ($this->wm_vrt_alignment == 'B')
985 $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
986
987 if ($this->wm_hor_alignment == 'R')
988 $this->wm_hor_offset = $this->wm_hor_offset * -1;
989
990 // Set font width and height
991 // These are calculated differently depending on
992 // whether we are using the true type font or not
993 if ($this->wm_use_truetype == TRUE)
994 {
995 if ($this->wm_font_size == '')
Andrey Andreeva92b9032011-12-24 19:05:58 +0200996 {
997 $this->wm_font_size = 17;
998 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000999
Derek Jones4b9c6292011-07-01 17:40:48 -05001000 $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 $fontheight = $this->wm_font_size;
1002 $this->wm_vrt_offset += $this->wm_font_size;
1003 }
1004 else
1005 {
Derek Jones4b9c6292011-07-01 17:40:48 -05001006 $fontwidth = imagefontwidth($this->wm_font_size);
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 $fontheight = imagefontheight($this->wm_font_size);
1008 }
1009
1010 // Set base X and Y axis values
1011 $x_axis = $this->wm_hor_offset + $this->wm_padding;
1012 $y_axis = $this->wm_vrt_offset + $this->wm_padding;
1013
1014 // Set verticle alignment
1015 if ($this->wm_use_drop_shadow == FALSE)
1016 $this->wm_shadow_distance = 0;
1017
1018 $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
1019 $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
1020
1021 switch ($this->wm_vrt_alignment)
1022 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001023 case 'T':
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001025 case 'M': $y_axis += ($this->orig_height/2)+($fontheight/2);
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001027 case 'B': $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 break;
1029 }
1030
1031 $x_shad = $x_axis + $this->wm_shadow_distance;
1032 $y_shad = $y_axis + $this->wm_shadow_distance;
1033
1034 // Set horizontal alignment
1035 switch ($this->wm_hor_alignment)
1036 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001037 case 'L':
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001039 case 'R':
1040 if ($this->wm_use_drop_shadow)
1041 {
1042 $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1043 $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
1044 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 break;
Andrey Andreeva92b9032011-12-24 19:05:58 +02001046 case 'C':
1047 if ($this->wm_use_drop_shadow)
1048 {
1049 $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1050 $x_axis += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
1051 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 break;
1053 }
1054
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001055 if ($this->wm_use_drop_shadow)
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 {
Andrey Andreev8323ae62011-12-31 18:39:10 +02001057 /* Set RGB values for text and shadow
1058 *
1059 * First character is #, so we don't really need it.
1060 * Get the rest of the string and split it into 2-length
1061 * hex values:
1062 */
1063 $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2);
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001064 $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
Andrey Andreev8323ae62011-12-31 18:39:10 +02001065 $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2);
Ronald Beilsma8f80bc42012-01-12 16:41:49 +01001066 $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[1]), hexdec($drp_color[2]));
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001067
Andrey Andreev8e70b792012-01-12 20:19:24 +02001068 // Add the text to the source image
Andrey Andreev64dbdfb2011-12-30 14:14:07 +02001069 if ($this->wm_use_truetype)
1070 {
1071 imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
1072 imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
1073 }
1074 else
1075 {
1076 imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
1077 imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
1078 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 }
1080
Andrey Andreev8e70b792012-01-12 20:19:24 +02001081 // Output the final image
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 if ($this->dynamic_output == TRUE)
1083 {
1084 $this->image_display_gd($src_img);
1085 }
1086 else
1087 {
1088 $this->image_save_gd($src_img);
1089 }
1090
1091 imagedestroy($src_img);
1092
1093 return TRUE;
1094 }
1095
1096 // --------------------------------------------------------------------
1097
1098 /**
1099 * Create Image - GD
1100 *
1101 * This simply creates an image resource handle
1102 * based on the type of image being processed
1103 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 * @param string
1105 * @return resource
1106 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001107 public function image_create_gd($path = '', $image_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 {
1109 if ($path == '')
1110 $path = $this->full_src_path;
1111
1112 if ($image_type == '')
1113 $image_type = $this->image_type;
1114
1115
1116 switch ($image_type)
1117 {
1118 case 1 :
1119 if ( ! function_exists('imagecreatefromgif'))
1120 {
1121 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1122 return FALSE;
1123 }
1124
1125 return imagecreatefromgif($path);
1126 break;
1127 case 2 :
1128 if ( ! function_exists('imagecreatefromjpeg'))
1129 {
1130 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1131 return FALSE;
1132 }
1133
1134 return imagecreatefromjpeg($path);
1135 break;
1136 case 3 :
1137 if ( ! function_exists('imagecreatefrompng'))
1138 {
1139 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1140 return FALSE;
1141 }
1142
1143 return imagecreatefrompng($path);
1144 break;
1145
1146 }
1147
1148 $this->set_error(array('imglib_unsupported_imagecreate'));
1149 return FALSE;
1150 }
1151
1152 // --------------------------------------------------------------------
1153
1154 /**
1155 * Write image file to disk - GD
1156 *
1157 * Takes an image resource as input and writes the file
1158 * to the specified destination
1159 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 * @param resource
1161 * @return bool
1162 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001163 public function image_save_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 {
1165 switch ($this->image_type)
1166 {
1167 case 1 :
1168 if ( ! function_exists('imagegif'))
1169 {
1170 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
1171 return FALSE;
1172 }
1173
Derek Jones541ddbd2008-12-09 15:25:31 +00001174 if ( ! @imagegif($resource, $this->full_dst_path))
1175 {
1176 $this->set_error('imglib_save_failed');
1177 return FALSE;
1178 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 break;
1180 case 2 :
1181 if ( ! function_exists('imagejpeg'))
1182 {
1183 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
1184 return FALSE;
1185 }
1186
Derek Jones541ddbd2008-12-09 15:25:31 +00001187 if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
1188 {
1189 $this->set_error('imglib_save_failed');
1190 return FALSE;
1191 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 break;
1193 case 3 :
1194 if ( ! function_exists('imagepng'))
1195 {
1196 $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
1197 return FALSE;
1198 }
1199
Derek Jones541ddbd2008-12-09 15:25:31 +00001200 if ( ! @imagepng($resource, $this->full_dst_path))
1201 {
1202 $this->set_error('imglib_save_failed');
1203 return FALSE;
1204 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 break;
1206 default :
1207 $this->set_error(array('imglib_unsupported_imagecreate'));
1208 return FALSE;
1209 break;
1210 }
1211
1212 return TRUE;
1213 }
1214
1215 // --------------------------------------------------------------------
1216
1217 /**
1218 * Dynamically outputs an image
1219 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 * @param resource
1221 * @return void
1222 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001223 public function image_display_gd($resource)
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 {
1225 header("Content-Disposition: filename={$this->source_image};");
1226 header("Content-Type: {$this->mime_type}");
1227 header('Content-Transfer-Encoding: binary');
1228 header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
1229
1230 switch ($this->image_type)
1231 {
Barry Mienydd671972010-10-04 16:33:58 +02001232 case 1 : imagegif($resource);
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 break;
1234 case 2 : imagejpeg($resource, '', $this->quality);
1235 break;
1236 case 3 : imagepng($resource);
1237 break;
1238 default : echo 'Unable to display the image';
1239 break;
1240 }
1241 }
1242
1243 // --------------------------------------------------------------------
1244
1245 /**
1246 * Re-proportion Image Width/Height
1247 *
1248 * When creating thumbs, the desired width/height
1249 * can end up warping the image due to an incorrect
1250 * ratio between the full-sized image and the thumb.
1251 *
1252 * This function lets us re-proportion the width/height
1253 * if users choose to maintain the aspect ratio when resizing.
1254 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 * @return void
1256 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001257 public function image_reproportion()
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001259 if (($this->width == 0 && $this->height == 0) OR $this->orig_width == 0 OR $this->orig_height == 0
1260 OR ( ! preg_match('/^[0-9]+$/', $this->width) && ! preg_match('/^[0-9]+$/', $this->height))
1261 OR ! preg_match('/^[0-9]+$/', $this->orig_width) OR ! preg_match('/^[0-9]+$/', $this->orig_height))
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001263 return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 }
1265
Andrey Andreev8e70b792012-01-12 20:19:24 +02001266 // Sanitize so we don't call preg_match() anymore
1267 $this->width = (int) $this->width;
1268 $this->height = (int) $this->height;
1269
1270 if ($this->master_dim !== 'width' && $this->master_dim !== 'height')
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001272 if ($this->width > 0 && $this->height > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001274 $this->master_dim = ((($this->orig_height/$this->orig_width) - ($this->height/$this->width)) < 0)
1275 ? 'width' : 'height';
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 }
1277 else
1278 {
Andrey Andreev8e70b792012-01-12 20:19:24 +02001279 $this->master_dim = ($this->height === 0) ? 'width' : 'height';
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 }
1281 }
Andrey Andreev8e70b792012-01-12 20:19:24 +02001282 elseif (($this->master_dim === 'width' && $this->width === 0)
1283 OR ($this->master_dim === 'height' && $this->height === 0))
1284 {
1285 return;
1286 }
1287
1288 if ($this->master_dim === 'width')
1289 {
1290 $this->height = (int) ceil($this->width*$this->orig_height/$this->orig_width);
1291 }
1292 else
1293 {
1294 $this->width = (int) ceil($this->orig_width*$this->height/$this->orig_height);
1295 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 }
1297
1298 // --------------------------------------------------------------------
1299
1300 /**
1301 * Get image properties
1302 *
1303 * A helper function that gets info about the file
1304 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 * @param string
1306 * @return mixed
1307 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001308 public function get_image_properties($path = '', $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 {
1310 // For now we require GD but we should
1311 // find a way to determine this using IM or NetPBM
1312
1313 if ($path == '')
Andrey Andreev8e70b792012-01-12 20:19:24 +02001314 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 $path = $this->full_src_path;
Andrey Andreev8e70b792012-01-12 20:19:24 +02001316 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001317
1318 if ( ! file_exists($path))
1319 {
1320 $this->set_error('imglib_invalid_path');
1321 return FALSE;
1322 }
1323
Phil Sturgeon901998a2011-08-26 10:03:33 +01001324 $vals = getimagesize($path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
Andrey Andreeva92b9032011-12-24 19:05:58 +02001326 $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001327
1328 if ($return == TRUE)
1329 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001330 return array(
1331 'width' => $vals[0],
1332 'height' => $vals[1],
1333 'image_type' => $vals[2],
1334 'size_str' => $vals[3],
1335 'mime_type' => $mime
1336 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 }
1338
Andrey Andreeva92b9032011-12-24 19:05:58 +02001339 $this->orig_width = $vals[0];
1340 $this->orig_height = $vals[1];
1341 $this->image_type = $vals[2];
1342 $this->size_str = $vals[3];
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 $this->mime_type = $mime;
1344
1345 return TRUE;
1346 }
1347
1348 // --------------------------------------------------------------------
1349
1350 /**
1351 * Size calculator
1352 *
1353 * This function takes a known width x height and
Derek Jones4b9c6292011-07-01 17:40:48 -05001354 * recalculates it to a new size. Only one
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 * new variable needs to be known
1356 *
1357 * $props = array(
Barry Mienydd671972010-10-04 16:33:58 +02001358 * 'width' => $width,
1359 * 'height' => $height,
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 * 'new_width' => 40,
1361 * 'new_height' => ''
Derek Jones4b9c6292011-07-01 17:40:48 -05001362 * );
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 * @param array
1365 * @return array
1366 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001367 public function size_calculator($vals)
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 {
1369 if ( ! is_array($vals))
1370 {
1371 return;
1372 }
1373
1374 $allowed = array('new_width', 'new_height', 'width', 'height');
1375
1376 foreach ($allowed as $item)
1377 {
1378 if ( ! isset($vals[$item]) OR $vals[$item] == '')
1379 $vals[$item] = 0;
1380 }
1381
1382 if ($vals['width'] == 0 OR $vals['height'] == 0)
1383 {
1384 return $vals;
1385 }
1386
1387 if ($vals['new_width'] == 0)
1388 {
1389 $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
1390 }
1391 elseif ($vals['new_height'] == 0)
1392 {
1393 $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
1394 }
1395
1396 return $vals;
1397 }
1398
1399 // --------------------------------------------------------------------
1400
1401 /**
1402 * Explode source_image
1403 *
1404 * This is a helper function that extracts the extension
Derek Jones4b9c6292011-07-01 17:40:48 -05001405 * from the source_image. This function lets us deal with
1406 * source_images with multiple periods, like: my.cool.jpg
Derek Allard2067d1a2008-11-13 22:59:24 +00001407 * It returns an associative array with two elements:
Derek Jones4b9c6292011-07-01 17:40:48 -05001408 * $array['ext'] = '.jpg';
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 * $array['name'] = 'my.cool';
1410 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 * @param array
1412 * @return array
1413 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001414 public function explode_name($source_image)
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 {
Derek Jones08cae632009-02-10 20:03:29 +00001416 $ext = strrchr($source_image, '.');
1417 $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
Barry Mienydd671972010-10-04 16:33:58 +02001418
Derek Jones08cae632009-02-10 20:03:29 +00001419 return array('ext' => $ext, 'name' => $name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 }
1421
1422 // --------------------------------------------------------------------
1423
1424 /**
1425 * Is GD Installed?
1426 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 * @return bool
1428 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001429 public function gd_loaded()
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 {
1431 if ( ! extension_loaded('gd'))
1432 {
Andrey Andreeva92b9032011-12-24 19:05:58 +02001433 /* As it is stated in the PHP manual, dl() is not always available
1434 * and even if so - it could generate an E_WARNING message on failure
1435 */
Andrey Andreev8e70b792012-01-12 20:19:24 +02001436 return (function_exists('dl') && @dl('gd.so'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001437 }
1438
1439 return TRUE;
1440 }
1441
1442 // --------------------------------------------------------------------
1443
1444 /**
1445 * Get GD version
1446 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 * @return mixed
1448 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001449 public function gd_version()
Derek Allard2067d1a2008-11-13 22:59:24 +00001450 {
1451 if (function_exists('gd_info'))
1452 {
1453 $gd_version = @gd_info();
Andrey Andreev8e70b792012-01-12 20:19:24 +02001454 return preg_replace('/\D/', '', $gd_version['GD Version']);
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 }
1456
1457 return FALSE;
1458 }
1459
1460 // --------------------------------------------------------------------
1461
1462 /**
1463 * Set error message
1464 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 * @param string
1466 * @return void
1467 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001468 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 {
1470 $CI =& get_instance();
1471 $CI->lang->load('imglib');
1472
1473 if (is_array($msg))
1474 {
1475 foreach ($msg as $val)
1476 {
1477
1478 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
1479 $this->error_msg[] = $msg;
1480 log_message('error', $msg);
1481 }
1482 }
1483 else
1484 {
1485 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
1486 $this->error_msg[] = $msg;
1487 log_message('error', $msg);
1488 }
1489 }
1490
1491 // --------------------------------------------------------------------
1492
1493 /**
1494 * Show error messages
1495 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 * @param string
1497 * @return string
1498 */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001499 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001500 {
Andrey Andreev4eea9892011-12-19 12:05:41 +02001501 return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 }
1503
1504}
Derek Allard2067d1a2008-11-13 22:59:24 +00001505
1506/* End of file Image_lib.php */
Andrey Andreev4eea9892011-12-19 12:05:41 +02001507/* Location: ./system/libraries/Image_lib.php */