3 * Resource loader module for generated and embedded images.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Trevor Parscal
25 * Resource loader module for generated and embedded images.
29 class ResourceLoaderImageModule
extends ResourceLoaderModule
{
32 * Local base path, see __construct()
35 protected $localBasePath = '';
37 protected $origin = self
::ORIGIN_CORE_SITEWIDE
;
39 protected $images = array();
40 protected $variants = array();
41 protected $prefix = array();
42 protected $targets = array( 'desktop', 'mobile' );
45 * Constructs a new module from an options array.
47 * @param array $options List of options; if not given or empty, an empty module will be
49 * @param string $localBasePath Base path to prepend to all local paths in $options. Defaults
52 * Below is a description for the $options array:
53 * @par Construction options:
56 * // Base path to prepend to all local paths in $options. Defaults to $IP
57 * 'localBasePath' => [base path],
58 * // CSS class prefix to use in all style rules
59 * 'prefix' => [CSS class prefix],
60 * // List of variants that may be used for the image files
61 * 'variants' => array(
62 * // ([image type] is a string, used in generated CSS class
63 * // names and to match variants to images)
64 * [image type] => array(
65 * [variant name] => array(
66 * 'color' => [color string, e.g. '#ffff00'],
67 * 'global' => [boolean, if true, this variant is available
68 * for all images of this type],
72 * // List of image files and their options
74 * [image type] => array(
76 * [file path string] => array(
77 * 'name' => [image name string, defaults to file name],
78 * 'variants' => [array of variant name strings, variants
79 * available for this image],
87 public function __construct( $options = array(), $localBasePath = null ) {
88 $this->localBasePath
= self
::extractLocalBasePath( $options, $localBasePath );
90 if ( !isset( $options['prefix'] ) ||
!$options['prefix'] ) {
91 throw new MWException(
92 "Required 'prefix' option not given or empty."
96 foreach ( $options as $member => $option ) {
99 if ( !is_array( $option ) ) {
100 throw new MWException(
101 "Invalid collated file path list error. '$option' given, array expected."
104 foreach ( $option as $key => $value ) {
105 if ( !is_string( $key ) ) {
106 throw new MWException(
107 "Invalid collated file path list key error. '$key' given, string expected."
110 $this->{$member}[$key] = (array)$value;
115 if ( !is_array( $option ) ) {
116 throw new MWException(
117 "Invalid variant list error. '$option' given, array expected."
120 $this->{$member} = $option;
124 $this->{$member} = (string)$option;
131 * Get CSS class prefix used by this module.
134 public function getPrefix() {
135 return $this->prefix
;
139 * Get a ResourceLoaderImage object for given image.
140 * @param string $name Image name
141 * @return ResourceLoaderImage|null
143 public function getImage( $name ) {
144 $images = $this->getImages();
145 return isset( $images[$name] ) ?
$images[$name] : null;
149 * Get ResourceLoaderImage objects for all images.
150 * @return ResourceLoaderImage[] Array keyed by image name
152 public function getImages() {
153 if ( !isset( $this->imageObjects
) ) {
154 $this->imageObjects
= array();
156 foreach ( $this->images
as $type => $list ) {
157 foreach ( $list as $name => $options ) {
158 $imageDesc = is_string( $options ) ?
$options : $options['image'];
160 $allowedVariants = array_merge(
161 isset( $options['variants'] ) ?
$options['variants'] : array(),
162 $this->getGlobalVariants( $type )
164 if ( isset( $this->variants
[$type] ) ) {
165 $variantConfig = array_intersect_key(
166 $this->variants
[$type],
167 array_fill_keys( $allowedVariants, true )
170 $variantConfig = array();
173 $image = new ResourceLoaderImage(
177 $this->localBasePath
,
180 $this->imageObjects
[ $image->getName() ] = $image;
185 return $this->imageObjects
;
189 * Get list of variants in this module that are 'global' for given type of images, i.e., available
190 * for every image of given type regardless of image options.
191 * @param string $type Image type
194 public function getGlobalVariants( $type ) {
195 if ( !isset( $this->globalVariants
[$type] ) ) {
196 $this->globalVariants
[$type] = array();
198 if ( isset( $this->variants
[$type] ) ) {
199 foreach ( $this->variants
[$type] as $name => $config ) {
200 if ( isset( $config['global'] ) && $config['global'] ) {
201 $this->globalVariants
[$type][] = $name;
207 return $this->globalVariants
[$type];
211 * Get the type of given image.
212 * @param string $imageName Image name
215 public function getImageType( $imageName ) {
216 foreach ( $this->images
as $type => $list ) {
217 foreach ( $list as $key => $value ) {
218 $file = is_int( $key ) ?
$value : $key;
219 $options = is_array( $value ) ?
$value : array();
220 $name = isset( $options['name'] ) ?
$options['name'] : pathinfo( $file, PATHINFO_FILENAME
);
221 if ( $name === $imageName ) {
229 * @param ResourceLoaderContext $context
232 public function getStyles( ResourceLoaderContext
$context ) {
235 $script = $context->getResourceLoader()->getLoadScript( $this->getSource() );
236 $prefix = $this->getPrefix();
238 foreach ( $this->getImages() as $name => $image ) {
239 $type = $this->getImageType( $name );
241 $declarations = $this->getCssDeclarations(
242 $image->getDataUri( $context, null, 'original' ),
243 $image->getUrl( $context, $script, null, 'rasterized' )
245 $declarations = implode( "\n\t", $declarations );
246 $rules[] = ".$prefix-$type-$name {\n\t$declarations\n}";
248 // TODO: Get variant configurations from $context->getSkin()
249 foreach ( $image->getVariants() as $variant ) {
250 $declarations = $this->getCssDeclarations(
251 $image->getDataUri( $context, $variant, 'original' ),
252 $image->getUrl( $context, $script, $variant, 'rasterized' )
254 $declarations = implode( "\n\t", $declarations );
255 $rules[] = ".$prefix-$type-$name-$variant {\n\t$declarations\n}";
259 $style = implode( "\n", $rules );
260 if ( $this->getFlip( $context ) ) {
261 $style = CSSJanus
::transform( $style, true, false );
263 return array( 'all' => $style );
267 * @param string $primary Primary URI
268 * @param string $fallback Fallback URI
269 * @return string[] CSS declarations to use given URIs as background-image
271 protected function getCssDeclarations( $primary, $fallback ) {
272 // SVG support using a transparent gradient to guarantee cross-browser
273 // compatibility (browsers able to understand gradient syntax support also SVG).
274 // http://pauginer.tumblr.com/post/36614680636/invisible-gradient-technique
276 "background-image: url($fallback);",
277 "background-image: -webkit-linear-gradient(transparent, transparent), url($primary);",
278 "background-image: linear-gradient(transparent, transparent), url($primary);",
285 public function supportsURLLoading() {
290 * Extract a local base path from module definition information.
292 * @param array $options Module definition
293 * @param string $localBasePath Path to use if not provided in module definition. Defaults
295 * @return string Local base path
297 public static function extractLocalBasePath( $options, $localBasePath = null ) {
300 if ( $localBasePath === null ) {
301 $localBasePath = $IP;
304 if ( array_key_exists( 'localBasePath', $options ) ) {
305 $localBasePath = (string)$options['localBasePath'];
308 return $localBasePath;