Here is an outputfilter for smarty which replaces image url inside the smarty templates with your cdn url:
ie. an image like:
will be replaced to
Added after 4 minutes:
same will work with css and javascripts:
PHP Code:
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: outputfilter
* Name: cdn
* Author: GUMSLONE
* Install: Drop into the plugin directory, call
* $smarty->loadFilter('output','cdn');
* from application.
* -------------------------------------------------------------
*/
function smarty_outputfilter_cdn($tpl_source, &$smarty)
{
if($_SERVER["SERVER_PORT"] == 80) {
$tpl_source = preg_replace('/("|\')((\.\/|\/)[A-z0-9_\-\/@\.]+\.(jpg|jpeg|gif|png))("|\')/i', "\"http://cdn.yoursite.com\\2\"", $tpl_source);
}
return $tpl_source;
}
?>
HTML Code:
<img src="/images/image.gif"/>
HTML Code:
<img src="http://cdn.yoursite.com/images/image.gif"/>
same will work with css and javascripts:
PHP Code:
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: outputfilter
* Name: cdn
* Author: GUMSLONE
* Install: Drop into the plugin directory, call
* $smarty->loadFilter('output','cdn');
* from application.
* -------------------------------------------------------------
*/
function smarty_outputfilter_cdn($tpl_source, &$smarty)
{
if($_SERVER["SERVER_PORT"] == 80) {
$tpl_source = preg_replace('/("|\')((\.\/|\/)[A-z0-9][A-z0-9_\-\/@\.]+\.(jpg|jpeg|gif|png|js|css))("|\')/i', "\"http://cdn.yoursite.com\\2\"", $tpl_source);
}
return $tpl_source;
}
?>
Comment