smarty cdn outputfilter

Started on the old forum in February 2013 · view the original in the archive · old links redirect here

3 replies · 620 views · started 13 years ago · 1 min read

2 participants
GumSlone Administrator Principal · 1,690 posts #1

Here is an outputfilter for smarty which replaces image url inside the smarty templates with your cdn url:

<?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;
}
?>

ie. an image like:

<img src="/images/image.gif"/>

will be replaced to

<img src="http://cdn.yoursite.com/images/image.gif"/>

Added after 4 minutes:

same will work with css and javascripts:

<?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; 
} 
?>

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

GumSlone Administrator Principal · 1,690 posts #3

so do you have another solution to replace all the image urls in many smarty templates to cdn url except of doing it manually?

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

antony2kx Developer · 94 posts #4

GumSlone wrote: so do you have another solution to replace all the image urls in many smarty templates to cdn url except of doing it manually?

no i dont, i was just telling you realistically what is. Sorry if i offended you.

By the way, one way of doing this, if your working with a website, not old featured phone site, is to use a javascript instead of php, reason being the work will be done on the client side instead. you can always create a fallback.

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close