MediaWiki master
ThumbnailImage.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Media;
11
12use InvalidArgumentException;
19
41 public function __construct( $file, $url, $path = false, $parameters = [] ) {
42 // Previous parameters:
43 // $file, $url, $width, $height, $path = false, $page = false
44
45 $defaults = [
46 'page' => false,
47 'lang' => false
48 ];
49
50 if ( is_array( $parameters ) ) {
51 $actualParams = $parameters + $defaults;
52 } else {
53 // Using old format, should convert. Later a warning could be added here.
54 $numArgs = func_num_args();
55 $actualParams = [
56 'width' => $path,
57 'height' => $parameters,
58 'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
59 ] + $defaults;
60 $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
61 }
62
63 if ( $actualParams['usePhysicalSize'] ?? false ) {
64 $actualParams['width'] = $actualParams['physicalWidth'];
65 $actualParams['height'] = $actualParams['physicalHeight'];
66 }
67
68 $this->file = $file;
69 $this->url = $url;
70 $this->path = $path;
71
72 // These should be integers when they get here.
73 // If not, there's a bug somewhere. But let's at
74 // least produce valid HTML code regardless.
75 $this->width = (int)round( $actualParams['width'] );
76 $this->height = (int)round( $actualParams['height'] );
77
78 $this->page = $actualParams['page'];
79 $this->lang = $actualParams['lang'];
80 }
81
83 public function getAttribs( array $options = [] ): array {
84 $services = MediaWikiServices::getInstance();
85 $mainConfig = $services->getMainConfig();
86 $nativeImageLazyLoading = $mainConfig->get( MainConfigNames::NativeImageLazyLoading );
87
88 $attribs = parent::getAttribs( $options );
89
90 // An empty alt indicates an image is not a key part of the content and
91 // that non-visual browsers may omit it from rendering. Only set the
92 // parameter if it's explicitly requested.
93 if ( isset( $options['alt'] ) ) {
94 $attribs['alt'] = $options['alt'];
95 }
96
97 // Description links get the mw-file-description class and link
98 // to the file description page, making the resource redundant
99 if (
100 isset( $options['magnify-resource'] ) &&
101 !( $options['desc-link'] ?? false )
102 ) {
103 $attribs['resource'] = $options['magnify-resource'];
104 }
105
106 $attribs += [
107 'src' => $this->getUrl(),
108 'decoding' => 'async',
109 ];
110
111 if ( $options['loading'] ?? $nativeImageLazyLoading ) {
112 $attribs['loading'] = $options['loading'] ?? 'lazy';
113 }
114
115 if ( empty( $options['no-dimensions'] ) ) {
116 $attribs['width'] = $this->width;
117 $attribs['height'] = $this->height;
118 }
119 $style = '';
120 if ( !empty( $options['valign'] ) ) {
121 $style .= "vertical-align: {$options['valign']};";
122 }
123 if ( !empty( $options['style'] ) ) {
124 $style .= $options['style'];
125 }
126 $style = trim( $style );
127 if ( $style ) {
128 $attribs['style'] = $style;
129 }
130 if ( !empty( $options['img-class'] ) ) {
131 $attribs['class'] = $options['img-class'];
132 }
133 if ( isset( $options['override-height'] ) ) {
134 $attribs['height'] = $options['override-height'];
135 }
136 if ( isset( $options['override-width'] ) ) {
137 $attribs['width'] = $options['override-width'];
138 }
139
140 // Additional densities for responsive images, if specified.
141 // If any of these urls is the same as src url, it'll be excluded.
142 $responsiveUrls = array_diff( $this->responsiveUrls, [ $this->getUrl() ] );
143 if ( $responsiveUrls ) {
144 $attribs['srcset'] = Html::srcSet( $responsiveUrls );
145 }
146
147 return $attribs;
148 }
149
184 public function toHtml( $options = [] ) {
185 if ( func_num_args() === 2 ) {
186 throw new InvalidArgumentException( __METHOD__ . ' called in the old style' );
187 }
188
189 $query = $options['desc-query'] ?? '';
190
191 $attribs = $this->getAttribs( $options );
192
193 if ( !empty( $options['custom-url-link'] ) ) {
194 $linkAttribs = [ 'href' => $options['custom-url-link'] ];
195 if ( !empty( $options['title'] ) ) {
196 $linkAttribs['title'] = $options['title'];
197 }
198 if ( !empty( $options['custom-target-link'] ) ) {
199 $linkAttribs['target'] = $options['custom-target-link'];
200 } elseif ( !empty( $options['parser-extlink-target'] ) ) {
201 $linkAttribs['target'] = $options['parser-extlink-target'];
202 }
203 if ( !empty( $options['parser-extlink-rel'] ) ) {
204 $linkAttribs['rel'] = $options['parser-extlink-rel'];
205 }
206 } elseif ( !empty( $options['custom-title-link'] ) ) {
208 $title = $options['custom-title-link'];
209 $linkAttribs = [
210 'href' => $title->getLinkURL( $options['custom-title-link-query'] ?? null ),
211 'title' => empty( $options['title'] ) ? $title->getPrefixedText() : $options['title']
212 ];
213 } elseif ( !empty( $options['desc-link'] ) ) {
214 $linkAttribs = $this->getDescLinkAttribs(
215 empty( $options['title'] ) ? null : $options['title'],
216 $query
217 );
218 } elseif ( !empty( $options['file-link'] ) ) {
219 $linkAttribs = [ 'href' => $this->file->getUrl() ];
220 } else {
221 $linkAttribs = false;
222 if ( !empty( $options['title'] ) ) {
223 $linkAttribs = [ 'title' => $options['title'] ];
224 }
225 }
226
227 $services = MediaWikiServices::getInstance();
228 ( new HookRunner( $services->getHookContainer() ) )
229 ->onThumbnailBeforeProduceHTML( $this, $attribs, $linkAttribs );
230
231 return $this->linkWrap( $linkAttribs, Html::element( 'img', $attribs ) );
232 }
233}
234
236class_alias( ThumbnailImage::class, 'ThumbnailImage' );
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:80
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
A class containing constants representing the names of configuration variables.
const NativeImageLazyLoading
Name constant for the NativeImageLazyLoading setting, for use with Config::get()
Service locator for MediaWiki core services.
getMainConfig()
Returns the Config object that provides configuration for MediaWiki core.
static getInstance()
Returns the global default instance of the top level service locator.
Base class for the output of MediaHandler::doTransform() and File::transform().
array $responsiveUrls
Associative array mapping optional supplementary image files from pixel density (eg 1....
string null false $path
Filesystem path to the thumb.
string false $url
URL path to the thumb.
Media transform output for images.
toHtml( $options=[])
Return HTML <img ... /> tag for the thumbnail, will include width and height attributes and a blank a...
__construct( $file, $url, $path=false, $parameters=[])
Get a thumbnail object from a file and parameters.
getAttribs(array $options=[])
Return extra attributes that should be present on the media tag in the HTML.Takes the same option arr...
Represents a title within MediaWiki.
Definition Title.php:69
element(SerializerNode $parent, SerializerNode $node, $contents)