3 * Copyright 2010 Wikimedia Foundation
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12 * specific language governing permissions and limitations under the License.
18 * This class provides minification, URL remapping, URL extracting, and data-URL embedding.
21 * @version 0.1.1 -- 2010-09-11
22 * @author Trevor Parscal <tparscal@wikimedia.org>
23 * @copyright Copyright 2010 Wikimedia Foundation
24 * @license http://www.apache.org/licenses/LICENSE-2.0
31 * Maximum file size to still qualify for in-line embedding as a data-URI
33 * 24,576 is used because Internet Explorer has a 32,768 byte limit for data URIs, which when base64 encoded will
34 * result in a 1/3 increase in size.
36 const EMBED_SIZE_LIMIT
= 24576;
37 const URL_REGEX
= 'url\([\'"]?(?<file>[^\?\)\:\'"]*)\??[^\)\'"]*[\'"]?\)';
39 /* Protected Static Members */
41 /** @var array List of common image files extensions and mime-types */
42 protected static $mimeTypes = array(
44 'jpe' => 'image/jpeg',
45 'jpeg' => 'image/jpeg',
46 'jpg' => 'image/jpeg',
48 'tif' => 'image/tiff',
49 'tiff' => 'image/tiff',
50 'xbm' => 'image/x-xbitmap',
56 * Gets a list of local file paths which are referenced in a CSS style sheet
58 * @param $source string CSS data to remap
59 * @param $path string File path where the source was read from (optional)
60 * @return array List of local file references
62 public static function getLocalFileReferences( $source, $path = null ) {
64 if ( preg_match_all( '/' . self
::URL_REGEX
. '/', $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER
) ) {
65 foreach ( $matches as $match ) {
66 $file = ( isset( $path ) ?
rtrim( $path, '/' ) . '/' : '' ) . "{$match['file'][0]}";
68 // Only proceed if we can access the file
69 if ( !is_null( $path ) && file_exists( $file ) ) {
78 * Remaps CSS URL paths and automatically embeds data URIs for URL rules preceded by an /* @embed * / comment
80 * @param $source string CSS data to remap
81 * @param $local string File path where the source was read from
82 * @param $remote string URL path to the file
84 * @return string Remapped CSS data
86 public static function remap( $source, $local, $remote, $embed = true ) {
87 $pattern = '/((?<embed>\s*\/\*\s*\@embed\s*\*\/)(?<pre>[^\;\}]*))?' . self
::URL_REGEX
. '(?<post>[^;]*)[\;]?/';
89 while ( preg_match( $pattern, $source, $match, PREG_OFFSET_CAPTURE
, $offset ) ) {
91 $embed = $match['embed'][0];
92 $pre = $match['pre'][0];
93 $post = $match['post'][0];
94 $file = "{$local}/{$match['file'][0]}";
95 $url = "{$remote}/{$match['file'][0]}";
96 // Only proceed if we can access the file
97 if ( file_exists( $file ) ) {
98 // Add version parameter as a time-stamp in ISO 8601 format, using Z for the timezone, meaning GMT
99 $url .= '?' . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $file ), -2 ) );
100 // If we the mime-type can't be determined, no embedding will take place
102 $realpath = realpath( $file );
103 // Try a couple of different ways to get the mime-type of a file, in order of preference
104 if ( $realpath && function_exists( 'finfo_file' ) && function_exists( 'finfo_open' ) ) {
105 // As of PHP 5.3, this is how you get the mime-type of a file; it uses the Fileinfo PECL extension
106 $type = finfo_file( finfo_open( FILEINFO_MIME_TYPE
), $realpath );
107 } else if ( function_exists( 'mime_content_type' ) ) {
108 // Before this was deprecated in PHP 5.3, this used to be how you get the mime-type of a file
109 $type = mime_content_type( $file );
111 // Worst-case scenario has happend, use the file extension to infer the mime-type
112 $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION
) );
113 if ( isset( self
::$mimeTypes[$ext] ) ) {
114 $type = self
::$mimeTypes[$ext];
117 // Detect when URLs were preceeded with embed tags, and also verify file size is below the limit
118 if ( $embed && $type && $match['embed'][1] > 0 && filesize( $file ) < self
::EMBED_SIZE_LIMIT
) {
119 // Strip off any trailing = symbols (makes browsers freak out)
120 $data = base64_encode( file_get_contents( $file ) );
121 // Build 2 CSS properties; one which uses a base64 encoded data URI in place of the @embed
122 // comment to try and retain line-number integrity , and the other with a remapped an versioned
123 // URL and an Internet Explorer hack making it ignored in all browsers that support data URIs
124 $replacement = "{$pre}url(data:{$type};base64,{$data}){$post};{$pre}url({$url}){$post}!ie;";
126 // Build a CSS property with a remapped and versioned URL, preserving comment for debug mode
127 $replacement = "{$embed}{$pre}url({$url}){$post};";
130 // Perform replacement on the source
131 $source = substr_replace( $source, $replacement, $match[0][1], strlen( $match[0][0] ) );
132 // Move the offset to the end of the replacement in the source
133 $offset = $match[0][1] +
strlen( $replacement );
136 // Move the offset to the end of the match, leaving it alone
137 $offset = $match[0][1] +
strlen( $match[0][0] );
143 * Removes whitespace from CSS data
145 * @param $css string CSS data to minify
146 * @return string Minified CSS data
148 public static function minify( $css ) {
151 array( '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ),
152 array( ';', ':', '{', '{', ',', '}', '}' ),
153 preg_replace( array( '/\s+/', '/\/\*.*?\*\//s' ), array( ' ', '' ), $css )