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