MediaWiki  REL1_24
Jpeg.php
Go to the documentation of this file.
00001 <?php
00033 class JpegHandler extends ExifBitmapHandler {
00034 
00035     function normaliseParams( $image, &$params ) {
00036         if ( !parent::normaliseParams( $image, $params ) ) {
00037             return false;
00038         }
00039         if ( isset( $params['quality'] ) && !self::validateQuality( $params['quality'] ) ) {
00040             return false;
00041         }
00042         return true;
00043     }
00044 
00045     function validateParam( $name, $value ) {
00046         if ( $name === 'quality' ) {
00047             return self::validateQuality( $value );
00048         } else {
00049             return parent::validateParam( $name, $value );
00050         }
00051     }
00052 
00057     private static function validateQuality( $value ) {
00058         return $value === 'low';
00059     }
00060 
00061     function makeParamString( $params ) {
00062         // Prepend quality as "qValue-". This has to match parseParamString() below
00063         $res = parent::makeParamString( $params );
00064         if ( $res && isset( $params['quality'] ) ) {
00065             $res = "q{$params['quality']}-$res";
00066         }
00067         return $res;
00068     }
00069 
00070     function parseParamString( $str ) {
00071         // $str contains "qlow-200px" or "200px" strings because thumb.php would strip the filename
00072         // first - check if the string begins with "qlow-", and if so, treat it as quality.
00073         // Pass the first portion, or the whole string if "qlow-" not found, to the parent
00074         // The parsing must match the makeParamString() above
00075         $res = false;
00076         $m = false;
00077         if ( preg_match( '/q([^-]+)-(.*)$/', $str, $m ) ) {
00078             $v = $m[1];
00079             if ( self::validateQuality( $v ) ) {
00080                 $res = parent::parseParamString( $m[2] );
00081                 if ( $res ) {
00082                     $res['quality'] = $v;
00083                 }
00084             }
00085         } else {
00086             $res = parent::parseParamString( $str );
00087         }
00088         return $res;
00089     }
00090 
00091     function getScriptParams( $params ) {
00092         $res = parent::getScriptParams( $params );
00093         if ( isset( $params['quality'] ) ) {
00094             $res['quality'] = $params['quality'];
00095         }
00096         return $res;
00097     }
00098 
00099     function getMetadata( $image, $filename ) {
00100         try {
00101             $meta = BitmapMetadataHandler::Jpeg( $filename );
00102             if ( !is_array( $meta ) ) {
00103                 // This should never happen, but doesn't hurt to be paranoid.
00104                 throw new MWException( 'Metadata array is not an array' );
00105             }
00106             $meta['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
00107 
00108             return serialize( $meta );
00109         } catch ( MWException $e ) {
00110             // BitmapMetadataHandler throws an exception in certain exceptional
00111             // cases like if file does not exist.
00112             wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
00113 
00114             /* This used to use 0 (ExifBitmapHandler::OLD_BROKEN_FILE) for the cases
00115              *  * No metadata in the file
00116              *  * Something is broken in the file.
00117              * However, if the metadata support gets expanded then you can't tell if the 0 is from
00118              * a broken file, or just no props found. A broken file is likely to stay broken, but
00119              * a file which had no props could have props once the metadata support is improved.
00120              * Thus switch to using -1 to denote only a broken file, and use an array with only
00121              * MEDIAWIKI_EXIF_VERSION to denote no props.
00122              */
00123 
00124             return ExifBitmapHandler::BROKEN_FILE;
00125         }
00126     }
00127 
00135     public function rotate( $file, $params ) {
00136         global $wgJpegTran;
00137 
00138         $rotation = ( $params['rotation'] + $this->getRotation( $file ) ) % 360;
00139 
00140         if ( $wgJpegTran && is_file( $wgJpegTran ) ) {
00141             $cmd = wfEscapeShellArg( $wgJpegTran ) .
00142                 " -rotate " . wfEscapeShellArg( $rotation ) .
00143                 " -outfile " . wfEscapeShellArg( $params['dstPath'] ) .
00144                 " " . wfEscapeShellArg( $params['srcPath'] );
00145             wfDebug( __METHOD__ . ": running jpgtran: $cmd\n" );
00146             wfProfileIn( 'jpegtran' );
00147             $retval = 0;
00148             $err = wfShellExecWithStderr( $cmd, $retval );
00149             wfProfileOut( 'jpegtran' );
00150             if ( $retval !== 0 ) {
00151                 $this->logErrorForExternalProcess( $retval, $err, $cmd );
00152 
00153                 return new MediaTransformError( 'thumbnail_error', 0, 0, $err );
00154             }
00155 
00156             return false;
00157         } else {
00158             return parent::rotate( $file, $params );
00159         }
00160     }
00161 
00162     public function supportsBucketing() {
00163         return true;
00164     }
00165 
00166     public function sanitizeParamsForBucketing( $params ) {
00167         $params = parent::sanitizeParamsForBucketing( $params );
00168 
00169         // Quality needs to be cleared for bucketing. Buckets need to be default quality
00170         if ( isset( $params['quality'] ) ) {
00171             unset( $params['quality'] );
00172         }
00173 
00174         return $params;
00175     }
00176 }