root

Changeset 673

Show
Ignore:
Timestamp:
08/06/09 23:53:06 (1 year ago)
Author:
wbond
Message:

Performance tweaks for creating fDirectory, fFile and fImage objects via fFilesystem::createObject()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fDirectory.php

    r577 r673 Hide Line Numbers
    1010 * @link       http://flourishlib.com/fDirectory 
    1111 *  
    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] 
    1314 * @changes    1.0.0b4  Refactored ::scan() to use the new fFilesystem::createObject() method [wb, 2009-01-21] 
    1415 * @changes    1.0.0b3  Added the $regex_filter parameter to ::scan() and ::scanRecursive(), fixed bug in ::scanRecursive() [wb, 2009-01-05] 
     
    111112     * @throws fValidationException  When no directory was specified, when the directory does not exist or when the path specified is not a directory 
    112113     *  
    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 
    114116     * @return fDirectory 
    115117     */ 
    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            } 
    139137        } 
    140138         
  • fFile.php

    r672 r673 Hide Line Numbers
    1010 * @link       http://flourishlib.com/fFile 
    1111 *  
    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] 
    1314 * @changes    1.0.0b22  Fixed ::__toString() to never throw an exception [wb, 2009-08-06] 
    1415 * @changes    1.0.0b21  Fixed a bug in ::determineMimeType() [wb, 2009-07-21] 
     
    495496     * @throws fValidationException  When no file was specified, the file does not exist or the path specified is not a file 
    496497     *  
    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 
    498500     * @return fFile 
    499501     */ 
    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            } 
    525523        } 
    526524         
  • fFilesystem.php

    r643 r673 Hide Line Numbers
    1010 * @link       http://flourishlib.com/fFilesystem 
    1111 *  
    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] 
    1314 * @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] 
    1415 * @changes    1.0.0b7  Fixed ::formatFilesize() to work when `$bytes` equals zero [wb, 2009-07-08] 
     
    205206        } 
    206207         
    207         if (!file_exists($path)) { 
     208        if (!is_readable($path)) { 
    208209            throw new fValidationException( 
    209                 'The path specified, %s, does not exist', 
     210                'The path specified, %s, does not exist or is not readable', 
    210211                $path 
    211212            ); 
     
    213214         
    214215        if (is_dir($path)) { 
    215             return new fDirectory($path);   
     216            return new fDirectory($path, TRUE); 
    216217        } 
    217218         
    218219        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); 
    223224    } 
    224225     
  • fImage.php

    r664 r673 Hide Line Numbers
    1010 * @link       http://flourishlib.com/fImage 
    1111 *  
    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] 
    1314 * @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] 
    1415 * @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] 
     
    297298        $extension = strtolower(fFilesystem::getPathInfo($image_path, 'extension')); 
    298299        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) { 
    301302                throw new fValidationException( 
    302303                    'The file specified, %s, does not appear to be an image', 
     
    350351     
    351352    /** 
     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    /** 
    352389     * Checks to make sure the class can handle the image file specified 
    353390     *  
     
    370407        } 
    371408         
    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')) { 
    379412            return FALSE; 
    380413        } 
     
    493526     * @throws fValidationException  When no image was specified, when the image does not exist or when the path specified is not an image 
    494527     *  
    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 
    496530     * @return fImage 
    497531     */ 
    498     public function __construct($file_path
     532    public function __construct($file_path, $skip_checks=FALSE
    499533    { 
    500534        self::determineProcessor(); 
    501535         
    502         parent::__construct($file_path); 
     536        parent::__construct($file_path, $skip_checks); 
    503537         
    504538        if (!self::isImageCompatible($file_path)) { 
     
    718752    public function getType() 
    719753    { 
    720         return self::getInfo($this->file, 'type'); 
     754        return self::getImageType($this->file); 
    721755    } 
    722756     
     
    740774    private function isAnimatedGif() 
    741775    { 
    742         $info = self::getInfo($this->file); 
    743         if ($info['type'] == 'gif') { 
     776        $type = self::getImageType($this->file); 
     777        if ($type == 'gif') { 
    744778            if (preg_match('#\x00\x21\xF9\x04.{4}\x00\x2C#s', file_get_contents($this->file))) { 
    745779                return TRUE; 
     
    759793    private function processWithGD($output_file, $jpeg_quality) 
    760794    { 
    761         $info = self::getInfo($this->file); 
    762          
    763         switch ($info['type']) { 
     795        $type = self::getImageType($this->file); 
     796         
     797        switch ($type) { 
    764798            case 'gif': 
    765799                $gd_res = imagecreatefromgif($this->file); 
     
    835869        // Save the file 
    836870        $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) { 
    845879            case 'gif': 
    846880                imagegif($gd_res, $output_file); 
     
    867901    private function processWithImageMagick($output_file, $jpeg_quality) 
    868902    { 
    869         $info = self::getInfo($this->file); 
     903        $type = self::getImageType($this->file); 
    870904         
    871905        $command_line  = escapeshellcmd(self::$imagemagick_dir . 'convert'); 
     
    883917         
    884918        // TIFF files should be set to a depth of 8 
    885         if ($info['type'] == 'tif') { 
     919        if ($type == 'tif') { 
    886920            $command_line .= ' -depth 8 '; 
    887921        } 
     
    912946        // Set up jpeg compression 
    913947        $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') { 
    922956            $command_line .= ' -compress JPEG -quality ' . $jpeg_quality . ' '; 
    923957        } 
    924958         
    925         $command_line .= ' ' . escapeshellarg($type . ':' . $output_file); 
     959        $command_line .= ' ' . escapeshellarg($new_type . ':' . $output_file); 
    926960         
    927961        exec($command_line); 
     
    10331067        } 
    10341068         
    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') { 
    10371071            throw new fEnvironmentException( 
    10381072                '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.',