MediaWiki  1.27.2
Jpeg.php
Go to the documentation of this file.
1 <?php
34 
35  function normaliseParams( $image, &$params ) {
36  if ( !parent::normaliseParams( $image, $params ) ) {
37  return false;
38  }
39  if ( isset( $params['quality'] ) && !self::validateQuality( $params['quality'] ) ) {
40  return false;
41  }
42  return true;
43  }
44 
45  public function validateParam( $name, $value ) {
46  if ( $name === 'quality' ) {
47  return self::validateQuality( $value );
48  } else {
49  return parent::validateParam( $name, $value );
50  }
51  }
52 
57  private static function validateQuality( $value ) {
58  return $value === 'low';
59  }
60 
61  public function makeParamString( $params ) {
62  // Prepend quality as "qValue-". This has to match parseParamString() below
63  $res = parent::makeParamString( $params );
64  if ( $res && isset( $params['quality'] ) ) {
65  $res = "q{$params['quality']}-$res";
66  }
67  return $res;
68  }
69 
70  public function parseParamString( $str ) {
71  // $str contains "qlow-200px" or "200px" strings because thumb.php would strip the filename
72  // first - check if the string begins with "qlow-", and if so, treat it as quality.
73  // Pass the first portion, or the whole string if "qlow-" not found, to the parent
74  // The parsing must match the makeParamString() above
75  $res = false;
76  $m = false;
77  if ( preg_match( '/q([^-]+)-(.*)$/', $str, $m ) ) {
78  $v = $m[1];
79  if ( self::validateQuality( $v ) ) {
80  $res = parent::parseParamString( $m[2] );
81  if ( $res ) {
82  $res['quality'] = $v;
83  }
84  }
85  } else {
86  $res = parent::parseParamString( $str );
87  }
88  return $res;
89  }
90 
91  function getScriptParams( $params ) {
92  $res = parent::getScriptParams( $params );
93  if ( isset( $params['quality'] ) ) {
94  $res['quality'] = $params['quality'];
95  }
96  return $res;
97  }
98 
99  function getMetadata( $image, $filename ) {
100  try {
101  $meta = BitmapMetadataHandler::Jpeg( $filename );
102  if ( !is_array( $meta ) ) {
103  // This should never happen, but doesn't hurt to be paranoid.
104  throw new MWException( 'Metadata array is not an array' );
105  }
106  $meta['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
107 
108  return serialize( $meta );
109  } catch ( Exception $e ) {
110  // BitmapMetadataHandler throws an exception in certain exceptional
111  // cases like if file does not exist.
112  wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
113 
114  /* This used to use 0 (ExifBitmapHandler::OLD_BROKEN_FILE) for the cases
115  * * No metadata in the file
116  * * Something is broken in the file.
117  * However, if the metadata support gets expanded then you can't tell if the 0 is from
118  * a broken file, or just no props found. A broken file is likely to stay broken, but
119  * a file which had no props could have props once the metadata support is improved.
120  * Thus switch to using -1 to denote only a broken file, and use an array with only
121  * MEDIAWIKI_EXIF_VERSION to denote no props.
122  */
123 
125  }
126  }
127 
135  public function rotate( $file, $params ) {
137 
138  $rotation = ( $params['rotation'] + $this->getRotation( $file ) ) % 360;
139 
140  if ( $wgJpegTran && is_executable( $wgJpegTran ) ) {
141  $cmd = wfEscapeShellArg( $wgJpegTran ) .
142  " -rotate " . wfEscapeShellArg( $rotation ) .
143  " -outfile " . wfEscapeShellArg( $params['dstPath'] ) .
144  " " . wfEscapeShellArg( $params['srcPath'] );
145  wfDebug( __METHOD__ . ": running jpgtran: $cmd\n" );
146  $retval = 0;
147  $err = wfShellExecWithStderr( $cmd, $retval );
148  if ( $retval !== 0 ) {
149  $this->logErrorForExternalProcess( $retval, $err, $cmd );
150 
151  return new MediaTransformError( 'thumbnail_error', 0, 0, $err );
152  }
153 
154  return false;
155  } else {
156  return parent::rotate( $file, $params );
157  }
158  }
159 
160  public function supportsBucketing() {
161  return true;
162  }
163 
164  public function sanitizeParamsForBucketing( $params ) {
165  $params = parent::sanitizeParamsForBucketing( $params );
166 
167  // Quality needs to be cleared for bucketing. Buckets need to be default quality
168  if ( isset( $params['quality'] ) ) {
169  unset( $params['quality'] );
170  }
171 
172  return $params;
173  }
174 }
supportsBucketing()
Definition: Jpeg.php:160
normaliseParams($image, &$params)
Definition: Jpeg.php:35
rotate($file, $params)
Definition: Jpeg.php:135
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:1932
static version()
#@-
Definition: Exif.php:586
$value
validateParam($name, $value)
Definition: Jpeg.php:45
$wgJpegTran
used for lossless jpeg rotation
parseParamString($str)
Definition: Jpeg.php:70
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static validateQuality($value)
Validate and normalize quality value to be between 1 and 100 (inclusive).
Definition: Jpeg.php:57
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
logErrorForExternalProcess($retval, $err, $cmd)
Log an error that occurred in an external process.
makeParamString($params)
Definition: Jpeg.php:61
sanitizeParamsForBucketing($params)
Definition: Jpeg.php:164
$res
Definition: database.txt:21
$params
Stuff specific to JPEG and (built-in) TIFF handler.
Definition: ExifBitmap.php:30
JPEG specific handler.
Definition: Jpeg.php:33
static Jpeg($filename)
Main entry point for jpeg's.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
getMetadata($image, $filename)
Definition: Jpeg.php:99
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
Definition: hooks.txt:762
wfEscapeShellArg()
Windows-compatible version of escapeshellarg() Windows doesn't recognise single-quotes in the shell...
serialize()
Definition: ApiMessage.php:94
getScriptParams($params)
Definition: Jpeg.php:91
wfShellExecWithStderr($cmd, &$retval=null, $environ=[], $limits=[])
Execute a shell command, returning both stdout and stderr.
getRotation($file)
On supporting image formats, try to read out the low-level orientation of the file and return the ang...
Definition: ExifBitmap.php:205
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account incomplete not yet checked for validity & $retval
Definition: hooks.txt:242
Basic media transform error class.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310