- Timestamp:
- 08/06/09 23:53:06 (1 year ago)
- Files:
-
- fDirectory.php (modified) (2 diffs)
- fFile.php (modified) (2 diffs)
- fFilesystem.php (modified) (3 diffs)
- fImage.php (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
fDirectory.php
r577 r673 Hide Line Numbers 10 10 * @link http://flourishlib.com/fDirectory 11 11 * 12 * @version 1.0.0b4 12 * @version 1.0.0b5 13 * @changes 1.0.0b5 Added the ability to skip checks in ::__construct() for better performance in conjunction with fFilesystem::createObject() [wb, 2009-08-06] 13 14 * @changes 1.0.0b4 Refactored ::scan() to use the new fFilesystem::createObject() method [wb, 2009-01-21] 14 15 * @changes 1.0.0b3 Added the $regex_filter parameter to ::scan() and ::scanRecursive(), fixed bug in ::scanRecursive() [wb, 2009-01-05] … … 111 112 * @throws fValidationException When no directory was specified, when the directory does not exist or when the path specified is not a directory 112 113 * 113 * @param string $directory The path to the directory 114 * @param string $directory The path to the directory 115 * @param boolean $skip_checks If file checks should be skipped, which improves performance, but may cause undefined behavior - only skip these if they are duplicated elsewhere 114 116 * @return fDirectory 115 117 */ 116 public function __construct($directory) 117 { 118 if (empty($directory)) { 119 throw new fValidationException('No directory was specified'); 120 } 121 122 if (!file_exists($directory)) { 123 throw new fValidationException( 124 'The directory specified, %s, does not exist', 125 $directory 126 ); 127 } 128 if (!is_dir($directory)) { 129 throw new fValidationException( 130 'The directory specified, %s, is not a directory', 131 $directory 132 ); 133 } 134 if (!is_readable($directory)) { 135 throw new fEnvironmentException( 136 'The directory specified, %s, is not readable', 137 $directory 138 ); 118 public function __construct($directory, $skip_checks=FALSE) 119 { 120 if (!$skip_checks) { 121 if (empty($directory)) { 122 throw new fValidationException('No directory was specified'); 123 } 124 125 if (!is_readable($directory)) { 126 throw new fEnvironmentException( 127 'The directory specified, %s, does not exist or is not readable', 128 $directory 129 ); 130 } 131 if (!is_dir($directory)) { 132 throw new fValidationException( 133 'The directory specified, %s, is not a directory', 134 $directory 135 ); 136 } 139 137 } 140 138 fFile.php
r672 r673 Hide Line Numbers 10 10 * @link http://flourishlib.com/fFile 11 11 * 12 * @version 1.0.0b22 12 * @version 1.0.0b23 13 * @changes 1.0.0b23 Added the ability to skip checks in ::__construct() for better performance in conjunction with fFilesystem::createObject() [wb, 2009-08-06] 13 14 * @changes 1.0.0b22 Fixed ::__toString() to never throw an exception [wb, 2009-08-06] 14 15 * @changes 1.0.0b21 Fixed a bug in ::determineMimeType() [wb, 2009-07-21] … … 495 496 * @throws fValidationException When no file was specified, the file does not exist or the path specified is not a file 496 497 * 497 * @param string $file The path to the file 498 * @param string $file The path to the file 499 * @param boolean $skip_checks If file checks should be skipped, which improves performance, but may cause undefined behavior - only skip these if they are duplicated elsewhere 498 500 * @return fFile 499 501 */ 500 public function __construct($file) 501 { 502 if (empty($file)) { 503 throw new fValidationException( 504 'No filename was specified' 505 ); 506 } 507 508 if (!file_exists($file)) { 509 throw new fValidationException( 510 'The file specified, %s, does not exist', 511 $file 512 ); 513 } 514 if (!is_readable($file)) { 515 throw new fEnvironmentException( 516 'The file specified, %s, is not readable', 517 $file 518 ); 519 } 520 if (is_dir($file)) { 521 throw new fValidationException( 522 'The file specified, %s, is actually a directory', 523 $file 524 ); 502 public function __construct($file, $skip_checks=FALSE) 503 { 504 if (!$skip_checks) { 505 if (empty($file)) { 506 throw new fValidationException( 507 'No filename was specified' 508 ); 509 } 510 511 if (!is_readable($file)) { 512 throw new fEnvironmentException( 513 'The file specified, %s, does not exist or is not readable', 514 $file 515 ); 516 } 517 if (is_dir($file)) { 518 throw new fValidationException( 519 'The file specified, %s, is actually a directory', 520 $file 521 ); 522 } 525 523 } 526 524 fFilesystem.php
r643 r673 Hide Line Numbers 10 10 * @link http://flourishlib.com/fFilesystem 11 11 * 12 * @version 1.0.0b8 12 * @version 1.0.0b9 13 * @changes 1.0.0b9 Added some performance tweaks to ::createObject() [wb, 2009-08-06] 13 14 * @changes 1.0.0b8 Changed ::formatFilesize() to not use decimal places for bytes, add a space before and drop the `B` in suffixes [wb, 2009-07-12] 14 15 * @changes 1.0.0b7 Fixed ::formatFilesize() to work when `$bytes` equals zero [wb, 2009-07-08] … … 205 206 } 206 207 207 if (! file_exists($path)) {208 if (!is_readable($path)) { 208 209 throw new fValidationException( 209 'The path specified, %s, does not exist ',210 'The path specified, %s, does not exist or is not readable', 210 211 $path 211 212 ); … … 213 214 214 215 if (is_dir($path)) { 215 return new fDirectory($path );216 return new fDirectory($path, TRUE); 216 217 } 217 218 218 219 if (fImage::isImageCompatible($path)) { 219 return new fImage($path );220 } 221 222 return new fFile($path );220 return new fImage($path, TRUE); 221 } 222 223 return new fFile($path, TRUE); 223 224 } 224 225 fImage.php
r664 r673 Hide Line Numbers 10 10 * @link http://flourishlib.com/fImage 11 11 * 12 * @version 1.0.0b13 12 * @version 1.0.0b14 13 * @changes 1.0.0b14 Performance updates for checking image type and compatiblity [wb, 2009-07-31] 13 14 * @changes 1.0.0b13 Updated class to work even if the file extension is wrong or not present, ::saveChanges() detects files that aren't writable [wb, 2009-07-29] 14 15 * @changes 1.0.0b12 Fixed a bug where calling ::saveChanges() after unserializing would throw an exception related to the image processor [wb, 2009-05-27] … … 297 298 $extension = strtolower(fFilesystem::getPathInfo($image_path, 'extension')); 298 299 if (!in_array($extension, array('jpg', 'jpeg', 'png', 'gif', 'tif', 'tiff'))) { 299 $ mime_type = fFile::determineMimeType($image_path);300 if ( !in_array($mime_type, array('image/jpeg', 'image/png', 'image/gif', 'image/tiff'))) {300 $type = self::getImageType($image_path); 301 if ($type === NULL) { 301 302 throw new fValidationException( 302 303 'The file specified, %s, does not appear to be an image', … … 350 351 351 352 /** 353 * Gets the image type from a file by looking at the file contents 354 * 355 * @param string $image The image path to get the type for 356 * @return string|NULL The type of the image - `'jpg'`, `'gif'`, `'png'` or `'tif'` - NULL if not one of those 357 */ 358 static private function getImageType($image) 359 { 360 $handle = fopen($image, 'r'); 361 $contents = fread($handle, 12); 362 fclose($handle); 363 364 $_0_8 = substr($contents, 0, 8); 365 $_0_4 = substr($contents, 0, 4); 366 $_6_4 = substr($contents, 6, 4); 367 368 if ($_0_4 == "MM\x00\x2A" || $_0_4 == "II\x2A\x00") { 369 return 'tif'; 370 } 371 372 if ($_0_8 == "\x89PNG\x0D\x0A\x1A\x0A") { 373 return 'png'; 374 } 375 376 if ($_0_4 == 'GIF8') { 377 return 'gif'; 378 } 379 380 if ($_6_4 == 'JFIF' || $_6_4 == 'Exif') { 381 return 'jpg'; 382 } 383 384 return NULL; 385 } 386 387 388 /** 352 389 * Checks to make sure the class can handle the image file specified 353 390 * … … 370 407 } 371 408 372 try { 373 $info = self::getInfo($image); 374 375 if ($info['type'] === NULL || ($info['type'] == 'tif' && self::$processor == 'gd')) { 376 return FALSE; 377 } 378 } catch (fValidationException $e) { 409 $type = self::getImageType($image); 410 411 if ($type === NULL || ($type == 'tif' && self::$processor == 'gd')) { 379 412 return FALSE; 380 413 } … … 493 526 * @throws fValidationException When no image was specified, when the image does not exist or when the path specified is not an image 494 527 * 495 * @param string $file_path The path to the image 528 * @param string $file_path The path to the image 529 * @param boolean $skip_checks If file checks should be skipped, which improves performance, but may cause undefined behavior - only skip these if they are duplicated elsewhere 496 530 * @return fImage 497 531 */ 498 public function __construct($file_path )532 public function __construct($file_path, $skip_checks=FALSE) 499 533 { 500 534 self::determineProcessor(); 501 535 502 parent::__construct($file_path );536 parent::__construct($file_path, $skip_checks); 503 537 504 538 if (!self::isImageCompatible($file_path)) { … … 718 752 public function getType() 719 753 { 720 return self::getI nfo($this->file, 'type');754 return self::getImageType($this->file); 721 755 } 722 756 … … 740 774 private function isAnimatedGif() 741 775 { 742 $ info = self::getInfo($this->file);743 if ($ info['type']== 'gif') {776 $type = self::getImageType($this->file); 777 if ($type == 'gif') { 744 778 if (preg_match('#\x00\x21\xF9\x04.{4}\x00\x2C#s', file_get_contents($this->file))) { 745 779 return TRUE; … … 759 793 private function processWithGD($output_file, $jpeg_quality) 760 794 { 761 $ info = self::getInfo($this->file);762 763 switch ($ info['type']) {795 $type = self::getImageType($this->file); 796 797 switch ($type) { 764 798 case 'gif': 765 799 $gd_res = imagecreatefromgif($this->file); … … 835 869 // Save the file 836 870 $path_info = fFilesystem::getPathInfo($output_file); 837 $ type = $path_info['extension'];838 $ type = ($type == 'jpeg') ? 'jpg' : $type;839 840 if (!in_array($ type, array('gif', 'jpg', 'png'))) {841 $ type = $info['type'];842 } 843 844 switch ($ type) {871 $new_type = $path_info['extension']; 872 $new_type = ($type == 'jpeg') ? 'jpg' : $type; 873 874 if (!in_array($new_type, array('gif', 'jpg', 'png'))) { 875 $new_type = $type; 876 } 877 878 switch ($new_type) { 845 879 case 'gif': 846 880 imagegif($gd_res, $output_file); … … 867 901 private function processWithImageMagick($output_file, $jpeg_quality) 868 902 { 869 $ info = self::getInfo($this->file);903 $type = self::getImageType($this->file); 870 904 871 905 $command_line = escapeshellcmd(self::$imagemagick_dir . 'convert'); … … 883 917 884 918 // TIFF files should be set to a depth of 8 885 if ($ info['type']== 'tif') {919 if ($type == 'tif') { 886 920 $command_line .= ' -depth 8 '; 887 921 } … … 912 946 // Set up jpeg compression 913 947 $path_info = fFilesystem::getPathInfo($output_file); 914 $ type = $path_info['extension'];915 $ type = ($type == 'jpeg') ? 'jpg' : $type;916 917 if (!in_array($ type, array('gif', 'jpg', 'png'))) {918 $ type = $info['type'];919 } 920 921 if ($ type == 'jpg') {948 $new_type = $path_info['extension']; 949 $new_type = ($new_type == 'jpeg') ? 'jpg' : $new_type; 950 951 if (!in_array($new_type, array('gif', 'jpg', 'png'))) { 952 $new_type = $type; 953 } 954 955 if ($new_type == 'jpg') { 922 956 $command_line .= ' -compress JPEG -quality ' . $jpeg_quality . ' '; 923 957 } 924 958 925 $command_line .= ' ' . escapeshellarg($ type . ':' . $output_file);959 $command_line .= ' ' . escapeshellarg($new_type . ':' . $output_file); 926 960 927 961 exec($command_line); … … 1033 1067 } 1034 1068 1035 $ info = self::getInfo($this->file);1036 if ($ info['type']== 'tif' && self::$processor == 'gd') {1069 $type = self::getImageType($this->file); 1070 if ($type == 'tif' && self::$processor == 'gd') { 1037 1071 throw new fEnvironmentException( 1038 1072 'The image specified, %s, is a TIFF file and the GD extension can not handle TIFF files. Please install ImageMagick if you wish to manipulate TIFF files.',
