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