| [ PHPXref.com ] | [ Generated: Sun Jul 20 17:05:41 2008 ] | [ Coppermine 1.4.5 ] |
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
1 <?php 2 /************************* 3 Coppermine Photo Gallery 4 ************************ 5 Copyright (c) 2003-2006 Coppermine Dev Team 6 v1.1 originally written by Gregory DEMAR 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2 of the License, or 11 (at your option) any later version. 12 ******************************************** 13 Coppermine version: 1.4.5 14 $Source: /cvsroot/coppermine/stable/image_processor.php,v $ 15 $Revision: 1.13 $ 16 $Author: gaugau $ 17 $Date: 2006/03/02 08:17:40 $ 18 **********************************************/ 19 20 // To use this module, one must place a transitory directory in the primary 21 // CPG folder. Then one must change the form action of upload.php to this 22 // script instead of db_imput. 23 24 // Let all the libraries know that this program is part of Coppermine. 25 define('IN_COPPERMINE', true); 26 27 // Let the libraries know this is the image_processor_php file. 28 define('IMAGE_PROCESSOR_PHP', true); 29 30 // Require include/init.inc.php, so we may have 31 // access to Coppermine's configuration information. 32 33 require ('include/init.inc.php'); 34 35 // Declare global variables. 36 global $CONFIG, $lang_image_processor_php; 37 38 39 //-----------------------------FUNCTION BLOCK--------------------------------- 40 41 function write_to_disk($image_type, $final, $path) 42 { 43 global $lang_image_processor_php; 44 if ($image_type == "1") { 45 imagegif($final, $path) or die($lang_image_processor_php['no_write']); 46 } elseif ($image_type == "2") { 47 imagejpeg($final, $path, 100) or die($lang_image_processor_php['no_write']); 48 } elseif ($image_type == "3") { 49 imagepng($final, $path) or die($lang_image_processor_php['no_write']); 50 } 51 imagedestroy($final); 52 } 53 54 function rotate_image($path_to_primary_image, $degrees) { 55 56 // This function takes $path_to_primary_image and $degrees 57 //as arguments. It returns nothing. 58 59 //Globalize appropriate variables. 60 global $CONFIG, $lang_image_processor_php; 61 62 // The $method varaible should contain gd1, gd2, 63 // or im, which indicates GD Library v1, GD Library v2, 64 // and ImageMagick respectively. We will use a switch 65 // statement to control the program flow based on the 66 // value of the $method variable. 67 68 $method = $CONFIG['thumb_method']; 69 70 // Detect if user has the function imageistruecolor(). This function is 71 // available in GD2 in PHP 4.3.2 and up. It allows GD to distinguish between 72 // palette images and true color images, which allows GD to make the appropriate 73 // canvas. 74 75 if (($method == 'gd2') and (!function_exists('imageistruecolor'))) { 76 77 //Set ignore imageistruecolor to false. 78 $ignore = 0; 79 } else { 80 //Set $ignore imageistruecolor to true. 81 $ignore = 1; 82 } 83 84 switch ($method) { 85 86 case "im" : 87 88 $real_path_to_primary_image = realpath($path_to_primary_image); 89 90 $output = array(); 91 92 // Set IM path. 93 $im_path = $CONFIG['impath']; 94 95 //Check the IM path for the final slash. 96 if (eregi('/$',$im_path) or empty($im_path)) { 97 $trailing_slash = ""; 98 } else { 99 $trailing_slash = "/"; 100 } 101 102 //Select degree measure. 103 104 if (($degrees == 90) or ($degrees == 180) or ($degrees == 270)) { 105 106 //Form the command to rotate the image. 107 $cmd = "{$CONFIG['impath']}".$trailing_slash."mogrify -quality \"100\" -rotate \"$degrees\" \"$real_path_to_primary_image\""; 108 109 } 110 111 exec ($cmd, $output, $retval); 112 113 if ($retval) { 114 $ERROR = $lang_image_processor_php['IM_Error'] . $retval; 115 if ($CONFIG['debug_mode']) { 116 // Re-execute the command with the backtick operator in order to get all outputs 117 // will not work is safe mode is enabled 118 $output = `$cmd 2>&1`; 119 $ERROR .= "<br /><br /><div align=\"left\">{$lang_image_processor_php['cmd_line']}<br /><span style=\"font-size:120%\">".nl2br(htmlspecialchars($cmd))."</span></div>"; 120 $ERROR .= "<br /><br /><div align=\"left\">{$lang_image_processor_php['mog_said']}<br /><span style=\"font-size:120%\">"; 121 $ERROR .= nl2br(htmlspecialchars($output)); 122 $ERROR .= "</span></div>"; 123 } 124 die($ERROR); 125 126 } 127 128 break; 129 130 case "gd2" : 131 132 if ($ignore) { 133 134 $image_handle = get_handle($path_to_primary_image); 135 if (imageistruecolor($image_handle)) { 136 imagedestroy($image_handle); 137 true_color_rotate($path_to_primary_image, $degrees); 138 } else { 139 imagedestroy($image_handle); 140 palette_rotate($path_to_primary_image, $degrees); 141 } 142 } else { 143 true_color_rotate($path_to_primary_image, $degrees); 144 } 145 146 break; 147 148 case "gd1" : 149 150 palette_rotate($path_to_primary_image, $degrees); 151 152 break; 153 154 } 155 156 } 157 158 //********************************************************************************************* 159 160 function true_color_rotate($path_to_primary_image, $degrees) { 161 162 global $lang_image_processor_php; 163 164 // Get image info. 165 $source_image_size_and_type = getimagesize($path_to_primary_image) or die($lang_image_processor_php['file_corrupt']); 166 $source_image_width = $source_image_size_and_type[0]; 167 $source_image_height = $source_image_size_and_type[1]; 168 $source_image_type = $source_image_size_and_type[2]; 169 170 //Set new canvas size. 171 if ( $source_image_width > $source_image_height ) { 172 $new_maximum_dimension = $source_image_width; 173 } else { 174 $new_maximum_dimension = $source_image_height; 175 } 176 177 178 $image_handle = get_handle($path_to_primary_image); 179 180 if ($degrees == "90") { 181 $destination_image_handle = ImageCreateTrueColor($new_maximum_dimension,$new_maximum_dimension); 182 imagecopy($destination_image_handle,$image_handle,0,0,0,0,$source_image_width,$source_image_height); 183 imagedestroy($image_handle); 184 $rotated_image_handle = imagerotate($destination_image_handle,$degrees,0); 185 $final_image_handle = ImageCreateTrueColor($source_image_height,$source_image_width); 186 187 // Determine orientation. 188 189 if ($source_image_height > $source_image_width) { 190 191 $difference = $source_image_height-$source_image_width; 192 imagecopy($final_image_handle,$rotated_image_handle,0,0,0,$difference,$source_image_height,$source_image_width); 193 194 } else { 195 196 imagecopy($final_image_handle,$rotated_image_handle,0,0,0,0,$source_image_height,$source_image_width); 197 198 } 199 200 imagedestroy($rotated_image_handle); 201 202 } elseif ($degrees == "270") { 203 204 $new_dimension = $source_image_width + $source_image_height; 205 $destination_image_handle = ImageCreateTrueColor($new_dimension,$new_dimension); 206 imagecopy($destination_image_handle,$image_handle,$source_image_height,$source_image_width,0,0,$source_image_width,$source_image_height); 207 imagedestroy($image_handle); 208 $final_image_handle = ImageCreateTrueColor($source_image_height,$source_image_width); 209 $rotated_image_handle = imagerotate($destination_image_handle,$degrees,0); 210 imagecopy($final_image_handle,$rotated_image_handle,0,0,0,$source_image_height,$source_image_height,$source_image_width); 211 imagedestroy($rotated_image_handle); 212 213 } elseif ($degrees == "180") { 214 215 $destination_image_handle = ImageCreateTrueColor(2*$source_image_width,$source_image_height); 216 imagecopy($destination_image_handle,$image_handle,$source_image_width,0,0,0,$source_image_width,$source_image_height); 217 imagedestroy($image_handle); 218 $final_image_handle = ImageCreateTrueColor($source_image_width,$source_image_height); 219 $rotated_image_handle = imagerotate($destination_image_handle,$degrees,0); 220 imagecopy($final_image_handle,$rotated_image_handle,0,0,0,0,$source_image_width,$source_image_height); 221 imagedestroy($rotated_image_handle); 222 223 } 224 225 226 // Now let's write the image to disk. 227 write_to_disk($source_image_type, $final_image_handle, $path_to_primary_image); 228 229 /*if ($source_image_type == "2") { 230 231 imagejpeg($final_image_handle, $path_to_primary_image, 100) or die($lang_image_processor_php['no_write']); 232 233 } elseif ($source_image_type == "3") { 234 235 imagepng($final_image_handle, $path_to_primary_image) or die($lang_image_processor_php['no_write']); 236 237 } 238 239 // Destroy the final image handle. 240 241 imagedestroy($final_image_handle); */ 242 243 } 244 245 //****************************************************************************************** 246 247 function palette_rotate($path_to_primary_image, $degrees) { 248 249 global $lang_image_processor_php; 250 251 // Get image info. 252 $source_image_size_and_type = getimagesize($path_to_primary_image) or die($lang_image_processor_php['file_corrupt']); 253 $source_image_width = $source_image_size_and_type[0]; 254 $source_image_height = $source_image_size_and_type[1]; 255 $source_image_type = $source_image_size_and_type[2]; 256 257 //Set new canvas size. 258 if ( $source_image_width > $source_image_height ) { 259 $new_maximum_dimension = $source_image_width; 260 } else { 261 $new_maximum_dimension = $source_image_height; 262 } 263 264 $image_handle = get_handle($path_to_primary_image); 265 266 if ($degrees == "90") { 267 268 $destination_image_handle = ImageCreate($new_maximum_dimension,$new_maximum_dimension); 269 imagecopy($destination_image_handle,$image_handle,0,0,0,0,$source_image_width,$source_image_height); 270 imagedestroy($image_handle); 271 $rotated_image_handle = imagerotate($destination_image_handle,$degrees,0); 272 $final_image_handle = ImageCreate($source_image_height,$source_image_width); 273 274 // Determine orientation. 275 276 if ($source_image_height > $source_image_width) { 277 278 $difference = $source_image_height-$source_image_width; 279 imagecopy($final_image_handle,$rotated_image_handle,0,0,0,$difference,$source_image_height,$source_image_width); 280 281 } else { 282 283 imagecopy($final_image_handle,$rotated_image_handle,0,0,0,0,$source_image_height,$source_image_width); 284 285 } 286 287 imagedestroy($rotated_image_handle); 288 289 } elseif ($degrees == "270") { 290 291 $new_dimension = $source_image_width + $source_image_height; 292 $destination_image_handle = ImageCreate($new_dimension,$new_dimension); 293 imagecopy($destination_image_handle,$image_handle,$source_image_height,$source_image_width,0,0,$source_image_width,$source_image_height); 294 imagedestroy($image_handle); 295 $final_image_handle = ImageCreate($source_image_height,$source_image_width); 296 $rotated_image_handle = imagerotate($destination_image_handle,$degrees,0); 297 imagecopy($final_image_handle,$rotated_image_handle,0,0,0,$source_image_height,$source_image_height,$source_image_width); 298 imagedestroy($rotated_image_handle); 299 300 301 } elseif ($degrees == "180") { 302 303 $destination_image_handle = ImageCreate(2*$source_image_width,$source_image_height); 304 imagecopy($destination_image_handle,$image_handle,$source_image_width,0,0,0,$source_image_width,$source_image_height); 305 imagedestroy($image_handle); 306 $final_image_handle = ImageCreate($source_image_width,$source_image_height); 307 $rotated_image_handle = imagerotate($destination_image_handle,$degrees,0); 308 imagecopy($final_image_handle,$rotated_image_handle,0,0,0,0,$source_image_width,$source_image_height); 309 imagedestroy($rotated_image_handle); 310 311 } 312 313 // Now let's write the image to disk. 314 write_to_disk($source_image_type, $final_image_handle, $path_to_primary_image); 315 /*if ($source_image_type == "2") { 316 317 imagejpeg($final_image_handle, $path_to_primary_image, 100) or die($lang_image_processor_php['no_write']); 318 319 } elseif ($source_image_type == "3") { 320 321 imagepng($final_image_handle, $path_to_primary_image) or die($lang_image_processor_php['no_write']); 322 323 } 324 325 // Destroy the final image handle. 326 327 imagedestroy($final_image_handle);*/ 328 329 330 } 331 332 //********************************************** 333 334 function get_handle($path_to_primary_image) { 335 336 global $lang_image_processor_php, $CONFIG; 337 338 // Let's use this information to create the handle with which to hold our lovely 339 // image. The variable $image_handle is the handle that points to the image's 340 // location in memory. Other handle creating functions are available (wireless 341 // bitmap, for example), but they are very rarely needed. You can learn about 342 // them at php.net in the function library. Look under image functions. If you 343 // desire, you could add these types to the following if-then-else statements. 344 345 $source_image_size_and_type = getimagesize ($path_to_primary_image) or die($lang_image_processor_php['file_corrupt']); 346 $source_image_width = $source_image_size_and_type[0]; 347 $source_image_height = $source_image_size_and_type[1]; 348 $source_image_type = $source_image_size_and_type[2]; 349 350 if ($source_image_type == "1") { 351 // The image is a GIF file. We must verify PHP/GD supports GIF 352 // creation. If not, return an error. 353 354 if ($CONFIG['GIF_support'] == 1) { 355 $image_handle = imagecreatefromgif($path_to_primary_image); 356 } else { 357 cpg_die(CRITICAL_ERROR, $lang_image_processor_php['GD_GIF_Warning'], __FILE__, __LINE__); 358 } 359 360 } elseif ($source_image_type == "2") { 361 362 // The image is a JPG file, so we must use the function 363 // imagecreatefromjpeg. 364 365 $image_handle = imagecreatefromjpeg($path_to_primary_image); 366 367 } elseif ($source_image_type == "3") { 368 369 // The image is a PNG file, so we must use the function 370 // imagecreatefrompng. 371 372 $image_handle = imagecreatefrompng($path_to_primary_image); 373 374 } else { 375 376 // The user has given us an image we do not wish to work with. We return an 377 // error. 378 379 cpg_die(CRITICAL_ERROR, $lang_image_processor_php['not_supported'], __FILE__, __LINE__); 380 381 } 382 383 return $image_handle; 384 385 } 386 387 //****************************************************** 388 389 function image_preview($path_to_primary_image, $maximum_width) { 390 391 //Globalize appropriate variables. 392 global $CONFIG, $lang_image_processor_php, $preview_image_directory; 393 394 //Determine thumbnail method. 395 $method = $CONFIG['thumb_method']; 396 397 if (($method == 'gd2') and (!function_exists('imageistruecolor'))) { 398 399 //Set ignore imageistruecolor to false. 400 $ignore = 0; 401 } else { 402 //Set $ignore image is true color to true. 403 $ignore = 1; 404 } 405 406 // Get image info. 407 $source_image_size_and_type = getimagesize($path_to_primary_image) or die($lang_image_processor_php['file_corrupt']); 408 $source_image_width = $source_image_size_and_type[0]; 409 $source_image_height = $source_image_size_and_type[1]; 410 $source_image_type = $source_image_size_and_type[2]; 411 412 //We need specify the path for the transitory file. 413 414 // Create a prefix for easier human recognition. 415 $prefix = "pre_"; 416 417 //Set the correct file extension. 418 419 if ($source_image_type == '1') { 420 $suffix = '.gif'; 421 } elseif ($source_image_type == '2') { 422 $suffix = '.jpg'; 423 } elseif ($source_image_type == '3') { 424 $suffix = '.png'; 425 } 426 427 // Generate the unique name. 428 429 do { 430 $seed = substr(md5(uniqid('')), 0, 8); 431 $path_to_preview_image = $preview_image_directory . $prefix . $seed . $suffix; 432 } while (file_exists($path_to_preview_image)); 433 434 //Now we can upload the file. 435 436 437 // Calculate dimensions. 438 439 if ($source_image_width > $maximum_width) { 440 441 $new_width = (INTEGER) $maximum_width; 442 $new_height = (INTEGER) ($source_image_height * ($maximum_width / $source_image_width)); 443 444 } else { 445 446 $new_width = $source_image_width; 447 $new_height = $source_image_height; 448 449 } 450 451 452 //Begin processing if GD is used. 453 454 if ($method == "gd2" or $method =="gd1") { 455 456 // Get image handle 457 $image_handle = get_handle($path_to_primary_image); 458 459 // Create the destination image handle. 460 461 if ($method == "gd2") { 462 463 if ($ignore) { 464 if (ImageIsTrueColor($image_handle)) { 465 466 $destination_image_handle = ImageCreateTrueColor($new_width, $new_height); 467 468 } else { 469 470 $destination_image_handle = ImageCreate($new_width, $new_height); 471 472 } 473 } else { 474 475 $destination_image_handle = ImageCreate($new_width, $new_height); 476 477 } 478 479 480 } elseif ($method == "gd1") { 481 482 $destination_image_handle = ImageCreate($new_width, $new_height); 483 484 } 485 486 // Resize the image 487 488 if ($method == "gd2") { 489 490 //Use the higher quality function imagecopyresampled. 491 imagecopyresampled($destination_image_handle, $image_handle, 0, 0, 0, 0, $new_width, $new_height, $source_image_width, $source_image_height); 492 493 } elseif ($method == "gd1") { 494 495 //Use the lower quality imagecopyresized. 496 imagecopyresized($destination_image_handle, $image_handle, 0, 0, 0, 0, $new_width, $new_height, $source_image_width, $source_image_height); 497 498 } 499 500 //Destroy $image_handle 501 imagedestroy($image_handle); 502 503 // Write the image to disk. 504 write_to_disk($source_image_type, $destination_image_handle, $path_to_preview_image); 505 506 /* if ($source_image_type == "2") { 507 508 imagejpeg($destination_image_handle, $path_to_preview_image) or die($lang_image_processor_php['no_write']); 509 510 } elseif ($source_image_type == "3") { 511 512 imagepng($destination_image_handle, $path_to_preview_image) or die($lang_image_processor_php['no_write']); 513 514 } elseif ($source_image_type == "1" && $CONFIG['GIF_support'] == 1) { 515 imagegif($destination_image_handle, $path_to_preview_image) or die($lang_image_processor_php['no_write']); 516 } 517 518 // Destroy $destination_image_handle. 519 imagedestroy($destination_image_handle); */ 520 521 } elseif ($method == "im") { 522 523 // Set IM path. 524 $im_path = $CONFIG['impath']; 525 526 //Check the IM path for the final slash. 527 if (eregi('/$',$im_path) or empty($im_path)) { 528 $trailing_slash = ""; 529 } else { 530 $trailing_slash = "/"; 531 } 532 533 //Determine real paths to files. 534 $real_path_to_primary_image = realpath($path_to_primary_image); 535 $real_path_to_preview_image = realpath($path_to_preview_image); 536 537 // Prevent the user from creating a process zombie by aborting while IM does its work. 538 ignore_user_abort(true); 539 540 // Issue the command for resizing to IM. Have ImageMagick write the image to disk. 541 542 $output = array(); 543 544 $cmd = "{$CONFIG['impath']}".$trailing_slash."convert -geometry {$new_width}x{$new_height} \"$real_path_to_primary_image\" \"$real_path_to_preview_image\""; 545 546 exec ($cmd, $output, $retval); 547 548 // Restore the user abort setting. 549 ignore_user_abort(false); 550 551 if ($retval) { 552 $ERROR = $lang_image_processor_php['IM_Error'] . $retval; 553 if ($CONFIG['debug_mode']) { 554 // Re-execute the command with the backtick operator in order to get all outputs 555 // will not work is safe mode is enabled 556 $output = `$cmd 2>&1`; 557 $ERROR .= "<br /><br /><div align=\"left\">{$lang_image_processor_php['cmd_line']}<br /><span style=\"font-size:120%\">".nl2br(htmlspecialchars($cmd))."</span></div>"; 558 $ERROR .= "<br /><br /><div align=\"left\">{$lang_image_processor_php['mog_said']}<br /><span style=\"font-size:120%\">"; 559 $ERROR .= nl2br(htmlspecialchars($output)); 560 $ERROR .= "</span></div>"; 561 } 562 die($ERROR); 563 564 } 565 566 } 567 568 return $path_to_preview_image; 569 570 } 571 572 //************************************************************************** 573 574 /* commented out this function as newer versions are in upload.php and 575 logger.inc.php 576 function spring_cleaning($directory_path) { 577 578 global $lang_image_processor_php; 579 580 //First we get the transitory directory handle. 581 $directory_handle = opendir($directory_path) or die($lang_image_processor_php['no_open_trans_dir']); 582 583 // Now let's read through the directory contents. 584 while (!(($file = readdir($directory_handle)) === false)) { 585 586 $dir_path = "".$directory_path."/".$file.""; 587 588 if (is_dir($dir_path)) { 589 590 // This is a directory, so we move on. 591 continue; 592 593 } 594 595 // We find out when the file was last accessed. 596 $access_time = fileatime($dir_path); 597 598 // We find out the current time. 599 $current_time = time(); 600 601 // We calculate the the delete time. We will delete anything one hour old or older. 602 $delete_time = $current_time - 3600; 603 604 // Now we compare the two. 605 if ($access_time <= $delete_time) { 606 607 // The file is old. We delete it. 608 unlink($dir_path); 609 } 610 611 } 612 613 // Don't forget to close the directory. 614 closedir($directory_handle); 615 616 } 617 */ 618 619 //********************************************************************************** 620 621 function make_form($next_form_action, $path_to_preview_image, $path_to_primary_image, $file_name) { 622 623 global $event; 624 global $album; 625 global $title; 626 global $caption; 627 global $keywords; 628 global $user1; 629 global $user2; 630 global $user3; 631 global $user4; 632 global $lang_image_processor_php; 633 634 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past 635 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified 636 header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1 637 header("Cache-Control: post-check=0, pre-check=0", false); 638 header("Pragma: no-cache"); // HTTP/1.0 639 640 pageheader($lang_image_processor_php['page_title']); 641 642 print "<br /><br /><br />"; 643 644 print "<center>"; 645 646 print "<img src=\"$path_to_preview_image\" alt=\"{$lang_image_processor_php['preview_image_alt_text']}\" />"; 647 648 print "</center>"; 649 650 print "<br />"; 651 print "<br />"; 652 print "<form action=\"$next_form_action\" method=\"post\">"; 653 print "<input type=\"hidden\" name=\"album\" value=\"$album\" />"; 654 print "<input type=\"hidden\" name=\"title\" value=\"$title\" />"; 655 print "<input type=\"hidden\" name=\"caption\" value=\"$caption\" />"; 656 print "<input type=\"hidden\" name=\"keywords\" value=\"$keywords\" />"; 657 print "<input type=\"hidden\" name=\"user1\" value=\"$user1\" />"; 658 print "<input type=\"hidden\" name=\"user2\" value=\"$user2\" />"; 659 print "<input type=\"hidden\" name=\"user3\" value=\"$user3\" />"; 660 print "<input type=\"hidden\" name=\"user4\" value=\"$user4\" />"; 661 print "<input type=\"hidden\" name=\"event\" value=\"$event\" />"; 662 print "<input type=\"hidden\" name=\"file_name\" value=\"$file_name\" />"; 663 print "<input type=\"hidden\" name=\"transitory_image_path\" value=\"$path_to_primary_image\" />"; 664 print "<input type=\"hidden\" name=\"preview_image_path\" value=\"$path_to_preview_image\" />"; 665 666 print "<p>{$lang_image_processor_php['manipulation_query']}</p>"; 667 668 print "<br />"; 669 print "<input type=\"radio\" name=\"degrees\" value=\"no\" checked=\"checked\" />{$lang_image_processor_php['no_manipulation']} "; 670 print "<input type=\"radio\" name=\"degrees\" value=\"90\" />90° "; 671 print "<input type=\"radio\" name=\"degrees\" value=\"180\" />180° "; 672 print "<input type=\"radio\" name=\"degrees\" value=\"270\" />270° "; 673 print "<br /><br />"; 674 print "<input type=\"submit\" value=\"Continue\" />"; 675 print "</form>"; 676 677 pagefooter(); 678 679 } 680 681 //------------------------------MAIN CODE BLOCK--------------------------- 682 683 // Check to see if the uploader has permission to upload. close the script if he doesn't. 684 685 if (!USER_CAN_UPLOAD_PICTURES) cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__); 686 687 // Let us define the directories where images will be temporarily stored. 688 689 $transitory_file_directory = "./{$CONFIG['fullpath']}manipulation/transitory/"; 690 $preview_image_directory = "./{$CONFIG['fullpath']}manipulation/transitory/previews/"; 691 692 // We can also create a rudimentary language array to make integration into CPG easier at a later date. 693 $lang_image_processor_php = array('finished_manipulation'=>'You have finished manipulating the image. Please click the Proceed button to finish uploading the image.', 694 'finished_manipulation_button'=>'Proceed', 695 'page_title'=>'Image Manipulation', 696 'preview_image_alt_text'=>'Thumbnail preview of image.', 697 'manipulation_query'=>'This is how the image you selected for upload currently appears. Do you wish to rotate the image? All rotations are counterclockwise. Please note, your image has not been uploaded yet.', 698 'no_manipulation'=>'No', 699 'IM_Error'=>'Error executing ImageMagick - Return value: ', 700 'cmd_line'=>'Command Line :', 701 'mog_said'=>'The mogrify program said:', 702 'file_corrupt'=>'The file is corrupt or not accessible.', 703 'no_write'=>'Could not write image to disk.', 704 'GD_GIF_Warning'=>'The image you submitted is in GIF format. Unfortunately, GIF images use the Unisys patented LZW compression scheme, so this program cannot work with them. Please convert your image to PNG or JPG. Then try uploading it again.', 705 'not_supported'=>'The uploaded image type is not supported. Please upload JPG or PNG images.', 706 'no_open_trans_dir'=>'Could not open transitory directory.', 707 'no_move'=>'Couldn\'t copy', 708 'bad_angle'=>'The angle submitted is unacceptable. Please try again.', 709 'tampering' =>'The preview image is missing or its path has been altered.', 710 'primary_tampering' => 'The primary image path is not valid, or the file does not exist.', 711 'err_invalid_fext' => 'Your file extension is not valid.' 712 713 ); 714 715 // Let's inspect the directories for old files, and delete them if they are too old. 716 // Old files might appear if a user upload was interrupted. 717 718 spring_cleaning($transitory_file_directory); 719 720 spring_cleaning($preview_image_directory); 721 722 // The directories have been tidied. 723 724 725 // We also need to set the preview thumbnail width. 726 $maximum_width = $CONFIG['thumb_width']; 727 728 // First, we test for the variable $degrees to determine script action. 729 730 if (!isset($_POST['degrees'])) { 731 732 // Display initial form. 733 734 // First, we must capture all the data sent to us by upload.php. 735 736 $event = $_POST['event']; 737 $album = (int)$_POST['album']; 738 $title = $_POST['title']; 739 $caption = $_POST['caption']; 740 $keywords = $_POST['keywords']; 741 $user1 = $_POST['user1']; 742 $user2 = $_POST['user2']; 743 $user3 = $_POST['user3']; 744 $user4 = $_POST['user4']; 745 746 // First things first. Let's analyze the image file. 747 748 // We already have the file size in bytes and the temporary name in the file 749 // upload global array. 750 751 // The file size is $_FILES['userpicture']['size']. 752 753 $file_size = $_FILES['userpicture']['size']; 754 755 // The temporary name is $_FILES['userpicture']['tmp_name']. 756 757 $temporary_name = $_FILES['userpicture']['tmp_name']; 758 759 // The file name is $_FILES['userpicture']['name']. 760 761 $file_name = $_FILES['userpicture']['name']; 762 763 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 764 765 // We must check the file name for security reasons. 766 if (get_magic_quotes_gpc()) $file_name = stripslashes($file_name); 767 // Replace forbidden chars with underscores 768 $picture_name = replace_forbidden($file_name); 769 // Check that the file uploaded has a valid extension 770 $matches = array(); 771 if (!preg_match("/(.+)\.(.*?)\Z/", $picture_name, $matches)){ 772 $matches[1] = 'invalid_fname'; 773 $matches[2] = 'xxx'; 774 } 775 if ($matches[2]=='' || !stristr($CONFIG['allowed_file_extensions'], $matches[2])) { 776 cpg_die(ERROR, $lang_image_processor_php['err_invalid_fext'], __FILE__, __LINE__); 777 } 778 779 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 780 781 // Now for some validation. 782 // We check the file size of the user's file to end the program if it has no data. 783 // We also check the temporary name to make sure it uploaded properly. 784 785 if (($file_size <= 0) or ($temporary_name == '')) { 786 787 // The user has submitted a corrupted file, a file without data, or the 788 // upload failed. We return an error. 789 790 cpg_die(ERROR, $lang_errors['invalid_image'], __FILE__, __LINE__); 791 } 792 793 // Now we can extract other information into the array $source_image_size_and_type. 794 // getimagesize returns an array with 4 elements. Index 0 contains the width of 795 // the image in pixels. Index 1 contains the height. Index 2 is a flag indicating 796 // the type of the image: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 797 // 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 798 // 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM. Index 3 is a 799 // string for use in HTML img tags. The funtion returns FALSE if it totally fails, 800 // so we may use a die statement to generate the error message. 801 802 $source_image_size_and_type = getimagesize($temporary_name) or die($lang_image_processor_php['file_corrupt']); 803 $source_image_width = $source_image_size_and_type[0]; 804 $source_image_height = $source_image_size_and_type[1]; 805 $source_image_type = $source_image_size_and_type[2]; 806 807 // getimagesize returns a width or height of zero if there is no image in the file 808 // or if it cannot tell what it is looking at. Let's check the width and height. 809 810 if (($source_image_width <= 0) or ($source_image_height <= 0)) { 811 812 // The image supplied is either not an image or is not usable. We return 813 // an error. 814 815 cpg_die(ERROR, $lang_errors['invalid_image'], __FILE__, __LINE__); 816 } 817 818 // Now that we are pretty sure this image is legitimate, we need to move it into 819 // the transitory directory, so that we may continue to work with it. 820 821 //We need specify the path for the transitory file. 822 823 // Create a prefix for easier human recognition. 824 $prefix = "trans_"; 825 826 //Set the correct file extension. 827 828 if ($source_image_type == '1') { 829 $suffix = '.gif'; 830 } elseif ($source_image_type == '2') { 831 $suffix = '.jpg'; 832 } elseif ($source_image_type == '3') { 833 $suffix = '.png'; 834 } 835 836 // Generate the unique name. 837 838 do { 839 $seed = substr(md5(uniqid('')), 0, 8); 840 $path_to_primary_image = $transitory_file_directory . $prefix . $seed . $suffix; 841 } while (file_exists($path_to_primary_image)); 842 843 //Now we can upload the file. 844 845 if (is_uploaded_file($temporary_name)) { 846 move_uploaded_file($temporary_name, $path_to_primary_image) or die ($lang_image_processor_php['no_move']); 847 } 848 849 // The file has been uploaded to the transitory directory. At this point, we 850 // are ready to create the form that will allow the user to rotate the image. 851 // It requires a preview thumbnail to be loaded. We will create the thumbnail 852 // with the image preview function, which will return the file path. 853 854 $path_to_preview_image = image_preview($path_to_primary_image, $maximum_width); 855 856 // Our preview thumbnail is now stored on the server. Let's create the 857 // rotation form. 858 859 make_form($_SERVER[PHP_SELF], $path_to_preview_image, $path_to_primary_image, $file_name); 860 861 } else { 862 863 //Display secondary form. 864 865 // First, we must capture all the data sent to us by the initial form. 866 867 $degrees = $_POST['degrees']; 868 $event = $_POST['event']; 869 $path_to_primary_image = $_POST['transitory_image_path']; 870 $path_to_preview_image = $_POST['preview_image_path']; 871 $transitory_file_name = $_POST['file_name']; 872 $album = (int)$_POST['album']; 873 $title = $_POST['title']; 874 $caption = $_POST['caption']; 875 $keywords = $_POST['keywords']; 876 $user1 = $_POST['user1']; 877 $user2 = $_POST['user2']; 878 $user3 = $_POST['user3']; 879 $user4 = $_POST['user4']; 880 881 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 882 883 // We must check the file name for security reasons. 884 if (get_magic_quotes_gpc()) $transitory_file_name = stripslashes($transitory_file_name); 885 // Replace forbidden chars with underscores 886 $picture_name = replace_forbidden($transitory_file_name); 887 // Check that the file uploaded has a valid extension 888 $matches = array(); 889 if (!preg_match("/(.+)\.(.*?)\Z/", $picture_name, $matches)){ 890 $matches[1] = 'invalid_fname'; 891 $matches[2] = 'xxx'; 892 } 893 if ($matches[2]=='' || !stristr($CONFIG['allowed_file_extensions'], $matches[2])) { 894 cpg_die(ERROR, $lang_image_processor_php['err_invalid_fext'], __FILE__, __LINE__); 895 } 896 897 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 898 899 // Now let us delete the old preview image. First, we must verify the image path and preview path 900 // have not been altered too severely. 901 902 //Create the search strings. 903 904 $preview_search_string = "^".$preview_image_directory.""; 905 $primary_search_string = "^".$transitory_file_directory.""; 906 907 //Check the preview image path and delete if it passes the test. 908 909 if ((ereg($preview_search_string, $path_to_preview_image)) and (file_exists($path_to_preview_image))) { 910 911 // It is safe to delete the preview image. 912 unlink($path_to_preview_image); 913 914 } else { 915 916 // The supplied preview path is invalid. The image is either missing, or someone has tampered with the HTML request. 917 cpg_die(ERROR, $lang_image_processor_php['tampering'], __FILE__, __LINE__); 918 919 } 920 921 //Check the primary image path. 922 923 if ((!(ereg($primary_search_string, $path_to_primary_image))) or (!(file_exists($path_to_primary_image)))) { 924 925 // The primary image path is not valid, or the file does not exist. 926 cpg_die(ERROR, $lang_image_processor_php['primary_tampering'], __FILE__, __LINE__); 927 928 } 929 930 931 // In this instance, $degrees will tell us everything we wish to know about what 932 // to do next. Let's validate it. 933 934 // We will only accept four values: 90, 180, 270, and no. Let's check 935 // the user input: 936 937 if (!(($degrees == "90") or ($degrees == "180") or ($degrees == "270") or ($degrees == "no"))) { 938 939 //The user is submitting incorrect values. We generate an error. 940 941 cpg_die(CRITICAL_ERROR, $lang_image_processor_php['bad_angle'], __FILE__, __LINE__); 942 943 } 944 945 // Now that we have validated, let's analyze what we have. 946 947 if ($degrees == "no") { 948 949 // The user has finished modifiying the image. 950 951 // We create a hidden form that thanks the user, and passes 952 // the information to db_input. 953 954 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past 955 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified 956 header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1 957 header("Cache-Control: post-check=0, pre-check=0", false); 958 header("Pragma: no-cache"); // HTTP/1.0 959 960 pageheader($lang_image_processor_php['page_title']); 961 962 print "<br /><br /><br />"; 963 964 print "<form action=\"db_input.php\" method=\"post\">"; 965 print "<input type=\"hidden\" name=\"album\" value=\"$album\" />"; 966 print "<input type=\"hidden\" name=\"title\" value=\"$title\" />"; 967 print "<input type=\"hidden\" name=\"caption\" value=\"$caption\" />"; 968 print "<input type=\"hidden\" name=\"keywords\" value=\"$keywords\" />"; 969 print "<input type=\"hidden\" name=\"user1\" value=\"$user1\" />"; 970 print "<input type=\"hidden\" name=\"user2\" value=\"$user2\" />"; 971 print "<input type=\"hidden\" name=\"user3\" value=\"$user3\" />"; 972 print "<input type=\"hidden\" name=\"user4\" value=\"$user4\" />"; 973 print "<input type=\"hidden\" name=\"event\" value=\"$event\" />"; 974 print "<input type=\"hidden\" name=\"transitory_image_path\" value=\"$path_to_primary_image\" />"; 975 print "<input type=\"hidden\" name=\"file_name\" value=\"$transitory_file_name\" />"; 976 977 print "<p>{$lang_image_processor_php['finished_manipulation']}</p>"; 978 979 print "<br />"; 980 981 print "<input type=\"submit\" value=\"{$lang_image_processor_php['finished_manipulation_button']}\" />"; 982 print "</form>"; 983 984 pagefooter(); 985 986 } else { 987 988 // The user desires to rotate the image. 989 990 // We use the rotate image function. 991 rotate_image($path_to_primary_image, $degrees); 992 993 // Get width and height here. 994 995 $source_image_size_and_type = getimagesize($path_to_primary_image) or die($lang_image_processor_php['file_corrupt']); 996 $source_image_width = $source_image_size_and_type[0]; 997 $source_image_height = $source_image_size_and_type[1]; 998 $source_image_type = $source_image_size_and_type[2]; 999 1000 // Now we have to create the preview thumbnail. 1001 1002 // The file has been rotated in the transitory directory. At this point, we 1003 // are ready to create the form that will allow the user to rotate the image. 1004 // It requires a preview thumbnail to be loaded. We will use the image_preview 1005 // function. 1006 1007 $path_to_preview_image = image_preview($path_to_primary_image, $maximum_width); 1008 1009 // Our preview thumbnail is now on the server. Let's create the 1010 // rotation form. 1011 1012 make_form($_SERVER[PHP_SELF], $path_to_preview_image, $path_to_primary_image, $transitory_file_name); 1013 1014 } 1015 1016 } 1017 1018 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| [ Powered by PHPXref - Served by Debian GNU/Linux ] |