3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 * This class provides minification, URL remapping, URL extracting, and data-URL embedding.
24 * @author Trevor Parscal
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;
38 /* Protected Static Members */
40 /** @var array List of common image files extensions and mime-types */
41 protected static $mimeTypes = array(
43 'jpe' => 'image/jpeg',
44 'jpeg' => 'image/jpeg',
45 'jpg' => 'image/jpeg',
47 'tif' => 'image/tiff',
48 'tiff' => 'image/tiff',
49 'xbm' => 'image/x-xbitmap',
55 * Gets a list of local file paths which are referenced in a CSS style sheet
57 * @param $source string CSS data to remap
58 * @param $path string File path where the source was read from (optional)
59 * @return array List of local file references
61 public static function getLocalFileReferences( $source, $path = null ) {
62 $pattern = '/url\([\'"]?(?<file>[^\?\)\:]*)\??[^\)]*[\'"]?\)/';
64 if ( preg_match_all( $pattern, $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 ( 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 $path string File path where the source was read from
82 * @return string Remapped CSS data
84 public static function remap( $source, $path, $embed = true ) {
85 $pattern = '/((?<embed>\s*\/\*\s*\@embed\s*\*\/)(?<rule>[^\;\}]*))?url\([\'"]?(?<file>[^\?\)\:]*)\??[^\)]*[\'"]?\)(?<extra>[^;]*)[\;]?/';
87 while ( preg_match( $pattern, $source, $match, PREG_OFFSET_CAPTURE
, $offset ) ) {
89 $embed = $match['embed'][0];
90 $rule = $match['rule'][0];
91 $extra = $match['extra'][0];
92 $file = "{$path}/{$match['file'][0]}";
93 // Only proceed if we can access the file
94 if ( file_exists( $file ) ) {
95 // Add version parameter as a time-stamp in ISO 8601 format, using Z for the timezone, meaning GMT
96 $url = "{$file}?" . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $file ), -2 ) );
97 // If we the mime-type can't be determined, no embedding will take place
99 // Try a couple of different ways to get the mime-type of a file, in order of preference
100 if ( function_exists( 'finfo_file' ) && function_exists( 'finfo_open' ) ) {
101 // As of PHP 5.3, this is how you get the mime-type of a file; it uses the Fileinfo PECL extension
102 $type = finfo_file( finfo_open( FILEINFO_MIME_TYPE
), $file );
103 } else if ( function_exists( 'mime_content_type' ) ) {
104 // Before this was deprecated in PHP 5.3, this used to be how you get the mime-type of a file
105 $type = mime_content_type( $file );
107 // Worst-case scenario has happend, use the file extension to infer the mime-type
108 $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION
) );
109 if ( isset( self
::$mimeTypes[$ext] ) ) {
110 $type = self
::$mimeTypes[$ext];
113 // Detect when URLs were preceeded with embed tags, and also verify file size is below the limit
114 if ( $embed && $type && $match['embed'][1] > 0 && filesize( $file ) < self
::EMBED_SIZE_LIMIT
) {
115 // Strip off any trailing = symbols (makes browsers freak out)
116 $data = base64_encode( file_get_contents( $file ) );
117 // Build 2 CSS properties; one which uses a base64 encoded data URI in place of the @embed
118 // comment to try and retain line-number integrity , and the other with a remapped an versioned
119 // URL and an Internet Explorer hack making it ignored in all browsers that support data URIs
120 $replacement = "{$rule}url(data:{$type};base64,{$data}){$extra};{$rule}url({$url}){$extra}!ie;";
122 // Build a CSS property with a remapped and versioned URL
123 $replacement = "{$embed}{$rule}url({$url}){$extra};";
126 // Perform replacement on the source
127 $source = substr_replace( $source, $replacement, $match[0][1], strlen( $match[0][0] ) );
128 // Move the offset to the end of the replacement in the source
129 $offset = $match[0][1] +
strlen( $replacement );
132 // Move the offset to the end of the match, leaving it alone
133 $offset = $match[0][1] +
strlen( $match[0][0] );
139 * Removes whitespace from CSS data
141 * @param $source string CSS data to minify
142 * @return string Minified CSS data
144 public static function minify( $css ) {
147 array( '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ),
148 array( ';', ':', '{', '{', ',', '}', '}' ),
149 preg_replace( array( '/\s+/', '/\/\*.*?\*\//s' ), array( ' ', '' ), $css )