Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Eric Barnes | b167336 | 2011-12-05 22:05:38 -0500 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Eric Barnes | b167336 | 2011-12-05 22:05:38 -0500 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/libraries/image_lib.html |
| 38 | */ |
| 39 | class CI_Image_lib { |
| 40 | |
Andrey Andreev | 3a45957 | 2011-12-21 11:23:11 +0200 | [diff] [blame] | 41 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | |
| 57 | // Watermark Vars |
Andrey Andreev | 3a45957 | 2011-12-21 11:23:11 +0200 | [diff] [blame] | 58 | 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 Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 70 | protected $wm_font_color = '#ffffff'; // Text color |
| 71 | protected $wm_shadow_color = ''; // Dropshadow color |
Andrey Andreev | 3a45957 | 2011-12-21 11:23:11 +0200 | [diff] [blame] | 72 | public $wm_shadow_distance = 2; // Dropshadow distance |
| 73 | public $wm_opacity = 50; // Image opacity: 1 - 100 Only works with image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | |
| 75 | // Private Vars |
Andrey Andreev | 3a45957 | 2011-12-21 11:23:11 +0200 | [diff] [blame] | 76 | 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 Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 88 | protected $wm_use_drop_shadow = FALSE; |
Andrey Andreev | 3a45957 | 2011-12-21 11:23:11 +0200 | [diff] [blame] | 89 | public $wm_use_truetype = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Constructor |
| 93 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | * @param string |
| 95 | * @return void |
| 96 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 97 | public function __construct($props = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | * @return void |
| 115 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 116 | public function clear() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | { |
Michael Dennis | cb07a32 | 2011-08-20 23:40:59 -0700 | [diff] [blame] | 118 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | |
| 120 | foreach ($props as $val) |
| 121 | { |
| 122 | $this->$val = ''; |
| 123 | } |
| 124 | |
Michael Dennis | cb07a32 | 2011-08-20 23:40:59 -0700 | [diff] [blame] | 125 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // -------------------------------------------------------------------- |
| 152 | |
| 153 | /** |
| 154 | * initialize image preferences |
| 155 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | * @param array |
| 157 | * @return bool |
| 158 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 159 | public function initialize($props = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | { |
| 161 | /* |
| 162 | * Convert array elements into class variables |
| 163 | */ |
| 164 | if (count($props) > 0) |
| 165 | { |
| 166 | foreach ($props as $key => $val) |
| 167 | { |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 168 | if (property_exists($this, $key)) |
| 169 | { |
| 170 | if (in_array($key, array('wm_font_color', 'wm_shadow_color'))) |
| 171 | { |
Andrey Andreev | 8323ae6 | 2011-12-31 18:39:10 +0200 | [diff] [blame] | 172 | if (preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches)) |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 173 | { |
Andrey Andreev | cf2ba9e | 2012-01-01 21:57:13 +0200 | [diff] [blame^] | 174 | /* $matches[1] contains our hex color value, but it might be |
Andrey Andreev | 8323ae6 | 2011-12-31 18:39:10 +0200 | [diff] [blame] | 175 | * both in the full 6-length format or the shortened 3-length |
| 176 | * value. |
Andrey Andreev | bb96c8b | 2011-12-31 18:48:39 +0200 | [diff] [blame] | 177 | * We'll later need the full version, so we keep it if it's |
Andrey Andreev | 8323ae6 | 2011-12-31 18:39:10 +0200 | [diff] [blame] | 178 | * 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 Andreev | 665af0c | 2011-12-30 14:39:29 +0200 | [diff] [blame] | 183 | $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 Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 186 | } |
| 187 | else |
| 188 | { |
| 189 | continue; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | $this->$key = $val; |
| 194 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | } |
| 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 Barnes | b167336 | 2011-12-05 22:05:38 -0500 | [diff] [blame] | 207 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Is getimagesize() Available? |
| 212 | * |
| 213 | * We use it to determine the image properties (width/height). |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 214 | * Note: We need to figure out how to determine image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | * 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 Barnes | b167336 | 2011-12-05 22:05:38 -0500 | [diff] [blame] | 250 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | } |
| 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 258 | * it means we are altering the original. We'll |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | * 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 328 | * image's width/height. We'll recalculate it here. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | * |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | 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 Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 370 | elseif ($this->wm_use_drop_shadow == TRUE AND $this->wm_shadow_color == '') |
| 371 | { |
| 372 | $this->wm_use_drop_shadow = FALSE; |
| 373 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | * @return bool |
| 392 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 393 | public function resize() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 395 | $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | * @return bool |
| 408 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 409 | public function crop() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 411 | $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | * @return bool |
| 424 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 425 | public function rotate() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | { |
| 427 | // Allowed rotation values |
| 428 | $degs = array(90, 180, 270, 'vrt', 'hor'); |
| 429 | |
Derek Allard | d9c7f03 | 2008-12-01 20:18:00 +0000 | [diff] [blame] | 430 | if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | { |
| 432 | $this->set_error('imglib_rotation_angle_required'); |
Eric Barnes | b167336 | 2011-12-05 22:05:38 -0500 | [diff] [blame] | 433 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | } |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 473 | * @param string |
| 474 | * @return bool |
| 475 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 476 | public function image_process_gd($action = 'resize') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 477 | { |
| 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 Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 482 | if ($this->dynamic_output === FALSE AND $this->orig_width == $this->width AND $this->orig_height == $this->height) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 484 | if ($this->source_image != $this->new_image AND @copy($this->full_src_path, $this->full_dst_path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 486 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 487 | } |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 488 | |
| 489 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | // Let's set up our values based on the action |
| 493 | if ($action == 'crop') |
| 494 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 495 | // Reassign the source width/height if cropping |
| 496 | $this->orig_width = $this->width; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 497 | $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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 513 | // Create the image handle |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 514 | if ( ! ($src_img = $this->image_create_gd())) |
| 515 | { |
| 516 | return FALSE; |
| 517 | } |
| 518 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 519 | // Create The Image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 520 | // |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 521 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 524 | // |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 525 | // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 526 | if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 527 | { |
| 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 Jones | 595bfd1 | 2010-08-20 10:28:22 -0500 | [diff] [blame] | 538 | |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 544 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 545 | $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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 547 | // Show the image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 548 | 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 561 | // Kill the file handles |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 562 | imagedestroy($dst_img); |
| 563 | imagedestroy($src_img); |
| 564 | |
| 565 | // Set the file to 777 |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 566 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 567 | |
| 568 | return TRUE; |
| 569 | } |
| 570 | |
| 571 | // -------------------------------------------------------------------- |
| 572 | |
| 573 | /** |
| 574 | * Image Process Using ImageMagick |
| 575 | * |
| 576 | * This function will resize, crop or rotate |
| 577 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 578 | * @param string |
| 579 | * @return bool |
| 580 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 581 | public function image_process_imagemagick($action = 'resize') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 583 | // Do we have a vaild library path? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 584 | if ($this->library_path == '') |
| 585 | { |
| 586 | $this->set_error('imglib_libpath_invalid'); |
| 587 | return FALSE; |
| 588 | } |
| 589 | |
Derek Jones | 1322ec5 | 2009-03-11 17:01:14 +0000 | [diff] [blame] | 590 | if ( ! preg_match("/convert$/i", $this->library_path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 592 | $this->library_path = rtrim($this->library_path, '/').'/convert'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 593 | } |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 606 | case 'hor' : $angle = '-flop'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 607 | break; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 608 | case 'vrt' : $angle = '-flip'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 609 | 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 616 | else // Resize |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 617 | { |
| 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 Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 633 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 634 | |
| 635 | return TRUE; |
| 636 | } |
| 637 | |
| 638 | // -------------------------------------------------------------------- |
| 639 | |
| 640 | /** |
| 641 | * Image Process Using NetPBM |
| 642 | * |
| 643 | * This function will resize, crop or rotate |
| 644 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 645 | * @param string |
| 646 | * @return bool |
| 647 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 648 | public function image_process_netpbm($action = 'resize') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 649 | { |
| 650 | if ($this->library_path == '') |
| 651 | { |
| 652 | $this->set_error('imglib_libpath_invalid'); |
| 653 | return FALSE; |
| 654 | } |
| 655 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 656 | // Build the resizing command |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 657 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 685 | case 270 : $angle = 'r90'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 686 | 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 706 | // Did it work? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 707 | 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 Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 718 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 719 | |
| 720 | return TRUE; |
| 721 | } |
| 722 | |
| 723 | // -------------------------------------------------------------------- |
| 724 | |
| 725 | /** |
| 726 | * Image Rotate Using GD |
| 727 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 728 | * @return bool |
| 729 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 730 | public function image_rotate_gd() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 731 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 732 | // Create the image handle |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 733 | 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 745 | // Rotate it! |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 746 | $dst_img = imagerotate($src_img, $this->rotation_angle, $white); |
| 747 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 748 | // Save the Image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 749 | 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 762 | // Kill the file handles |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 763 | imagedestroy($dst_img); |
| 764 | imagedestroy($src_img); |
| 765 | |
| 766 | // Set the file to 777 |
| 767 | |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 768 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 769 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 770 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | // -------------------------------------------------------------------- |
| 774 | |
| 775 | /** |
| 776 | * Create Mirror Image using GD |
| 777 | * |
| 778 | * This function will flip horizontal or vertical |
| 779 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 780 | * @return bool |
| 781 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 782 | public function image_mirror_gd() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 783 | { |
| 784 | if ( ! $src_img = $this->image_create_gd()) |
| 785 | { |
| 786 | return FALSE; |
| 787 | } |
| 788 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 789 | $width = $this->orig_width; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 790 | $height = $this->orig_height; |
| 791 | |
| 792 | if ($this->rotation_angle == 'hor') |
| 793 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 794 | for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 795 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 796 | 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 Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 811 | for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 812 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 813 | 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 827 | // Show the image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 828 | 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 841 | // Kill the file handles |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 842 | imagedestroy($src_img); |
| 843 | |
| 844 | // Set the file to 777 |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 845 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 846 | |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 858 | * @param string |
| 859 | * @return bool |
| 860 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 861 | public function watermark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 862 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 878 | * @return bool |
| 879 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 880 | public function overlay_watermark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 881 | { |
| 882 | if ( ! function_exists('imagecolortransparent')) |
| 883 | { |
| 884 | $this->set_error('imglib_gd_required'); |
| 885 | return FALSE; |
| 886 | } |
| 887 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 888 | // Fetch source image properties |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 889 | $this->get_image_properties(); |
| 890 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 891 | // Fetch watermark image properties |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 892 | $props = $this->get_image_properties($this->wm_overlay_path, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 893 | $wm_img_type = $props['image_type']; |
| 894 | $wm_width = $props['width']; |
| 895 | $wm_height = $props['height']; |
| 896 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 897 | // Create two image resources |
| 898 | $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 899 | $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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 904 | // further down. We want the reverse, so we'll |
| 905 | // invert the offset. Same with the horizontal |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 906 | // 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 917 | // Set the base x and y axis values |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 918 | $x_axis = $this->wm_hor_offset + $this->wm_padding; |
| 919 | $y_axis = $this->wm_vrt_offset + $this->wm_padding; |
| 920 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 921 | // Set the vertical position |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 922 | 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 932 | // Set the horizontal position |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 933 | 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 943 | // Build the finalized image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 944 | if ($wm_img_type == 3 AND function_exists('imagealphablending')) |
| 945 | { |
| 946 | @imagealphablending($src_img, TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 947 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 948 | |
| 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 966 | // Output the image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 967 | if ($this->dynamic_output == TRUE) |
| 968 | { |
| 969 | $this->image_display_gd($src_img); |
| 970 | } |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 971 | elseif ( ! $this->image_save_gd($src_img)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 972 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 973 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | imagedestroy($src_img); |
| 977 | imagedestroy($wm_img); |
| 978 | |
| 979 | return TRUE; |
| 980 | } |
| 981 | |
| 982 | // -------------------------------------------------------------------- |
| 983 | |
| 984 | /** |
| 985 | * Watermark - Text Version |
| 986 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 987 | * @return bool |
| 988 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 989 | public function text_watermark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 990 | { |
| 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1002 | // Fetch source image properties |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1003 | $this->get_image_properties(); |
| 1004 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1005 | // 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1008 | // further down. We want the reverse, so we'll |
| 1009 | // invert the offset. Note: The horizontal |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1010 | // 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 Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1024 | { |
| 1025 | $this->wm_font_size = 17; |
| 1026 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1027 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1028 | $fontwidth = $this->wm_font_size-($this->wm_font_size/4); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1029 | $fontheight = $this->wm_font_size; |
| 1030 | $this->wm_vrt_offset += $this->wm_font_size; |
| 1031 | } |
| 1032 | else |
| 1033 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1034 | $fontwidth = imagefontwidth($this->wm_font_size); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1035 | $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 Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1051 | case 'T': |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1052 | break; |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1053 | case 'M': $y_axis += ($this->orig_height/2)+($fontheight/2); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1054 | break; |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1055 | case 'B': $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1056 | 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 Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1065 | case 'L': |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1066 | break; |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1067 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1073 | break; |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1074 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1080 | break; |
| 1081 | } |
| 1082 | |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 1083 | if ($this->wm_use_drop_shadow) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1084 | { |
Andrey Andreev | 8323ae6 | 2011-12-31 18:39:10 +0200 | [diff] [blame] | 1085 | /* 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 Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 1092 | $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2])); |
Andrey Andreev | 8323ae6 | 2011-12-31 18:39:10 +0200 | [diff] [blame] | 1093 | $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2); |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 1094 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1109 | // Output the final image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1110 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1132 | * @param string |
| 1133 | * @return resource |
| 1134 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1135 | public function image_create_gd($path = '', $image_type = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1136 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1188 | * @param resource |
| 1189 | * @return bool |
| 1190 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1191 | public function image_save_gd($resource) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1192 | { |
| 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 Jones | 541ddbd | 2008-12-09 15:25:31 +0000 | [diff] [blame] | 1202 | if ( ! @imagegif($resource, $this->full_dst_path)) |
| 1203 | { |
| 1204 | $this->set_error('imglib_save_failed'); |
| 1205 | return FALSE; |
| 1206 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1207 | 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 Jones | 541ddbd | 2008-12-09 15:25:31 +0000 | [diff] [blame] | 1215 | if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality)) |
| 1216 | { |
| 1217 | $this->set_error('imglib_save_failed'); |
| 1218 | return FALSE; |
| 1219 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1220 | 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 Jones | 541ddbd | 2008-12-09 15:25:31 +0000 | [diff] [blame] | 1228 | if ( ! @imagepng($resource, $this->full_dst_path)) |
| 1229 | { |
| 1230 | $this->set_error('imglib_save_failed'); |
| 1231 | return FALSE; |
| 1232 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1233 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1248 | * @param resource |
| 1249 | * @return void |
| 1250 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1251 | public function image_display_gd($resource) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1252 | { |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1260 | case 1 : imagegif($resource); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1261 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1283 | * @return void |
| 1284 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1285 | public function image_reproportion() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1286 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1323 | * @param string |
| 1324 | * @return mixed |
| 1325 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1326 | public function get_image_properties($path = '', $return = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1327 | { |
| 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 Sturgeon | 901998a | 2011-08-26 10:03:33 +0100 | [diff] [blame] | 1340 | $vals = getimagesize($path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1341 | $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png'); |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1342 | $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1343 | |
| 1344 | if ($return == TRUE) |
| 1345 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1346 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1355 | $this->orig_width = $vals[0]; |
| 1356 | $this->orig_height = $vals[1]; |
| 1357 | $this->image_type = $vals[2]; |
| 1358 | $this->size_str = $vals[3]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1359 | $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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1370 | * recalculates it to a new size. Only one |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1371 | * new variable needs to be known |
| 1372 | * |
| 1373 | * $props = array( |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1374 | * 'width' => $width, |
| 1375 | * 'height' => $height, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1376 | * 'new_width' => 40, |
| 1377 | * 'new_height' => '' |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1378 | * ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1379 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1380 | * @param array |
| 1381 | * @return array |
| 1382 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1383 | public function size_calculator($vals) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1384 | { |
| 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1421 | * from the source_image. This function lets us deal with |
| 1422 | * source_images with multiple periods, like: my.cool.jpg |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1423 | * It returns an associative array with two elements: |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1424 | * $array['ext'] = '.jpg'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1425 | * $array['name'] = 'my.cool'; |
| 1426 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1427 | * @param array |
| 1428 | * @return array |
| 1429 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1430 | public function explode_name($source_image) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1431 | { |
Derek Jones | 08cae63 | 2009-02-10 20:03:29 +0000 | [diff] [blame] | 1432 | $ext = strrchr($source_image, '.'); |
| 1433 | $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1434 | |
Derek Jones | 08cae63 | 2009-02-10 20:03:29 +0000 | [diff] [blame] | 1435 | return array('ext' => $ext, 'name' => $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | // -------------------------------------------------------------------- |
| 1439 | |
| 1440 | /** |
| 1441 | * Is GD Installed? |
| 1442 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1443 | * @return bool |
| 1444 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1445 | public function gd_loaded() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1446 | { |
| 1447 | if ( ! extension_loaded('gd')) |
| 1448 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1449 | /* 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | return TRUE; |
| 1456 | } |
| 1457 | |
| 1458 | // -------------------------------------------------------------------- |
| 1459 | |
| 1460 | /** |
| 1461 | * Get GD version |
| 1462 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1463 | * @return mixed |
| 1464 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1465 | public function gd_version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1466 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1483 | * @param string |
| 1484 | * @return void |
| 1485 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1486 | public function set_error($msg) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1487 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1514 | * @param string |
| 1515 | * @return string |
| 1516 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1517 | public function display_errors($open = '<p>', $close = '</p>') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1518 | { |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1519 | return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | } |
| 1523 | // END Image_lib Class |
| 1524 | |
| 1525 | /* End of file Image_lib.php */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1526 | /* Location: ./system/libraries/Image_lib.php */ |