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 | { |
| 172 | if ($val != '' AND preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches)) |
| 173 | { |
| 174 | if (strlen($matches[1]) === 6) |
| 175 | { |
| 176 | $val = '#'.$val; |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | $val = str_split($val, 1); |
| 181 | $val = '#'.$val[0].$val[0].$val[1].$val[1].$val[2].$val[2]; |
| 182 | } |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | continue; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | $this->$key = $val; |
| 191 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Is there a source image? |
| 197 | * |
| 198 | * If not, there's no reason to continue |
| 199 | * |
| 200 | */ |
| 201 | if ($this->source_image == '') |
| 202 | { |
| 203 | $this->set_error('imglib_source_image_required'); |
Eric Barnes | b167336 | 2011-12-05 22:05:38 -0500 | [diff] [blame] | 204 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Is getimagesize() Available? |
| 209 | * |
| 210 | * We use it to determine the image properties (width/height). |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 211 | * Note: We need to figure out how to determine image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | * properties using ImageMagick and NetPBM |
| 213 | * |
| 214 | */ |
| 215 | if ( ! function_exists('getimagesize')) |
| 216 | { |
| 217 | $this->set_error('imglib_gd_required_for_props'); |
| 218 | return FALSE; |
| 219 | } |
| 220 | |
| 221 | $this->image_library = strtolower($this->image_library); |
| 222 | |
| 223 | /* |
| 224 | * Set the full server path |
| 225 | * |
| 226 | * The source image may or may not contain a path. |
| 227 | * Either way, we'll try use realpath to generate the |
| 228 | * full server path in order to more reliably read it. |
| 229 | * |
| 230 | */ |
| 231 | if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE) |
| 232 | { |
| 233 | $full_source_path = str_replace("\\", "/", realpath($this->source_image)); |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | $full_source_path = $this->source_image; |
| 238 | } |
| 239 | |
| 240 | $x = explode('/', $full_source_path); |
| 241 | $this->source_image = end($x); |
| 242 | $this->source_folder = str_replace($this->source_image, '', $full_source_path); |
| 243 | |
| 244 | // Set the Image Properties |
| 245 | if ( ! $this->get_image_properties($this->source_folder.$this->source_image)) |
| 246 | { |
Eric Barnes | b167336 | 2011-12-05 22:05:38 -0500 | [diff] [blame] | 247 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Assign the "new" image name/path |
| 252 | * |
| 253 | * If the user has set a "new_image" name it means |
| 254 | * we are making a copy of the source image. If not |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 255 | * it means we are altering the original. We'll |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | * set the destination filename and path accordingly. |
| 257 | * |
| 258 | */ |
| 259 | if ($this->new_image == '') |
| 260 | { |
| 261 | $this->dest_image = $this->source_image; |
| 262 | $this->dest_folder = $this->source_folder; |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | if (strpos($this->new_image, '/') === FALSE) |
| 267 | { |
| 268 | $this->dest_folder = $this->source_folder; |
| 269 | $this->dest_image = $this->new_image; |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE) |
| 274 | { |
| 275 | $full_dest_path = str_replace("\\", "/", realpath($this->new_image)); |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | $full_dest_path = $this->new_image; |
| 280 | } |
| 281 | |
| 282 | // Is there a file name? |
| 283 | if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path)) |
| 284 | { |
| 285 | $this->dest_folder = $full_dest_path.'/'; |
| 286 | $this->dest_image = $this->source_image; |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | $x = explode('/', $full_dest_path); |
| 291 | $this->dest_image = end($x); |
| 292 | $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Compile the finalized filenames/paths |
| 299 | * |
| 300 | * We'll create two master strings containing the |
| 301 | * full server path to the source image and the |
| 302 | * full server path to the destination image. |
| 303 | * We'll also split the destination image name |
| 304 | * so we can insert the thumbnail marker if needed. |
| 305 | * |
| 306 | */ |
| 307 | if ($this->create_thumb === FALSE OR $this->thumb_marker == '') |
| 308 | { |
| 309 | $this->thumb_marker = ''; |
| 310 | } |
| 311 | |
| 312 | $xp = $this->explode_name($this->dest_image); |
| 313 | |
| 314 | $filename = $xp['name']; |
| 315 | $file_ext = $xp['ext']; |
| 316 | |
| 317 | $this->full_src_path = $this->source_folder.$this->source_image; |
| 318 | $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext; |
| 319 | |
| 320 | /* |
| 321 | * Should we maintain image proportions? |
| 322 | * |
| 323 | * When creating thumbs or copies, the target width/height |
| 324 | * might not be in correct proportion with the source |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 325 | * image's width/height. We'll recalculate it here. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | * |
| 327 | */ |
| 328 | if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != '')) |
| 329 | { |
| 330 | $this->image_reproportion(); |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Was a width and height specified? |
| 335 | * |
| 336 | * If the destination width/height was |
| 337 | * not submitted we will use the values |
| 338 | * from the actual file |
| 339 | * |
| 340 | */ |
| 341 | if ($this->width == '') |
| 342 | $this->width = $this->orig_width; |
| 343 | |
| 344 | if ($this->height == '') |
| 345 | $this->height = $this->orig_height; |
| 346 | |
| 347 | // Set the quality |
| 348 | $this->quality = trim(str_replace("%", "", $this->quality)); |
| 349 | |
| 350 | if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality)) |
| 351 | $this->quality = 90; |
| 352 | |
| 353 | // Set the x/y coordinates |
| 354 | $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis; |
| 355 | $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis; |
| 356 | |
| 357 | // Watermark-related Stuff... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | if ($this->wm_overlay_path != '') |
| 359 | { |
| 360 | $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path)); |
| 361 | } |
| 362 | |
| 363 | if ($this->wm_shadow_color != '') |
| 364 | { |
| 365 | $this->wm_use_drop_shadow = TRUE; |
| 366 | } |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame^] | 367 | elseif ($this->wm_use_drop_shadow == TRUE AND $this->wm_shadow_color == '') |
| 368 | { |
| 369 | $this->wm_use_drop_shadow = FALSE; |
| 370 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | |
| 372 | if ($this->wm_font_path != '') |
| 373 | { |
| 374 | $this->wm_use_truetype = TRUE; |
| 375 | } |
| 376 | |
| 377 | return TRUE; |
| 378 | } |
| 379 | |
| 380 | // -------------------------------------------------------------------- |
| 381 | |
| 382 | /** |
| 383 | * Image Resize |
| 384 | * |
| 385 | * This is a wrapper function that chooses the proper |
| 386 | * resize function based on the protocol specified |
| 387 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | * @return bool |
| 389 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 390 | public function resize() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 392 | $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] | 393 | return $this->$protocol('resize'); |
| 394 | } |
| 395 | |
| 396 | // -------------------------------------------------------------------- |
| 397 | |
| 398 | /** |
| 399 | * Image Crop |
| 400 | * |
| 401 | * This is a wrapper function that chooses the proper |
| 402 | * cropping function based on the protocol specified |
| 403 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | * @return bool |
| 405 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 406 | public function crop() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 408 | $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] | 409 | return $this->$protocol('crop'); |
| 410 | } |
| 411 | |
| 412 | // -------------------------------------------------------------------- |
| 413 | |
| 414 | /** |
| 415 | * Image Rotate |
| 416 | * |
| 417 | * This is a wrapper function that chooses the proper |
| 418 | * rotation function based on the protocol specified |
| 419 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | * @return bool |
| 421 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 422 | public function rotate() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | { |
| 424 | // Allowed rotation values |
| 425 | $degs = array(90, 180, 270, 'vrt', 'hor'); |
| 426 | |
Derek Allard | d9c7f03 | 2008-12-01 20:18:00 +0000 | [diff] [blame] | 427 | if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | { |
| 429 | $this->set_error('imglib_rotation_angle_required'); |
Eric Barnes | b167336 | 2011-12-05 22:05:38 -0500 | [diff] [blame] | 430 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | // Reassign the width and height |
| 434 | if ($this->rotation_angle == 90 OR $this->rotation_angle == 270) |
| 435 | { |
| 436 | $this->width = $this->orig_height; |
| 437 | $this->height = $this->orig_width; |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | $this->width = $this->orig_width; |
| 442 | $this->height = $this->orig_height; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | // Choose resizing function |
| 447 | if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm') |
| 448 | { |
| 449 | $protocol = 'image_process_'.$this->image_library; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 450 | return $this->$protocol('rotate'); |
| 451 | } |
| 452 | |
| 453 | if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt') |
| 454 | { |
| 455 | return $this->image_mirror_gd(); |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | return $this->image_rotate_gd(); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | // -------------------------------------------------------------------- |
| 464 | |
| 465 | /** |
| 466 | * Image Process Using GD/GD2 |
| 467 | * |
| 468 | * This function will resize or crop |
| 469 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | * @param string |
| 471 | * @return bool |
| 472 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 473 | public function image_process_gd($action = 'resize') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | { |
| 475 | $v2_override = FALSE; |
| 476 | |
| 477 | // If the target width/height match the source, AND if the new file name is not equal to the old file name |
| 478 | // 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] | 479 | 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] | 480 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 481 | 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] | 482 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 483 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | } |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 485 | |
| 486 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // Let's set up our values based on the action |
| 490 | if ($action == 'crop') |
| 491 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 492 | // Reassign the source width/height if cropping |
| 493 | $this->orig_width = $this->width; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 494 | $this->orig_height = $this->height; |
| 495 | |
| 496 | // GD 2.0 has a cropping bug so we'll test for it |
| 497 | if ($this->gd_version() !== FALSE) |
| 498 | { |
| 499 | $gd_version = str_replace('0', '', $this->gd_version()); |
| 500 | $v2_override = ($gd_version == 2) ? TRUE : FALSE; |
| 501 | } |
| 502 | } |
| 503 | else |
| 504 | { |
| 505 | // If resizing the x/y axis must be zero |
| 506 | $this->x_axis = 0; |
| 507 | $this->y_axis = 0; |
| 508 | } |
| 509 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 510 | // Create the image handle |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | if ( ! ($src_img = $this->image_create_gd())) |
| 512 | { |
| 513 | return FALSE; |
| 514 | } |
| 515 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 516 | // Create The Image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 517 | // |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 518 | // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater" |
| 519 | // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment |
| 520 | // below should that ever prove inaccurate. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 521 | // |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 522 | // 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] | 523 | if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 524 | { |
| 525 | $create = 'imagecreatetruecolor'; |
| 526 | $copy = 'imagecopyresampled'; |
| 527 | } |
| 528 | else |
| 529 | { |
| 530 | $create = 'imagecreate'; |
| 531 | $copy = 'imagecopyresized'; |
| 532 | } |
| 533 | |
| 534 | $dst_img = $create($this->width, $this->height); |
Derek Jones | 595bfd1 | 2010-08-20 10:28:22 -0500 | [diff] [blame] | 535 | |
| 536 | if ($this->image_type == 3) // png we can actually preserve transparency |
| 537 | { |
| 538 | imagealphablending($dst_img, FALSE); |
| 539 | imagesavealpha($dst_img, TRUE); |
| 540 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 541 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 542 | $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height); |
| 543 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 544 | // Show the image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 545 | if ($this->dynamic_output == TRUE) |
| 546 | { |
| 547 | $this->image_display_gd($dst_img); |
| 548 | } |
| 549 | else |
| 550 | { |
| 551 | // Or save it |
| 552 | if ( ! $this->image_save_gd($dst_img)) |
| 553 | { |
| 554 | return FALSE; |
| 555 | } |
| 556 | } |
| 557 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 558 | // Kill the file handles |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 559 | imagedestroy($dst_img); |
| 560 | imagedestroy($src_img); |
| 561 | |
| 562 | // Set the file to 777 |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 563 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 564 | |
| 565 | return TRUE; |
| 566 | } |
| 567 | |
| 568 | // -------------------------------------------------------------------- |
| 569 | |
| 570 | /** |
| 571 | * Image Process Using ImageMagick |
| 572 | * |
| 573 | * This function will resize, crop or rotate |
| 574 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 575 | * @param string |
| 576 | * @return bool |
| 577 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 578 | public function image_process_imagemagick($action = 'resize') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 579 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 580 | // Do we have a vaild library path? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 581 | if ($this->library_path == '') |
| 582 | { |
| 583 | $this->set_error('imglib_libpath_invalid'); |
| 584 | return FALSE; |
| 585 | } |
| 586 | |
Derek Jones | 1322ec5 | 2009-03-11 17:01:14 +0000 | [diff] [blame] | 587 | if ( ! preg_match("/convert$/i", $this->library_path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 588 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 589 | $this->library_path = rtrim($this->library_path, '/').'/convert'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | // Execute the command |
| 593 | $cmd = $this->library_path." -quality ".$this->quality; |
| 594 | |
| 595 | if ($action == 'crop') |
| 596 | { |
| 597 | $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1"; |
| 598 | } |
| 599 | elseif ($action == 'rotate') |
| 600 | { |
| 601 | switch ($this->rotation_angle) |
| 602 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 603 | case 'hor' : $angle = '-flop'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 604 | break; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 605 | case 'vrt' : $angle = '-flip'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 606 | break; |
| 607 | default : $angle = '-rotate '.$this->rotation_angle; |
| 608 | break; |
| 609 | } |
| 610 | |
| 611 | $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1"; |
| 612 | } |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 613 | else // Resize |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 614 | { |
| 615 | $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1"; |
| 616 | } |
| 617 | |
| 618 | $retval = 1; |
| 619 | |
| 620 | @exec($cmd, $output, $retval); |
| 621 | |
| 622 | // Did it work? |
| 623 | if ($retval > 0) |
| 624 | { |
| 625 | $this->set_error('imglib_image_process_failed'); |
| 626 | return FALSE; |
| 627 | } |
| 628 | |
| 629 | // Set the file to 777 |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 630 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 631 | |
| 632 | return TRUE; |
| 633 | } |
| 634 | |
| 635 | // -------------------------------------------------------------------- |
| 636 | |
| 637 | /** |
| 638 | * Image Process Using NetPBM |
| 639 | * |
| 640 | * This function will resize, crop or rotate |
| 641 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 642 | * @param string |
| 643 | * @return bool |
| 644 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 645 | public function image_process_netpbm($action = 'resize') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 646 | { |
| 647 | if ($this->library_path == '') |
| 648 | { |
| 649 | $this->set_error('imglib_libpath_invalid'); |
| 650 | return FALSE; |
| 651 | } |
| 652 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 653 | // Build the resizing command |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 654 | switch ($this->image_type) |
| 655 | { |
| 656 | case 1 : |
| 657 | $cmd_in = 'giftopnm'; |
| 658 | $cmd_out = 'ppmtogif'; |
| 659 | break; |
| 660 | case 2 : |
| 661 | $cmd_in = 'jpegtopnm'; |
| 662 | $cmd_out = 'ppmtojpeg'; |
| 663 | break; |
| 664 | case 3 : |
| 665 | $cmd_in = 'pngtopnm'; |
| 666 | $cmd_out = 'ppmtopng'; |
| 667 | break; |
| 668 | } |
| 669 | |
| 670 | if ($action == 'crop') |
| 671 | { |
| 672 | $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height; |
| 673 | } |
| 674 | elseif ($action == 'rotate') |
| 675 | { |
| 676 | switch ($this->rotation_angle) |
| 677 | { |
| 678 | case 90 : $angle = 'r270'; |
| 679 | break; |
| 680 | case 180 : $angle = 'r180'; |
| 681 | break; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 682 | case 270 : $angle = 'r90'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 683 | break; |
| 684 | case 'vrt' : $angle = 'tb'; |
| 685 | break; |
| 686 | case 'hor' : $angle = 'lr'; |
| 687 | break; |
| 688 | } |
| 689 | |
| 690 | $cmd_inner = 'pnmflip -'.$angle.' '; |
| 691 | } |
| 692 | else // Resize |
| 693 | { |
| 694 | $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height; |
| 695 | } |
| 696 | |
| 697 | $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp'; |
| 698 | |
| 699 | $retval = 1; |
| 700 | |
| 701 | @exec($cmd, $output, $retval); |
| 702 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 703 | // Did it work? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 704 | if ($retval > 0) |
| 705 | { |
| 706 | $this->set_error('imglib_image_process_failed'); |
| 707 | return FALSE; |
| 708 | } |
| 709 | |
| 710 | // With NetPBM we have to create a temporary image. |
| 711 | // If you try manipulating the original it fails so |
| 712 | // we have to rename the temp file. |
| 713 | copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path); |
| 714 | unlink ($this->dest_folder.'netpbm.tmp'); |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 715 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 716 | |
| 717 | return TRUE; |
| 718 | } |
| 719 | |
| 720 | // -------------------------------------------------------------------- |
| 721 | |
| 722 | /** |
| 723 | * Image Rotate Using GD |
| 724 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 725 | * @return bool |
| 726 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 727 | public function image_rotate_gd() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 728 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 729 | // Create the image handle |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 730 | if ( ! ($src_img = $this->image_create_gd())) |
| 731 | { |
| 732 | return FALSE; |
| 733 | } |
| 734 | |
| 735 | // Set the background color |
| 736 | // This won't work with transparent PNG files so we are |
| 737 | // going to have to figure out how to determine the color |
| 738 | // of the alpha channel in a future release. |
| 739 | |
| 740 | $white = imagecolorallocate($src_img, 255, 255, 255); |
| 741 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 742 | // Rotate it! |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 743 | $dst_img = imagerotate($src_img, $this->rotation_angle, $white); |
| 744 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 745 | // Save the Image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 746 | if ($this->dynamic_output == TRUE) |
| 747 | { |
| 748 | $this->image_display_gd($dst_img); |
| 749 | } |
| 750 | else |
| 751 | { |
| 752 | // Or save it |
| 753 | if ( ! $this->image_save_gd($dst_img)) |
| 754 | { |
| 755 | return FALSE; |
| 756 | } |
| 757 | } |
| 758 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 759 | // Kill the file handles |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 760 | imagedestroy($dst_img); |
| 761 | imagedestroy($src_img); |
| 762 | |
| 763 | // Set the file to 777 |
| 764 | |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 765 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 766 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 767 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | // -------------------------------------------------------------------- |
| 771 | |
| 772 | /** |
| 773 | * Create Mirror Image using GD |
| 774 | * |
| 775 | * This function will flip horizontal or vertical |
| 776 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 777 | * @return bool |
| 778 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 779 | public function image_mirror_gd() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 780 | { |
| 781 | if ( ! $src_img = $this->image_create_gd()) |
| 782 | { |
| 783 | return FALSE; |
| 784 | } |
| 785 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 786 | $width = $this->orig_width; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 787 | $height = $this->orig_height; |
| 788 | |
| 789 | if ($this->rotation_angle == 'hor') |
| 790 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 791 | for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 792 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 793 | while ($left < $right) |
| 794 | { |
| 795 | $cl = imagecolorat($src_img, $left, $i); |
| 796 | $cr = imagecolorat($src_img, $right, $i); |
| 797 | |
| 798 | imagesetpixel($src_img, $left, $i, $cr); |
| 799 | imagesetpixel($src_img, $right, $i, $cl); |
| 800 | |
| 801 | $left++; |
| 802 | $right--; |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | else |
| 807 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 808 | for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 809 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 810 | while ($top < $bot) |
| 811 | { |
| 812 | $ct = imagecolorat($src_img, $i, $top); |
| 813 | $cb = imagecolorat($src_img, $i, $bot); |
| 814 | |
| 815 | imagesetpixel($src_img, $i, $top, $cb); |
| 816 | imagesetpixel($src_img, $i, $bot, $ct); |
| 817 | |
| 818 | $top++; |
| 819 | $bot--; |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 824 | // Show the image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 825 | if ($this->dynamic_output == TRUE) |
| 826 | { |
| 827 | $this->image_display_gd($src_img); |
| 828 | } |
| 829 | else |
| 830 | { |
| 831 | // Or save it |
| 832 | if ( ! $this->image_save_gd($src_img)) |
| 833 | { |
| 834 | return FALSE; |
| 835 | } |
| 836 | } |
| 837 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 838 | // Kill the file handles |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 839 | imagedestroy($src_img); |
| 840 | |
| 841 | // Set the file to 777 |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 842 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 843 | |
| 844 | return TRUE; |
| 845 | } |
| 846 | |
| 847 | // -------------------------------------------------------------------- |
| 848 | |
| 849 | /** |
| 850 | * Image Watermark |
| 851 | * |
| 852 | * This is a wrapper function that chooses the type |
| 853 | * of watermarking based on the specified preference. |
| 854 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 855 | * @param string |
| 856 | * @return bool |
| 857 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 858 | public function watermark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 859 | { |
| 860 | if ($this->wm_type == 'overlay') |
| 861 | { |
| 862 | return $this->overlay_watermark(); |
| 863 | } |
| 864 | else |
| 865 | { |
| 866 | return $this->text_watermark(); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | // -------------------------------------------------------------------- |
| 871 | |
| 872 | /** |
| 873 | * Watermark - Graphic Version |
| 874 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 875 | * @return bool |
| 876 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 877 | public function overlay_watermark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 878 | { |
| 879 | if ( ! function_exists('imagecolortransparent')) |
| 880 | { |
| 881 | $this->set_error('imglib_gd_required'); |
| 882 | return FALSE; |
| 883 | } |
| 884 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 885 | // Fetch source image properties |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 886 | $this->get_image_properties(); |
| 887 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 888 | // Fetch watermark image properties |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 889 | $props = $this->get_image_properties($this->wm_overlay_path, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 890 | $wm_img_type = $props['image_type']; |
| 891 | $wm_width = $props['width']; |
| 892 | $wm_height = $props['height']; |
| 893 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 894 | // Create two image resources |
| 895 | $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] | 896 | $src_img = $this->image_create_gd($this->full_src_path); |
| 897 | |
| 898 | // Reverse the offset if necessary |
| 899 | // When the image is positioned at the bottom |
| 900 | // we don't want the vertical offset to push it |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 901 | // further down. We want the reverse, so we'll |
| 902 | // invert the offset. Same with the horizontal |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 903 | // offset when the image is at the right |
| 904 | |
| 905 | $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1)); |
| 906 | $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1)); |
| 907 | |
| 908 | if ($this->wm_vrt_alignment == 'B') |
| 909 | $this->wm_vrt_offset = $this->wm_vrt_offset * -1; |
| 910 | |
| 911 | if ($this->wm_hor_alignment == 'R') |
| 912 | $this->wm_hor_offset = $this->wm_hor_offset * -1; |
| 913 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 914 | // Set the base x and y axis values |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 915 | $x_axis = $this->wm_hor_offset + $this->wm_padding; |
| 916 | $y_axis = $this->wm_vrt_offset + $this->wm_padding; |
| 917 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 918 | // Set the vertical position |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 919 | switch ($this->wm_vrt_alignment) |
| 920 | { |
| 921 | case 'T': |
| 922 | break; |
| 923 | case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2); |
| 924 | break; |
| 925 | case 'B': $y_axis += $this->orig_height - $wm_height; |
| 926 | break; |
| 927 | } |
| 928 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 929 | // Set the horizontal position |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 930 | switch ($this->wm_hor_alignment) |
| 931 | { |
| 932 | case 'L': |
| 933 | break; |
| 934 | case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2); |
| 935 | break; |
| 936 | case 'R': $x_axis += $this->orig_width - $wm_width; |
| 937 | break; |
| 938 | } |
| 939 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 940 | // Build the finalized image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 941 | if ($wm_img_type == 3 AND function_exists('imagealphablending')) |
| 942 | { |
| 943 | @imagealphablending($src_img, TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 944 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 945 | |
| 946 | // Set RGB values for text and shadow |
| 947 | $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp); |
| 948 | $alpha = ($rgba & 0x7F000000) >> 24; |
| 949 | |
| 950 | // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency |
| 951 | if ($alpha > 0) |
| 952 | { |
| 953 | // copy the image directly, the image's alpha transparency being the sole determinant of blending |
| 954 | imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height); |
| 955 | } |
| 956 | else |
| 957 | { |
| 958 | // set our RGB value from above to be transparent and merge the images with the specified opacity |
| 959 | imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp)); |
| 960 | imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity); |
| 961 | } |
| 962 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 963 | // Output the image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 964 | if ($this->dynamic_output == TRUE) |
| 965 | { |
| 966 | $this->image_display_gd($src_img); |
| 967 | } |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 968 | elseif ( ! $this->image_save_gd($src_img)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 969 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 970 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | imagedestroy($src_img); |
| 974 | imagedestroy($wm_img); |
| 975 | |
| 976 | return TRUE; |
| 977 | } |
| 978 | |
| 979 | // -------------------------------------------------------------------- |
| 980 | |
| 981 | /** |
| 982 | * Watermark - Text Version |
| 983 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 984 | * @return bool |
| 985 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 986 | public function text_watermark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 987 | { |
| 988 | if ( ! ($src_img = $this->image_create_gd())) |
| 989 | { |
| 990 | return FALSE; |
| 991 | } |
| 992 | |
| 993 | if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path)) |
| 994 | { |
| 995 | $this->set_error('imglib_missing_font'); |
| 996 | return FALSE; |
| 997 | } |
| 998 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 999 | // Fetch source image properties |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1000 | $this->get_image_properties(); |
| 1001 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1002 | // Reverse the vertical offset |
| 1003 | // When the image is positioned at the bottom |
| 1004 | // we don't want the vertical offset to push it |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1005 | // further down. We want the reverse, so we'll |
| 1006 | // invert the offset. Note: The horizontal |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1007 | // offset flips itself automatically |
| 1008 | |
| 1009 | if ($this->wm_vrt_alignment == 'B') |
| 1010 | $this->wm_vrt_offset = $this->wm_vrt_offset * -1; |
| 1011 | |
| 1012 | if ($this->wm_hor_alignment == 'R') |
| 1013 | $this->wm_hor_offset = $this->wm_hor_offset * -1; |
| 1014 | |
| 1015 | // Set font width and height |
| 1016 | // These are calculated differently depending on |
| 1017 | // whether we are using the true type font or not |
| 1018 | if ($this->wm_use_truetype == TRUE) |
| 1019 | { |
| 1020 | if ($this->wm_font_size == '') |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1021 | { |
| 1022 | $this->wm_font_size = 17; |
| 1023 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1024 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1025 | $fontwidth = $this->wm_font_size-($this->wm_font_size/4); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1026 | $fontheight = $this->wm_font_size; |
| 1027 | $this->wm_vrt_offset += $this->wm_font_size; |
| 1028 | } |
| 1029 | else |
| 1030 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1031 | $fontwidth = imagefontwidth($this->wm_font_size); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1032 | $fontheight = imagefontheight($this->wm_font_size); |
| 1033 | } |
| 1034 | |
| 1035 | // Set base X and Y axis values |
| 1036 | $x_axis = $this->wm_hor_offset + $this->wm_padding; |
| 1037 | $y_axis = $this->wm_vrt_offset + $this->wm_padding; |
| 1038 | |
| 1039 | // Set verticle alignment |
| 1040 | if ($this->wm_use_drop_shadow == FALSE) |
| 1041 | $this->wm_shadow_distance = 0; |
| 1042 | |
| 1043 | $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1)); |
| 1044 | $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1)); |
| 1045 | |
| 1046 | switch ($this->wm_vrt_alignment) |
| 1047 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1048 | case 'T': |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1049 | break; |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1050 | case 'M': $y_axis += ($this->orig_height/2)+($fontheight/2); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1051 | break; |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1052 | 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] | 1053 | break; |
| 1054 | } |
| 1055 | |
| 1056 | $x_shad = $x_axis + $this->wm_shadow_distance; |
| 1057 | $y_shad = $y_axis + $this->wm_shadow_distance; |
| 1058 | |
| 1059 | // Set horizontal alignment |
| 1060 | switch ($this->wm_hor_alignment) |
| 1061 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1062 | case 'L': |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1063 | break; |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1064 | case 'R': |
| 1065 | if ($this->wm_use_drop_shadow) |
| 1066 | { |
| 1067 | $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text)); |
| 1068 | $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text)); |
| 1069 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1070 | break; |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1071 | case 'C': |
| 1072 | if ($this->wm_use_drop_shadow) |
| 1073 | { |
| 1074 | $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2); |
| 1075 | $x_axis += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2); |
| 1076 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1077 | break; |
| 1078 | } |
| 1079 | |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame^] | 1080 | if ($this->wm_use_drop_shadow) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1081 | { |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame^] | 1082 | // Set RGB values for text and shadow |
| 1083 | $txt_color = str_split(substr($this->wm_font_color, 1, 6)); |
| 1084 | $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2])); |
| 1085 | $drp_color = str_split(substr($this->wm_shadow_color, 1, 6)); |
| 1086 | $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[2]), hexdec($drp_color[3])); |
| 1087 | |
| 1088 | // Add the text to the source image |
| 1089 | if ($this->wm_use_truetype) |
| 1090 | { |
| 1091 | imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text); |
| 1092 | imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text); |
| 1093 | } |
| 1094 | else |
| 1095 | { |
| 1096 | imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color); |
| 1097 | imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color); |
| 1098 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1101 | // Output the final image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1102 | if ($this->dynamic_output == TRUE) |
| 1103 | { |
| 1104 | $this->image_display_gd($src_img); |
| 1105 | } |
| 1106 | else |
| 1107 | { |
| 1108 | $this->image_save_gd($src_img); |
| 1109 | } |
| 1110 | |
| 1111 | imagedestroy($src_img); |
| 1112 | |
| 1113 | return TRUE; |
| 1114 | } |
| 1115 | |
| 1116 | // -------------------------------------------------------------------- |
| 1117 | |
| 1118 | /** |
| 1119 | * Create Image - GD |
| 1120 | * |
| 1121 | * This simply creates an image resource handle |
| 1122 | * based on the type of image being processed |
| 1123 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1124 | * @param string |
| 1125 | * @return resource |
| 1126 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1127 | public function image_create_gd($path = '', $image_type = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1128 | { |
| 1129 | if ($path == '') |
| 1130 | $path = $this->full_src_path; |
| 1131 | |
| 1132 | if ($image_type == '') |
| 1133 | $image_type = $this->image_type; |
| 1134 | |
| 1135 | |
| 1136 | switch ($image_type) |
| 1137 | { |
| 1138 | case 1 : |
| 1139 | if ( ! function_exists('imagecreatefromgif')) |
| 1140 | { |
| 1141 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported')); |
| 1142 | return FALSE; |
| 1143 | } |
| 1144 | |
| 1145 | return imagecreatefromgif($path); |
| 1146 | break; |
| 1147 | case 2 : |
| 1148 | if ( ! function_exists('imagecreatefromjpeg')) |
| 1149 | { |
| 1150 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported')); |
| 1151 | return FALSE; |
| 1152 | } |
| 1153 | |
| 1154 | return imagecreatefromjpeg($path); |
| 1155 | break; |
| 1156 | case 3 : |
| 1157 | if ( ! function_exists('imagecreatefrompng')) |
| 1158 | { |
| 1159 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported')); |
| 1160 | return FALSE; |
| 1161 | } |
| 1162 | |
| 1163 | return imagecreatefrompng($path); |
| 1164 | break; |
| 1165 | |
| 1166 | } |
| 1167 | |
| 1168 | $this->set_error(array('imglib_unsupported_imagecreate')); |
| 1169 | return FALSE; |
| 1170 | } |
| 1171 | |
| 1172 | // -------------------------------------------------------------------- |
| 1173 | |
| 1174 | /** |
| 1175 | * Write image file to disk - GD |
| 1176 | * |
| 1177 | * Takes an image resource as input and writes the file |
| 1178 | * to the specified destination |
| 1179 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1180 | * @param resource |
| 1181 | * @return bool |
| 1182 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1183 | public function image_save_gd($resource) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1184 | { |
| 1185 | switch ($this->image_type) |
| 1186 | { |
| 1187 | case 1 : |
| 1188 | if ( ! function_exists('imagegif')) |
| 1189 | { |
| 1190 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported')); |
| 1191 | return FALSE; |
| 1192 | } |
| 1193 | |
Derek Jones | 541ddbd | 2008-12-09 15:25:31 +0000 | [diff] [blame] | 1194 | if ( ! @imagegif($resource, $this->full_dst_path)) |
| 1195 | { |
| 1196 | $this->set_error('imglib_save_failed'); |
| 1197 | return FALSE; |
| 1198 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1199 | break; |
| 1200 | case 2 : |
| 1201 | if ( ! function_exists('imagejpeg')) |
| 1202 | { |
| 1203 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported')); |
| 1204 | return FALSE; |
| 1205 | } |
| 1206 | |
Derek Jones | 541ddbd | 2008-12-09 15:25:31 +0000 | [diff] [blame] | 1207 | if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality)) |
| 1208 | { |
| 1209 | $this->set_error('imglib_save_failed'); |
| 1210 | return FALSE; |
| 1211 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1212 | break; |
| 1213 | case 3 : |
| 1214 | if ( ! function_exists('imagepng')) |
| 1215 | { |
| 1216 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported')); |
| 1217 | return FALSE; |
| 1218 | } |
| 1219 | |
Derek Jones | 541ddbd | 2008-12-09 15:25:31 +0000 | [diff] [blame] | 1220 | if ( ! @imagepng($resource, $this->full_dst_path)) |
| 1221 | { |
| 1222 | $this->set_error('imglib_save_failed'); |
| 1223 | return FALSE; |
| 1224 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1225 | break; |
| 1226 | default : |
| 1227 | $this->set_error(array('imglib_unsupported_imagecreate')); |
| 1228 | return FALSE; |
| 1229 | break; |
| 1230 | } |
| 1231 | |
| 1232 | return TRUE; |
| 1233 | } |
| 1234 | |
| 1235 | // -------------------------------------------------------------------- |
| 1236 | |
| 1237 | /** |
| 1238 | * Dynamically outputs an image |
| 1239 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1240 | * @param resource |
| 1241 | * @return void |
| 1242 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1243 | public function image_display_gd($resource) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1244 | { |
| 1245 | header("Content-Disposition: filename={$this->source_image};"); |
| 1246 | header("Content-Type: {$this->mime_type}"); |
| 1247 | header('Content-Transfer-Encoding: binary'); |
| 1248 | header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT'); |
| 1249 | |
| 1250 | switch ($this->image_type) |
| 1251 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1252 | case 1 : imagegif($resource); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1253 | break; |
| 1254 | case 2 : imagejpeg($resource, '', $this->quality); |
| 1255 | break; |
| 1256 | case 3 : imagepng($resource); |
| 1257 | break; |
| 1258 | default : echo 'Unable to display the image'; |
| 1259 | break; |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | // -------------------------------------------------------------------- |
| 1264 | |
| 1265 | /** |
| 1266 | * Re-proportion Image Width/Height |
| 1267 | * |
| 1268 | * When creating thumbs, the desired width/height |
| 1269 | * can end up warping the image due to an incorrect |
| 1270 | * ratio between the full-sized image and the thumb. |
| 1271 | * |
| 1272 | * This function lets us re-proportion the width/height |
| 1273 | * if users choose to maintain the aspect ratio when resizing. |
| 1274 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1275 | * @return void |
| 1276 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1277 | public function image_reproportion() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1278 | { |
| 1279 | if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0) |
| 1280 | return; |
| 1281 | |
| 1282 | if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0) |
| 1283 | return; |
| 1284 | |
| 1285 | $new_width = ceil($this->orig_width*$this->height/$this->orig_height); |
| 1286 | $new_height = ceil($this->width*$this->orig_height/$this->orig_width); |
| 1287 | |
| 1288 | $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width)); |
| 1289 | |
| 1290 | if ($this->master_dim != 'width' AND $this->master_dim != 'height') |
| 1291 | { |
| 1292 | $this->master_dim = ($ratio < 0) ? 'width' : 'height'; |
| 1293 | } |
| 1294 | |
| 1295 | if (($this->width != $new_width) AND ($this->height != $new_height)) |
| 1296 | { |
| 1297 | if ($this->master_dim == 'height') |
| 1298 | { |
| 1299 | $this->width = $new_width; |
| 1300 | } |
| 1301 | else |
| 1302 | { |
| 1303 | $this->height = $new_height; |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | // -------------------------------------------------------------------- |
| 1309 | |
| 1310 | /** |
| 1311 | * Get image properties |
| 1312 | * |
| 1313 | * A helper function that gets info about the file |
| 1314 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1315 | * @param string |
| 1316 | * @return mixed |
| 1317 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1318 | public function get_image_properties($path = '', $return = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1319 | { |
| 1320 | // For now we require GD but we should |
| 1321 | // find a way to determine this using IM or NetPBM |
| 1322 | |
| 1323 | if ($path == '') |
| 1324 | $path = $this->full_src_path; |
| 1325 | |
| 1326 | if ( ! file_exists($path)) |
| 1327 | { |
| 1328 | $this->set_error('imglib_invalid_path'); |
| 1329 | return FALSE; |
| 1330 | } |
| 1331 | |
Phil Sturgeon | 901998a | 2011-08-26 10:03:33 +0100 | [diff] [blame] | 1332 | $vals = getimagesize($path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1333 | $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png'); |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1334 | $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1335 | |
| 1336 | if ($return == TRUE) |
| 1337 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1338 | return array( |
| 1339 | 'width' => $vals[0], |
| 1340 | 'height' => $vals[1], |
| 1341 | 'image_type' => $vals[2], |
| 1342 | 'size_str' => $vals[3], |
| 1343 | 'mime_type' => $mime |
| 1344 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1347 | $this->orig_width = $vals[0]; |
| 1348 | $this->orig_height = $vals[1]; |
| 1349 | $this->image_type = $vals[2]; |
| 1350 | $this->size_str = $vals[3]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1351 | $this->mime_type = $mime; |
| 1352 | |
| 1353 | return TRUE; |
| 1354 | } |
| 1355 | |
| 1356 | // -------------------------------------------------------------------- |
| 1357 | |
| 1358 | /** |
| 1359 | * Size calculator |
| 1360 | * |
| 1361 | * This function takes a known width x height and |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1362 | * recalculates it to a new size. Only one |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1363 | * new variable needs to be known |
| 1364 | * |
| 1365 | * $props = array( |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1366 | * 'width' => $width, |
| 1367 | * 'height' => $height, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1368 | * 'new_width' => 40, |
| 1369 | * 'new_height' => '' |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1370 | * ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1371 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1372 | * @param array |
| 1373 | * @return array |
| 1374 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1375 | public function size_calculator($vals) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1376 | { |
| 1377 | if ( ! is_array($vals)) |
| 1378 | { |
| 1379 | return; |
| 1380 | } |
| 1381 | |
| 1382 | $allowed = array('new_width', 'new_height', 'width', 'height'); |
| 1383 | |
| 1384 | foreach ($allowed as $item) |
| 1385 | { |
| 1386 | if ( ! isset($vals[$item]) OR $vals[$item] == '') |
| 1387 | $vals[$item] = 0; |
| 1388 | } |
| 1389 | |
| 1390 | if ($vals['width'] == 0 OR $vals['height'] == 0) |
| 1391 | { |
| 1392 | return $vals; |
| 1393 | } |
| 1394 | |
| 1395 | if ($vals['new_width'] == 0) |
| 1396 | { |
| 1397 | $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']); |
| 1398 | } |
| 1399 | elseif ($vals['new_height'] == 0) |
| 1400 | { |
| 1401 | $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']); |
| 1402 | } |
| 1403 | |
| 1404 | return $vals; |
| 1405 | } |
| 1406 | |
| 1407 | // -------------------------------------------------------------------- |
| 1408 | |
| 1409 | /** |
| 1410 | * Explode source_image |
| 1411 | * |
| 1412 | * This is a helper function that extracts the extension |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1413 | * from the source_image. This function lets us deal with |
| 1414 | * source_images with multiple periods, like: my.cool.jpg |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1415 | * It returns an associative array with two elements: |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1416 | * $array['ext'] = '.jpg'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1417 | * $array['name'] = 'my.cool'; |
| 1418 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1419 | * @param array |
| 1420 | * @return array |
| 1421 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1422 | public function explode_name($source_image) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1423 | { |
Derek Jones | 08cae63 | 2009-02-10 20:03:29 +0000 | [diff] [blame] | 1424 | $ext = strrchr($source_image, '.'); |
| 1425 | $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1426 | |
Derek Jones | 08cae63 | 2009-02-10 20:03:29 +0000 | [diff] [blame] | 1427 | return array('ext' => $ext, 'name' => $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | // -------------------------------------------------------------------- |
| 1431 | |
| 1432 | /** |
| 1433 | * Is GD Installed? |
| 1434 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1435 | * @return bool |
| 1436 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1437 | public function gd_loaded() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1438 | { |
| 1439 | if ( ! extension_loaded('gd')) |
| 1440 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1441 | /* As it is stated in the PHP manual, dl() is not always available |
| 1442 | * and even if so - it could generate an E_WARNING message on failure |
| 1443 | */ |
| 1444 | return (function_exists('dl') AND @dl('gd.so')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | return TRUE; |
| 1448 | } |
| 1449 | |
| 1450 | // -------------------------------------------------------------------- |
| 1451 | |
| 1452 | /** |
| 1453 | * Get GD version |
| 1454 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1455 | * @return mixed |
| 1456 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1457 | public function gd_version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1458 | { |
| 1459 | if (function_exists('gd_info')) |
| 1460 | { |
| 1461 | $gd_version = @gd_info(); |
| 1462 | $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']); |
| 1463 | |
| 1464 | return $gd_version; |
| 1465 | } |
| 1466 | |
| 1467 | return FALSE; |
| 1468 | } |
| 1469 | |
| 1470 | // -------------------------------------------------------------------- |
| 1471 | |
| 1472 | /** |
| 1473 | * Set error message |
| 1474 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1475 | * @param string |
| 1476 | * @return void |
| 1477 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1478 | public function set_error($msg) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1479 | { |
| 1480 | $CI =& get_instance(); |
| 1481 | $CI->lang->load('imglib'); |
| 1482 | |
| 1483 | if (is_array($msg)) |
| 1484 | { |
| 1485 | foreach ($msg as $val) |
| 1486 | { |
| 1487 | |
| 1488 | $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val); |
| 1489 | $this->error_msg[] = $msg; |
| 1490 | log_message('error', $msg); |
| 1491 | } |
| 1492 | } |
| 1493 | else |
| 1494 | { |
| 1495 | $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg); |
| 1496 | $this->error_msg[] = $msg; |
| 1497 | log_message('error', $msg); |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | // -------------------------------------------------------------------- |
| 1502 | |
| 1503 | /** |
| 1504 | * Show error messages |
| 1505 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1506 | * @param string |
| 1507 | * @return string |
| 1508 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1509 | public function display_errors($open = '<p>', $close = '</p>') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1510 | { |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1511 | 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] | 1512 | } |
| 1513 | |
| 1514 | } |
| 1515 | // END Image_lib Class |
| 1516 | |
| 1517 | /* End of file Image_lib.php */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1518 | /* Location: ./system/libraries/Image_lib.php */ |