Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://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 ExpressionEngine Dev Team |
| 25 | * @link http://codeigniter.com/user_guide/libraries/image_lib.html |
| 26 | */ |
| 27 | class CI_Image_lib { |
| 28 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 29 | var $image_library = 'gd2'; // Can be: imagemagick, netpbm, gd, gd2 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 39 | var $maintain_ratio = TRUE; // Whether to maintain aspect ratio when resizing or use hard values |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 57 | var $wm_vrt_offset = 0; // Lets you push text down |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 58 | var $wm_font_color = '#ffffff'; // Text color |
| 59 | var $wm_shadow_color = ''; // Dropshadow color |
| 60 | var $wm_shadow_distance = 2; // Dropshadow distance |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 61 | var $wm_opacity = 50; // Image opacity: 1 - 100 Only works with image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | // special consideration for master_dim |
| 116 | $this->master_dim = 'auto'; |
| 117 | } |
| 118 | |
| 119 | // -------------------------------------------------------------------- |
| 120 | |
| 121 | /** |
| 122 | * initialize image preferences |
| 123 | * |
| 124 | * @access public |
| 125 | * @param array |
| 126 | * @return bool |
| 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'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 150 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | */ |
| 161 | if ( ! function_exists('getimagesize')) |
| 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 |
| 191 | if ( ! $this->get_image_properties($this->source_folder.$this->source_image)) |
| 192 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 193 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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? |
| 229 | if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path)) |
| 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 | |
Derek Jones | 1322ec5 | 2009-03-11 17:01:14 +0000 | [diff] [blame] | 353 | if (preg_match('/gd2$/i', $protocol)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Derek Jones | 1322ec5 | 2009-03-11 17:01:14 +0000 | [diff] [blame] | 376 | if (preg_match('/gd2$/i', $protocol)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Derek Allard | d9c7f03 | 2008-12-01 20:18:00 +0000 | [diff] [blame] | 400 | if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | { |
| 402 | $this->set_error('imglib_rotation_angle_required'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 403 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 the target width/height match the source, AND if the new file name is not equal to the old file name |
| 453 | // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off. |
| 454 | if ($this->dynamic_output === FALSE) |
| 455 | { |
| 456 | if ($this->orig_width == $this->width AND $this->orig_height == $this->height) |
| 457 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 458 | if ($this->source_image != $this->new_image) |
| 459 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | if (@copy($this->full_src_path, $this->full_dst_path)) |
| 461 | { |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 462 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
| 466 | return TRUE; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | // Let's set up our values based on the action |
| 471 | if ($action == 'crop') |
| 472 | { |
| 473 | // Reassign the source width/height if cropping |
| 474 | $this->orig_width = $this->width; |
| 475 | $this->orig_height = $this->height; |
| 476 | |
| 477 | // GD 2.0 has a cropping bug so we'll test for it |
| 478 | if ($this->gd_version() !== FALSE) |
| 479 | { |
| 480 | $gd_version = str_replace('0', '', $this->gd_version()); |
| 481 | $v2_override = ($gd_version == 2) ? TRUE : FALSE; |
| 482 | } |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | // If resizing the x/y axis must be zero |
| 487 | $this->x_axis = 0; |
| 488 | $this->y_axis = 0; |
| 489 | } |
| 490 | |
| 491 | // Create the image handle |
| 492 | if ( ! ($src_img = $this->image_create_gd())) |
| 493 | { |
| 494 | return FALSE; |
| 495 | } |
| 496 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 497 | // Create The Image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | // |
| 499 | // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater" |
| 500 | // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment |
| 501 | // below should that ever prove inaccurate. |
| 502 | // |
| 503 | // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 504 | if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 505 | { |
| 506 | $create = 'imagecreatetruecolor'; |
| 507 | $copy = 'imagecopyresampled'; |
| 508 | } |
| 509 | else |
| 510 | { |
| 511 | $create = 'imagecreate'; |
| 512 | $copy = 'imagecopyresized'; |
| 513 | } |
| 514 | |
| 515 | $dst_img = $create($this->width, $this->height); |
Derek Jones | 595bfd1 | 2010-08-20 10:28:22 -0500 | [diff] [blame] | 516 | |
| 517 | if ($this->image_type == 3) // png we can actually preserve transparency |
| 518 | { |
| 519 | imagealphablending($dst_img, FALSE); |
| 520 | imagesavealpha($dst_img, TRUE); |
| 521 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 522 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 523 | $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height); |
| 524 | |
| 525 | // Show the image |
| 526 | if ($this->dynamic_output == TRUE) |
| 527 | { |
| 528 | $this->image_display_gd($dst_img); |
| 529 | } |
| 530 | else |
| 531 | { |
| 532 | // Or save it |
| 533 | if ( ! $this->image_save_gd($dst_img)) |
| 534 | { |
| 535 | return FALSE; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | // Kill the file handles |
| 540 | imagedestroy($dst_img); |
| 541 | imagedestroy($src_img); |
| 542 | |
| 543 | // Set the file to 777 |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 544 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 545 | |
| 546 | return TRUE; |
| 547 | } |
| 548 | |
| 549 | // -------------------------------------------------------------------- |
| 550 | |
| 551 | /** |
| 552 | * Image Process Using ImageMagick |
| 553 | * |
| 554 | * This function will resize, crop or rotate |
| 555 | * |
| 556 | * @access public |
| 557 | * @param string |
| 558 | * @return bool |
| 559 | */ |
| 560 | function image_process_imagemagick($action = 'resize') |
| 561 | { |
| 562 | // Do we have a vaild library path? |
| 563 | if ($this->library_path == '') |
| 564 | { |
| 565 | $this->set_error('imglib_libpath_invalid'); |
| 566 | return FALSE; |
| 567 | } |
| 568 | |
Derek Jones | 1322ec5 | 2009-03-11 17:01:14 +0000 | [diff] [blame] | 569 | if ( ! preg_match("/convert$/i", $this->library_path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 570 | { |
Derek Jones | 1322ec5 | 2009-03-11 17:01:14 +0000 | [diff] [blame] | 571 | $this->library_path = rtrim($this->library_path, '/').'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 572 | |
| 573 | $this->library_path .= 'convert'; |
| 574 | } |
| 575 | |
| 576 | // Execute the command |
| 577 | $cmd = $this->library_path." -quality ".$this->quality; |
| 578 | |
| 579 | if ($action == 'crop') |
| 580 | { |
| 581 | $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1"; |
| 582 | } |
| 583 | elseif ($action == 'rotate') |
| 584 | { |
| 585 | switch ($this->rotation_angle) |
| 586 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 587 | case 'hor' : $angle = '-flop'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 588 | break; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 589 | case 'vrt' : $angle = '-flip'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 590 | break; |
| 591 | default : $angle = '-rotate '.$this->rotation_angle; |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1"; |
| 596 | } |
| 597 | else // Resize |
| 598 | { |
| 599 | $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1"; |
| 600 | } |
| 601 | |
| 602 | $retval = 1; |
| 603 | |
| 604 | @exec($cmd, $output, $retval); |
| 605 | |
| 606 | // Did it work? |
| 607 | if ($retval > 0) |
| 608 | { |
| 609 | $this->set_error('imglib_image_process_failed'); |
| 610 | return FALSE; |
| 611 | } |
| 612 | |
| 613 | // Set the file to 777 |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 614 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 615 | |
| 616 | return TRUE; |
| 617 | } |
| 618 | |
| 619 | // -------------------------------------------------------------------- |
| 620 | |
| 621 | /** |
| 622 | * Image Process Using NetPBM |
| 623 | * |
| 624 | * This function will resize, crop or rotate |
| 625 | * |
| 626 | * @access public |
| 627 | * @param string |
| 628 | * @return bool |
| 629 | */ |
| 630 | function image_process_netpbm($action = 'resize') |
| 631 | { |
| 632 | if ($this->library_path == '') |
| 633 | { |
| 634 | $this->set_error('imglib_libpath_invalid'); |
| 635 | return FALSE; |
| 636 | } |
| 637 | |
| 638 | // Build the resizing command |
| 639 | switch ($this->image_type) |
| 640 | { |
| 641 | case 1 : |
| 642 | $cmd_in = 'giftopnm'; |
| 643 | $cmd_out = 'ppmtogif'; |
| 644 | break; |
| 645 | case 2 : |
| 646 | $cmd_in = 'jpegtopnm'; |
| 647 | $cmd_out = 'ppmtojpeg'; |
| 648 | break; |
| 649 | case 3 : |
| 650 | $cmd_in = 'pngtopnm'; |
| 651 | $cmd_out = 'ppmtopng'; |
| 652 | break; |
| 653 | } |
| 654 | |
| 655 | if ($action == 'crop') |
| 656 | { |
| 657 | $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height; |
| 658 | } |
| 659 | elseif ($action == 'rotate') |
| 660 | { |
| 661 | switch ($this->rotation_angle) |
| 662 | { |
| 663 | case 90 : $angle = 'r270'; |
| 664 | break; |
| 665 | case 180 : $angle = 'r180'; |
| 666 | break; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 667 | case 270 : $angle = 'r90'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 668 | break; |
| 669 | case 'vrt' : $angle = 'tb'; |
| 670 | break; |
| 671 | case 'hor' : $angle = 'lr'; |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | $cmd_inner = 'pnmflip -'.$angle.' '; |
| 676 | } |
| 677 | else // Resize |
| 678 | { |
| 679 | $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height; |
| 680 | } |
| 681 | |
| 682 | $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp'; |
| 683 | |
| 684 | $retval = 1; |
| 685 | |
| 686 | @exec($cmd, $output, $retval); |
| 687 | |
| 688 | // Did it work? |
| 689 | if ($retval > 0) |
| 690 | { |
| 691 | $this->set_error('imglib_image_process_failed'); |
| 692 | return FALSE; |
| 693 | } |
| 694 | |
| 695 | // With NetPBM we have to create a temporary image. |
| 696 | // If you try manipulating the original it fails so |
| 697 | // we have to rename the temp file. |
| 698 | copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path); |
| 699 | unlink ($this->dest_folder.'netpbm.tmp'); |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 700 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 701 | |
| 702 | return TRUE; |
| 703 | } |
| 704 | |
| 705 | // -------------------------------------------------------------------- |
| 706 | |
| 707 | /** |
| 708 | * Image Rotate Using GD |
| 709 | * |
| 710 | * @access public |
| 711 | * @return bool |
| 712 | */ |
| 713 | function image_rotate_gd() |
| 714 | { |
| 715 | // Is Image Rotation Supported? |
| 716 | // this function is only supported as of PHP 4.3 |
| 717 | if ( ! function_exists('imagerotate')) |
| 718 | { |
| 719 | $this->set_error('imglib_rotate_unsupported'); |
| 720 | return FALSE; |
| 721 | } |
| 722 | |
| 723 | // Create the image handle |
| 724 | if ( ! ($src_img = $this->image_create_gd())) |
| 725 | { |
| 726 | return FALSE; |
| 727 | } |
| 728 | |
| 729 | // Set the background color |
| 730 | // This won't work with transparent PNG files so we are |
| 731 | // going to have to figure out how to determine the color |
| 732 | // of the alpha channel in a future release. |
| 733 | |
| 734 | $white = imagecolorallocate($src_img, 255, 255, 255); |
| 735 | |
| 736 | // Rotate it! |
| 737 | $dst_img = imagerotate($src_img, $this->rotation_angle, $white); |
| 738 | |
| 739 | // Save the Image |
| 740 | if ($this->dynamic_output == TRUE) |
| 741 | { |
| 742 | $this->image_display_gd($dst_img); |
| 743 | } |
| 744 | else |
| 745 | { |
| 746 | // Or save it |
| 747 | if ( ! $this->image_save_gd($dst_img)) |
| 748 | { |
| 749 | return FALSE; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | // Kill the file handles |
| 754 | imagedestroy($dst_img); |
| 755 | imagedestroy($src_img); |
| 756 | |
| 757 | // Set the file to 777 |
| 758 | |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 759 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 760 | |
| 761 | return true; |
| 762 | } |
| 763 | |
| 764 | // -------------------------------------------------------------------- |
| 765 | |
| 766 | /** |
| 767 | * Create Mirror Image using GD |
| 768 | * |
| 769 | * This function will flip horizontal or vertical |
| 770 | * |
| 771 | * @access public |
| 772 | * @return bool |
| 773 | */ |
| 774 | function image_mirror_gd() |
| 775 | { |
| 776 | if ( ! $src_img = $this->image_create_gd()) |
| 777 | { |
| 778 | return FALSE; |
| 779 | } |
| 780 | |
| 781 | $width = $this->orig_width; |
| 782 | $height = $this->orig_height; |
| 783 | |
| 784 | if ($this->rotation_angle == 'hor') |
| 785 | { |
| 786 | for ($i = 0; $i < $height; $i++) |
| 787 | { |
| 788 | $left = 0; |
| 789 | $right = $width-1; |
| 790 | |
| 791 | while ($left < $right) |
| 792 | { |
| 793 | $cl = imagecolorat($src_img, $left, $i); |
| 794 | $cr = imagecolorat($src_img, $right, $i); |
| 795 | |
| 796 | imagesetpixel($src_img, $left, $i, $cr); |
| 797 | imagesetpixel($src_img, $right, $i, $cl); |
| 798 | |
| 799 | $left++; |
| 800 | $right--; |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | else |
| 805 | { |
| 806 | for ($i = 0; $i < $width; $i++) |
| 807 | { |
| 808 | $top = 0; |
| 809 | $bot = $height-1; |
| 810 | |
| 811 | while ($top < $bot) |
| 812 | { |
| 813 | $ct = imagecolorat($src_img, $i, $top); |
| 814 | $cb = imagecolorat($src_img, $i, $bot); |
| 815 | |
| 816 | imagesetpixel($src_img, $i, $top, $cb); |
| 817 | imagesetpixel($src_img, $i, $bot, $ct); |
| 818 | |
| 819 | $top++; |
| 820 | $bot--; |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | // Show the image |
| 826 | if ($this->dynamic_output == TRUE) |
| 827 | { |
| 828 | $this->image_display_gd($src_img); |
| 829 | } |
| 830 | else |
| 831 | { |
| 832 | // Or save it |
| 833 | if ( ! $this->image_save_gd($src_img)) |
| 834 | { |
| 835 | return FALSE; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | // Kill the file handles |
| 840 | imagedestroy($src_img); |
| 841 | |
| 842 | // Set the file to 777 |
Derek Jones | 172e161 | 2009-10-13 14:32:48 +0000 | [diff] [blame] | 843 | @chmod($this->full_dst_path, FILE_WRITE_MODE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 844 | |
| 845 | return TRUE; |
| 846 | } |
| 847 | |
| 848 | // -------------------------------------------------------------------- |
| 849 | |
| 850 | /** |
| 851 | * Image Watermark |
| 852 | * |
| 853 | * This is a wrapper function that chooses the type |
| 854 | * of watermarking based on the specified preference. |
| 855 | * |
| 856 | * @access public |
| 857 | * @param string |
| 858 | * @return bool |
| 859 | */ |
| 860 | function watermark() |
| 861 | { |
| 862 | if ($this->wm_type == 'overlay') |
| 863 | { |
| 864 | return $this->overlay_watermark(); |
| 865 | } |
| 866 | else |
| 867 | { |
| 868 | return $this->text_watermark(); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | // -------------------------------------------------------------------- |
| 873 | |
| 874 | /** |
| 875 | * Watermark - Graphic Version |
| 876 | * |
| 877 | * @access public |
| 878 | * @return bool |
| 879 | */ |
| 880 | function overlay_watermark() |
| 881 | { |
| 882 | if ( ! function_exists('imagecolortransparent')) |
| 883 | { |
| 884 | $this->set_error('imglib_gd_required'); |
| 885 | return FALSE; |
| 886 | } |
| 887 | |
| 888 | // Fetch source image properties |
| 889 | $this->get_image_properties(); |
| 890 | |
| 891 | // Fetch watermark image properties |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 892 | $props = $this->get_image_properties($this->wm_overlay_path, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 893 | $wm_img_type = $props['image_type']; |
| 894 | $wm_width = $props['width']; |
| 895 | $wm_height = $props['height']; |
| 896 | |
| 897 | // Create two image resources |
| 898 | $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type); |
| 899 | $src_img = $this->image_create_gd($this->full_src_path); |
| 900 | |
| 901 | // Reverse the offset if necessary |
| 902 | // When the image is positioned at the bottom |
| 903 | // we don't want the vertical offset to push it |
| 904 | // further down. We want the reverse, so we'll |
| 905 | // invert the offset. Same with the horizontal |
| 906 | // offset when the image is at the right |
| 907 | |
| 908 | $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1)); |
| 909 | $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1)); |
| 910 | |
| 911 | if ($this->wm_vrt_alignment == 'B') |
| 912 | $this->wm_vrt_offset = $this->wm_vrt_offset * -1; |
| 913 | |
| 914 | if ($this->wm_hor_alignment == 'R') |
| 915 | $this->wm_hor_offset = $this->wm_hor_offset * -1; |
| 916 | |
| 917 | // Set the base x and y axis values |
| 918 | $x_axis = $this->wm_hor_offset + $this->wm_padding; |
| 919 | $y_axis = $this->wm_vrt_offset + $this->wm_padding; |
| 920 | |
| 921 | // Set the vertical position |
| 922 | switch ($this->wm_vrt_alignment) |
| 923 | { |
| 924 | case 'T': |
| 925 | break; |
| 926 | case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2); |
| 927 | break; |
| 928 | case 'B': $y_axis += $this->orig_height - $wm_height; |
| 929 | break; |
| 930 | } |
| 931 | |
| 932 | // Set the horizontal position |
| 933 | switch ($this->wm_hor_alignment) |
| 934 | { |
| 935 | case 'L': |
| 936 | break; |
| 937 | case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2); |
| 938 | break; |
| 939 | case 'R': $x_axis += $this->orig_width - $wm_width; |
| 940 | break; |
| 941 | } |
| 942 | |
| 943 | // Build the finalized image |
| 944 | if ($wm_img_type == 3 AND function_exists('imagealphablending')) |
| 945 | { |
| 946 | @imagealphablending($src_img, TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 947 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 948 | |
| 949 | // Set RGB values for text and shadow |
| 950 | $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp); |
| 951 | $alpha = ($rgba & 0x7F000000) >> 24; |
| 952 | |
| 953 | // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency |
| 954 | if ($alpha > 0) |
| 955 | { |
| 956 | // copy the image directly, the image's alpha transparency being the sole determinant of blending |
| 957 | imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height); |
| 958 | } |
| 959 | else |
| 960 | { |
| 961 | // set our RGB value from above to be transparent and merge the images with the specified opacity |
| 962 | imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp)); |
| 963 | imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity); |
| 964 | } |
| 965 | |
| 966 | // Output the image |
| 967 | if ($this->dynamic_output == TRUE) |
| 968 | { |
| 969 | $this->image_display_gd($src_img); |
| 970 | } |
| 971 | else |
| 972 | { |
| 973 | if ( ! $this->image_save_gd($src_img)) |
| 974 | { |
| 975 | return FALSE; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | imagedestroy($src_img); |
| 980 | imagedestroy($wm_img); |
| 981 | |
| 982 | return TRUE; |
| 983 | } |
| 984 | |
| 985 | // -------------------------------------------------------------------- |
| 986 | |
| 987 | /** |
| 988 | * Watermark - Text Version |
| 989 | * |
| 990 | * @access public |
| 991 | * @return bool |
| 992 | */ |
| 993 | function text_watermark() |
| 994 | { |
| 995 | if ( ! ($src_img = $this->image_create_gd())) |
| 996 | { |
| 997 | return FALSE; |
| 998 | } |
| 999 | |
| 1000 | if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path)) |
| 1001 | { |
| 1002 | $this->set_error('imglib_missing_font'); |
| 1003 | return FALSE; |
| 1004 | } |
| 1005 | |
| 1006 | // Fetch source image properties |
| 1007 | $this->get_image_properties(); |
| 1008 | |
| 1009 | // Set RGB values for text and shadow |
| 1010 | $this->wm_font_color = str_replace('#', '', $this->wm_font_color); |
| 1011 | $this->wm_shadow_color = str_replace('#', '', $this->wm_shadow_color); |
| 1012 | |
| 1013 | $R1 = hexdec(substr($this->wm_font_color, 0, 2)); |
| 1014 | $G1 = hexdec(substr($this->wm_font_color, 2, 2)); |
| 1015 | $B1 = hexdec(substr($this->wm_font_color, 4, 2)); |
| 1016 | |
| 1017 | $R2 = hexdec(substr($this->wm_shadow_color, 0, 2)); |
| 1018 | $G2 = hexdec(substr($this->wm_shadow_color, 2, 2)); |
| 1019 | $B2 = hexdec(substr($this->wm_shadow_color, 4, 2)); |
| 1020 | |
| 1021 | $txt_color = imagecolorclosest($src_img, $R1, $G1, $B1); |
| 1022 | $drp_color = imagecolorclosest($src_img, $R2, $G2, $B2); |
| 1023 | |
| 1024 | // Reverse the vertical offset |
| 1025 | // When the image is positioned at the bottom |
| 1026 | // we don't want the vertical offset to push it |
| 1027 | // further down. We want the reverse, so we'll |
| 1028 | // invert the offset. Note: The horizontal |
| 1029 | // offset flips itself automatically |
| 1030 | |
| 1031 | if ($this->wm_vrt_alignment == 'B') |
| 1032 | $this->wm_vrt_offset = $this->wm_vrt_offset * -1; |
| 1033 | |
| 1034 | if ($this->wm_hor_alignment == 'R') |
| 1035 | $this->wm_hor_offset = $this->wm_hor_offset * -1; |
| 1036 | |
| 1037 | // Set font width and height |
| 1038 | // These are calculated differently depending on |
| 1039 | // whether we are using the true type font or not |
| 1040 | if ($this->wm_use_truetype == TRUE) |
| 1041 | { |
| 1042 | if ($this->wm_font_size == '') |
| 1043 | $this->wm_font_size = '17'; |
| 1044 | |
| 1045 | $fontwidth = $this->wm_font_size-($this->wm_font_size/4); |
| 1046 | $fontheight = $this->wm_font_size; |
| 1047 | $this->wm_vrt_offset += $this->wm_font_size; |
| 1048 | } |
| 1049 | else |
| 1050 | { |
| 1051 | $fontwidth = imagefontwidth($this->wm_font_size); |
| 1052 | $fontheight = imagefontheight($this->wm_font_size); |
| 1053 | } |
| 1054 | |
| 1055 | // Set base X and Y axis values |
| 1056 | $x_axis = $this->wm_hor_offset + $this->wm_padding; |
| 1057 | $y_axis = $this->wm_vrt_offset + $this->wm_padding; |
| 1058 | |
| 1059 | // Set verticle alignment |
| 1060 | if ($this->wm_use_drop_shadow == FALSE) |
| 1061 | $this->wm_shadow_distance = 0; |
| 1062 | |
| 1063 | $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1)); |
| 1064 | $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1)); |
| 1065 | |
| 1066 | switch ($this->wm_vrt_alignment) |
| 1067 | { |
| 1068 | case "T" : |
| 1069 | break; |
| 1070 | case "M": $y_axis += ($this->orig_height/2)+($fontheight/2); |
| 1071 | break; |
| 1072 | case "B": $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2)); |
| 1073 | break; |
| 1074 | } |
| 1075 | |
| 1076 | $x_shad = $x_axis + $this->wm_shadow_distance; |
| 1077 | $y_shad = $y_axis + $this->wm_shadow_distance; |
| 1078 | |
| 1079 | // Set horizontal alignment |
| 1080 | switch ($this->wm_hor_alignment) |
| 1081 | { |
| 1082 | case "L": |
| 1083 | break; |
| 1084 | case "R": |
| 1085 | if ($this->wm_use_drop_shadow) |
| 1086 | $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text)); |
| 1087 | $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text)); |
| 1088 | break; |
| 1089 | case "C": |
| 1090 | if ($this->wm_use_drop_shadow) |
| 1091 | $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2); |
| 1092 | $x_axis += floor(($this->orig_width -$fontwidth*strlen($this->wm_text))/2); |
| 1093 | break; |
| 1094 | } |
| 1095 | |
| 1096 | // Add the text to the source image |
| 1097 | if ($this->wm_use_truetype) |
| 1098 | { |
| 1099 | if ($this->wm_use_drop_shadow) |
| 1100 | imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text); |
| 1101 | imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text); |
| 1102 | } |
| 1103 | else |
| 1104 | { |
| 1105 | if ($this->wm_use_drop_shadow) |
| 1106 | imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color); |
| 1107 | imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color); |
| 1108 | } |
| 1109 | |
| 1110 | // Output the final image |
| 1111 | if ($this->dynamic_output == TRUE) |
| 1112 | { |
| 1113 | $this->image_display_gd($src_img); |
| 1114 | } |
| 1115 | else |
| 1116 | { |
| 1117 | $this->image_save_gd($src_img); |
| 1118 | } |
| 1119 | |
| 1120 | imagedestroy($src_img); |
| 1121 | |
| 1122 | return TRUE; |
| 1123 | } |
| 1124 | |
| 1125 | // -------------------------------------------------------------------- |
| 1126 | |
| 1127 | /** |
| 1128 | * Create Image - GD |
| 1129 | * |
| 1130 | * This simply creates an image resource handle |
| 1131 | * based on the type of image being processed |
| 1132 | * |
| 1133 | * @access public |
| 1134 | * @param string |
| 1135 | * @return resource |
| 1136 | */ |
| 1137 | function image_create_gd($path = '', $image_type = '') |
| 1138 | { |
| 1139 | if ($path == '') |
| 1140 | $path = $this->full_src_path; |
| 1141 | |
| 1142 | if ($image_type == '') |
| 1143 | $image_type = $this->image_type; |
| 1144 | |
| 1145 | |
| 1146 | switch ($image_type) |
| 1147 | { |
| 1148 | case 1 : |
| 1149 | if ( ! function_exists('imagecreatefromgif')) |
| 1150 | { |
| 1151 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported')); |
| 1152 | return FALSE; |
| 1153 | } |
| 1154 | |
| 1155 | return imagecreatefromgif($path); |
| 1156 | break; |
| 1157 | case 2 : |
| 1158 | if ( ! function_exists('imagecreatefromjpeg')) |
| 1159 | { |
| 1160 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported')); |
| 1161 | return FALSE; |
| 1162 | } |
| 1163 | |
| 1164 | return imagecreatefromjpeg($path); |
| 1165 | break; |
| 1166 | case 3 : |
| 1167 | if ( ! function_exists('imagecreatefrompng')) |
| 1168 | { |
| 1169 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported')); |
| 1170 | return FALSE; |
| 1171 | } |
| 1172 | |
| 1173 | return imagecreatefrompng($path); |
| 1174 | break; |
| 1175 | |
| 1176 | } |
| 1177 | |
| 1178 | $this->set_error(array('imglib_unsupported_imagecreate')); |
| 1179 | return FALSE; |
| 1180 | } |
| 1181 | |
| 1182 | // -------------------------------------------------------------------- |
| 1183 | |
| 1184 | /** |
| 1185 | * Write image file to disk - GD |
| 1186 | * |
| 1187 | * Takes an image resource as input and writes the file |
| 1188 | * to the specified destination |
| 1189 | * |
| 1190 | * @access public |
| 1191 | * @param resource |
| 1192 | * @return bool |
| 1193 | */ |
| 1194 | function image_save_gd($resource) |
| 1195 | { |
| 1196 | switch ($this->image_type) |
| 1197 | { |
| 1198 | case 1 : |
| 1199 | if ( ! function_exists('imagegif')) |
| 1200 | { |
| 1201 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported')); |
| 1202 | return FALSE; |
| 1203 | } |
| 1204 | |
Derek Jones | 541ddbd | 2008-12-09 15:25:31 +0000 | [diff] [blame] | 1205 | if ( ! @imagegif($resource, $this->full_dst_path)) |
| 1206 | { |
| 1207 | $this->set_error('imglib_save_failed'); |
| 1208 | return FALSE; |
| 1209 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1210 | break; |
| 1211 | case 2 : |
| 1212 | if ( ! function_exists('imagejpeg')) |
| 1213 | { |
| 1214 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported')); |
| 1215 | return FALSE; |
| 1216 | } |
| 1217 | |
| 1218 | if (phpversion() == '4.4.1') |
| 1219 | { |
| 1220 | @touch($this->full_dst_path); // PHP 4.4.1 bug #35060 - workaround |
| 1221 | } |
| 1222 | |
Derek Jones | 541ddbd | 2008-12-09 15:25:31 +0000 | [diff] [blame] | 1223 | if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality)) |
| 1224 | { |
| 1225 | $this->set_error('imglib_save_failed'); |
| 1226 | return FALSE; |
| 1227 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1228 | break; |
| 1229 | case 3 : |
| 1230 | if ( ! function_exists('imagepng')) |
| 1231 | { |
| 1232 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported')); |
| 1233 | return FALSE; |
| 1234 | } |
| 1235 | |
Derek Jones | 541ddbd | 2008-12-09 15:25:31 +0000 | [diff] [blame] | 1236 | if ( ! @imagepng($resource, $this->full_dst_path)) |
| 1237 | { |
| 1238 | $this->set_error('imglib_save_failed'); |
| 1239 | return FALSE; |
| 1240 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1241 | break; |
| 1242 | default : |
| 1243 | $this->set_error(array('imglib_unsupported_imagecreate')); |
| 1244 | return FALSE; |
| 1245 | break; |
| 1246 | } |
| 1247 | |
| 1248 | return TRUE; |
| 1249 | } |
| 1250 | |
| 1251 | // -------------------------------------------------------------------- |
| 1252 | |
| 1253 | /** |
| 1254 | * Dynamically outputs an image |
| 1255 | * |
| 1256 | * @access public |
| 1257 | * @param resource |
| 1258 | * @return void |
| 1259 | */ |
| 1260 | function image_display_gd($resource) |
| 1261 | { |
| 1262 | header("Content-Disposition: filename={$this->source_image};"); |
| 1263 | header("Content-Type: {$this->mime_type}"); |
| 1264 | header('Content-Transfer-Encoding: binary'); |
| 1265 | header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT'); |
| 1266 | |
| 1267 | switch ($this->image_type) |
| 1268 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 1269 | case 1 : imagegif($resource); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1270 | break; |
| 1271 | case 2 : imagejpeg($resource, '', $this->quality); |
| 1272 | break; |
| 1273 | case 3 : imagepng($resource); |
| 1274 | break; |
| 1275 | default : echo 'Unable to display the image'; |
| 1276 | break; |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | // -------------------------------------------------------------------- |
| 1281 | |
| 1282 | /** |
| 1283 | * Re-proportion Image Width/Height |
| 1284 | * |
| 1285 | * When creating thumbs, the desired width/height |
| 1286 | * can end up warping the image due to an incorrect |
| 1287 | * ratio between the full-sized image and the thumb. |
| 1288 | * |
| 1289 | * This function lets us re-proportion the width/height |
| 1290 | * if users choose to maintain the aspect ratio when resizing. |
| 1291 | * |
| 1292 | * @access public |
| 1293 | * @return void |
| 1294 | */ |
| 1295 | function image_reproportion() |
| 1296 | { |
| 1297 | if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0) |
| 1298 | return; |
| 1299 | |
| 1300 | if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0) |
| 1301 | return; |
| 1302 | |
| 1303 | $new_width = ceil($this->orig_width*$this->height/$this->orig_height); |
| 1304 | $new_height = ceil($this->width*$this->orig_height/$this->orig_width); |
| 1305 | |
| 1306 | $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width)); |
| 1307 | |
| 1308 | if ($this->master_dim != 'width' AND $this->master_dim != 'height') |
| 1309 | { |
| 1310 | $this->master_dim = ($ratio < 0) ? 'width' : 'height'; |
| 1311 | } |
| 1312 | |
| 1313 | if (($this->width != $new_width) AND ($this->height != $new_height)) |
| 1314 | { |
| 1315 | if ($this->master_dim == 'height') |
| 1316 | { |
| 1317 | $this->width = $new_width; |
| 1318 | } |
| 1319 | else |
| 1320 | { |
| 1321 | $this->height = $new_height; |
| 1322 | } |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | // -------------------------------------------------------------------- |
| 1327 | |
| 1328 | /** |
| 1329 | * Get image properties |
| 1330 | * |
| 1331 | * A helper function that gets info about the file |
| 1332 | * |
| 1333 | * @access public |
| 1334 | * @param string |
| 1335 | * @return mixed |
| 1336 | */ |
| 1337 | function get_image_properties($path = '', $return = FALSE) |
| 1338 | { |
| 1339 | // For now we require GD but we should |
| 1340 | // find a way to determine this using IM or NetPBM |
| 1341 | |
| 1342 | if ($path == '') |
| 1343 | $path = $this->full_src_path; |
| 1344 | |
| 1345 | if ( ! file_exists($path)) |
| 1346 | { |
| 1347 | $this->set_error('imglib_invalid_path'); |
| 1348 | return FALSE; |
| 1349 | } |
| 1350 | |
| 1351 | $vals = @getimagesize($path); |
| 1352 | |
| 1353 | $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png'); |
| 1354 | |
| 1355 | $mime = (isset($types[$vals['2']])) ? 'image/'.$types[$vals['2']] : 'image/jpg'; |
| 1356 | |
| 1357 | if ($return == TRUE) |
| 1358 | { |
| 1359 | $v['width'] = $vals['0']; |
| 1360 | $v['height'] = $vals['1']; |
| 1361 | $v['image_type'] = $vals['2']; |
| 1362 | $v['size_str'] = $vals['3']; |
| 1363 | $v['mime_type'] = $mime; |
| 1364 | |
| 1365 | return $v; |
| 1366 | } |
| 1367 | |
| 1368 | $this->orig_width = $vals['0']; |
| 1369 | $this->orig_height = $vals['1']; |
| 1370 | $this->image_type = $vals['2']; |
| 1371 | $this->size_str = $vals['3']; |
| 1372 | $this->mime_type = $mime; |
| 1373 | |
| 1374 | return TRUE; |
| 1375 | } |
| 1376 | |
| 1377 | // -------------------------------------------------------------------- |
| 1378 | |
| 1379 | /** |
| 1380 | * Size calculator |
| 1381 | * |
| 1382 | * This function takes a known width x height and |
| 1383 | * recalculates it to a new size. Only one |
| 1384 | * new variable needs to be known |
| 1385 | * |
| 1386 | * $props = array( |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 1387 | * 'width' => $width, |
| 1388 | * 'height' => $height, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1389 | * 'new_width' => 40, |
| 1390 | * 'new_height' => '' |
| 1391 | * ); |
| 1392 | * |
| 1393 | * @access public |
| 1394 | * @param array |
| 1395 | * @return array |
| 1396 | */ |
| 1397 | function size_calculator($vals) |
| 1398 | { |
| 1399 | if ( ! is_array($vals)) |
| 1400 | { |
| 1401 | return; |
| 1402 | } |
| 1403 | |
| 1404 | $allowed = array('new_width', 'new_height', 'width', 'height'); |
| 1405 | |
| 1406 | foreach ($allowed as $item) |
| 1407 | { |
| 1408 | if ( ! isset($vals[$item]) OR $vals[$item] == '') |
| 1409 | $vals[$item] = 0; |
| 1410 | } |
| 1411 | |
| 1412 | if ($vals['width'] == 0 OR $vals['height'] == 0) |
| 1413 | { |
| 1414 | return $vals; |
| 1415 | } |
| 1416 | |
| 1417 | if ($vals['new_width'] == 0) |
| 1418 | { |
| 1419 | $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']); |
| 1420 | } |
| 1421 | elseif ($vals['new_height'] == 0) |
| 1422 | { |
| 1423 | $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']); |
| 1424 | } |
| 1425 | |
| 1426 | return $vals; |
| 1427 | } |
| 1428 | |
| 1429 | // -------------------------------------------------------------------- |
| 1430 | |
| 1431 | /** |
| 1432 | * Explode source_image |
| 1433 | * |
| 1434 | * This is a helper function that extracts the extension |
| 1435 | * from the source_image. This function lets us deal with |
| 1436 | * source_images with multiple periods, like: my.cool.jpg |
| 1437 | * It returns an associative array with two elements: |
| 1438 | * $array['ext'] = '.jpg'; |
| 1439 | * $array['name'] = 'my.cool'; |
| 1440 | * |
| 1441 | * @access public |
| 1442 | * @param array |
| 1443 | * @return array |
| 1444 | */ |
| 1445 | function explode_name($source_image) |
| 1446 | { |
Derek Jones | 08cae63 | 2009-02-10 20:03:29 +0000 | [diff] [blame] | 1447 | $ext = strrchr($source_image, '.'); |
| 1448 | $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 1449 | |
Derek Jones | 08cae63 | 2009-02-10 20:03:29 +0000 | [diff] [blame] | 1450 | return array('ext' => $ext, 'name' => $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | // -------------------------------------------------------------------- |
| 1454 | |
| 1455 | /** |
| 1456 | * Is GD Installed? |
| 1457 | * |
| 1458 | * @access public |
| 1459 | * @return bool |
| 1460 | */ |
| 1461 | function gd_loaded() |
| 1462 | { |
| 1463 | if ( ! extension_loaded('gd')) |
| 1464 | { |
| 1465 | if ( ! dl('gd.so')) |
| 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 |
| 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 */ |