+ /** Validate and merge scale parameters with handler thumb parameters, give error if invalid.
+ *
+ * We do this later than getScale, since we need the image
+ * to know which handler, since handlers can make their own parameters.
+ * @param File $image Image that params are for.
+ * @param array $thumbParams Thumbnail parameters from getScale
+ * @param string $otherParams String of otherParams (iiurlparam).
+ * @return array Array of parameters for transform.
+ */
+ protected function mergeThumbParams( $image, $thumbParams, $otherParams ) {
+ if ( $thumbParams === null ) {
+ // No scaling requested
+ return null;
+ }
+ if ( !isset( $thumbParams['width'] ) && isset( $thumbParams['height'] ) ) {
+ // We want to limit only by height in this situation, so pass the
+ // image's full width as the limiting width. But some file types
+ // don't have a width of their own, so pick something arbitrary so
+ // thumbnailing the default icon works.
+ if ( $image->getWidth() <= 0 ) {
+ $thumbParams['width'] = max( $this->getConfig()->get( 'ThumbLimits' ) );
+ } else {
+ $thumbParams['width'] = $image->getWidth();
+ }
+ }
+
+ if ( !$otherParams ) {
+ $this->checkParameterNormalise( $image, $thumbParams );
+ return $thumbParams;
+ }
+ $p = $this->getModulePrefix();
+
+ $h = $image->getHandler();
+ if ( !$h ) {
+ $this->addWarning( [ 'apiwarn-nothumb-noimagehandler', wfEscapeWikiText( $image->getName() ) ] );
+
+ return $thumbParams;
+ }
+
+ $paramList = $h->parseParamString( $otherParams );
+ if ( !$paramList ) {
+ // Just set a warning (instead of dieUsage), as in many cases
+ // we could still render the image using width and height parameters,
+ // and this type of thing could happen between different versions of
+ // handlers.
+ $this->addWarning( [ 'apiwarn-badurlparam', $p, wfEscapeWikiText( $image->getName() ) ] );
+ $this->checkParameterNormalise( $image, $thumbParams );
+ return $thumbParams;
+ }
+
+ if ( isset( $paramList['width'] ) && isset( $thumbParams['width'] ) ) {
+ if ( intval( $paramList['width'] ) != intval( $thumbParams['width'] ) ) {
+ $this->addWarning(
+ [ 'apiwarn-urlparamwidth', $p, $paramList['width'], $thumbParams['width'] ]
+ );
+ }
+ }
+
+ foreach ( $paramList as $name => $value ) {
+ if ( !$h->validateParam( $name, $value ) ) {
+ $this->dieWithError(
+ [ 'apierror-invalidurlparam', $p, wfEscapeWikiText( $name ), wfEscapeWikiText( $value ) ]
+ );
+ }
+ }
+
+ $finalParams = $thumbParams + $paramList;
+ $this->checkParameterNormalise( $image, $finalParams );
+ return $finalParams;
+ }
+
+ /**
+ * Verify that the final image parameters can be normalised.
+ *
+ * This doesn't use the normalised parameters, since $file->transform
+ * expects the pre-normalised parameters, but doing the normalisation
+ * allows us to catch certain error conditions early (such as missing
+ * required parameter).
+ *
+ * @param File $image
+ * @param array $finalParams List of parameters to transform image with
+ */
+ protected function checkParameterNormalise( $image, $finalParams ) {
+ $h = $image->getHandler();
+ if ( !$h ) {
+ return;
+ }
+ // Note: normaliseParams modifies the array in place, but we aren't interested
+ // in the actual normalised version, only if we can actually normalise them,
+ // so we use the functions scope to throw away the normalisations.
+ if ( !$h->normaliseParams( $image, $finalParams ) ) {
+ $this->dieWithError( [ 'apierror-urlparamnormal', wfEscapeWikiText( $image->getName() ) ] );
+ }
+ }