Skip to content

Commit 0a9349a

Browse files
committed
Merge branch 'datauris'
Conflicts: lib/exe/css.php
2 parents bf1ec65 + 28f4004 commit 0a9349a

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

conf/dokuwiki.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
$conf['subscribe_time'] = 24*60*60; //Time after which digests / lists are sent (in sec, default 1 day)
109109
//Should be smaller than the time specified in recent_days
110110
$conf['compress'] = 1; //Strip whitespaces and comments from Styles and JavaScript? 1|0
111+
$conf['cssdatauri'] = 0; //Maximum byte size of small images to embed into CSS, won't work on IE<8
111112
$conf['hidepages'] = ''; //Regexp for pages to be skipped from RSS, Search and Recent Changes
112113
$conf['send404'] = 0; //Send a HTTP 404 status for non existing pages?
113114
$conf['sitemap'] = 0; //Create a google sitemap? How often? In days.

lib/exe/css.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ function css_out(){
123123
$css = css_compress($css);
124124
}
125125

126+
// embed small images right into the stylesheet
127+
if($conf['cssdatauri']){
128+
$base = preg_quote(DOKU_BASE,'#');
129+
$css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i','css_datauri',$css);
130+
}
131+
126132
http_cached_finish($cache->cache, $css);
127133
}
128134

@@ -221,11 +227,38 @@ function css_loadfile($file,$location=''){
221227
$css = io_readFile($file);
222228
if(!$location) return $css;
223229

224-
$css = preg_replace('#(url\([ \'"]*)(?!/|http://|https://| |\'|")#','\\1'.$location,$css);
225-
$css = preg_replace('#(@import\s+[\'"])(?!/|http://|https://)#', '\\1'.$location, $css);
230+
$css = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css);
231+
$css = preg_replace('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css);
232+
226233
return $css;
227234
}
228235

236+
/**
237+
* Converte local image URLs to data URLs if the filesize is small
238+
*
239+
* Callback for preg_replace_callback
240+
*/
241+
function css_datauri($match){
242+
global $conf;
243+
244+
$pre = unslash($match[1]);
245+
$base = unslash($match[2]);
246+
$url = unslash($match[3]);
247+
$ext = unslash($match[4]);
248+
249+
$local = DOKU_INC.$url;
250+
$size = @filesize($local);
251+
if($size && $size < $conf['cssdatauri']){
252+
$data = base64_encode(file_get_contents($local));
253+
}
254+
if($data){
255+
$url = 'data:image/'.$ext.';base64,'.$data;
256+
}else{
257+
$url = $base.$url;
258+
}
259+
return $pre.$url;
260+
}
261+
229262

230263
/**
231264
* Returns a list of possible Plugin Styles (no existance check here)

lib/plugins/config/lang/en/lang.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
$lang['subscribers'] = 'Enable page subscription support';
134134
$lang['subscribe_time'] = 'Time after which subscription lists and digests are sent (sec); This should be smaller than the time specified in recent_days.';
135135
$lang['compress'] = 'Compact CSS and javascript output';
136+
$lang['cssdatauri'] = 'Size in bytes up to which images referenced in CSS files should be embedded right into the stylesheet to reduce HTTP request header overhead. This technique won\'t work in IE < 8! <code>400</code> to <code>600</code> bytes is a good value. Set <code>0</code> to disable.';
136137
$lang['hidepages'] = 'Hide matching pages (regular expressions)';
137138
$lang['send404'] = 'Send "HTTP 404/Page Not Found" for non existing pages';
138139
$lang['sitemap'] = 'Generate Google sitemap (days)';

lib/plugins/config/settings/config.metadata.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
$meta['mailfrom'] = array('richemail');
180180
$meta['mailprefix'] = array('string');
181181
$meta['compress'] = array('onoff');
182+
$meta['cssdatauri'] = array('numeric','_pattern' => '/^\d+$/');
182183
$meta['gzip_output'] = array('onoff');
183184
$meta['hidepages'] = array('string');
184185
$meta['send404'] = array('onoff');

0 commit comments

Comments
 (0)