3 * PHP-provided functions for LESS; see docs for $wgResourceLoaderLESSFunctions
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
23 class ResourceLoaderLESSFunctions
{
25 * Check if an image file reference is suitable for embedding.
26 * An image is embeddable if it (a) exists, (b) has a suitable MIME-type,
27 * (c) does not exceed IE<9 size limit of 32kb. This is a LESS predicate
28 * function; it returns a LESS boolean value and can thus be used as a
33 * .background-image(@url) when(embeddable(@url)) {
34 * background-image: url(@url) !ie;
38 public static function embeddable( $frame, $less ) {
39 $base = pathinfo( $less->parser
->sourceName
, PATHINFO_DIRNAME
);
40 $url = trim( $less->compileValue( $frame ), '"\'' );
41 $file = realpath( $base . '/' . $url );
42 return $less->toBool( $file
43 && strpos( $url, '//' ) === false
44 && filesize( $file ) < CSSMin
::EMBED_SIZE_LIMIT
45 && CSSMin
::getMimeType( $file ) !== false );
49 * Convert an image URI to a base64-encoded data URI.
54 * background-image: embed('../images/button-bg.png');
58 public static function embed( $frame, $less ) {
59 $base = pathinfo( $less->parser
->sourceName
, PATHINFO_DIRNAME
);
60 $url = trim( $less->compileValue( $frame ), '"\'' );
61 $file = realpath( $base . '/' . $url );
63 $data = CSSMin
::encodeImageAsDataURI( $file );
64 $less->addParsedFile( $file );
65 return CSSMin
::buildUrlValue( $data );