smarty cdn outputfilter

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • smarty cdn outputfilter

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

    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;
    }
    ?>
    ie. an image like:
    HTML Code:
    <img src="/images/image.gif"/>
    will be replaced to
    HTML Code:
    <img src="http://cdn.yoursite.com/images/image.gif"/>
    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][A-z0-9_\-\/@\.]+\.(jpg|jpeg|gif|png|js|css))("|\')/i'"\"http://cdn.yoursite.com\\2\""$tpl_source);
        } 
         
        return 
    $tpl_source

    ?>
    Last edited by GumSlone; 24.02.13, 18:53.

  • #2
    Regular is expressions are very fast, but uses a lot of resources. especially for all image tags..

    Comment


    • #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?

      Comment


      • #4
        Originally posted by GumSlone View Post
        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.

        Comment

        Working...
        X