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