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