Quantcast
Channel: Live News for Yii Framework
Viewing all articles
Browse latest Browse all 2941

[Wiki] SVG/PNG CHtml:image replacement to provide SVG when the browser supports it.

$
0
0

Modern browsers support SVG, older browsers do not. This replacement for CHtml::image() allows you to handle to propose SVG in an efficient way with PNG as a fallback.

For each 'svg' you have, you need to provide the 'png' alternative in the same location.

(This could be optimised further if the browser is known to not support SVG, but that has not been tested)

class YHtml extends CHtml {
    private static $svgAsImg;
    private static $jsSvgAsImg=false;
    /**
     * Generates an image tag.
         * 
         * If the image is 'svg', checks the browser compatibility (cookie)
         * and adds 'png' fallback if the browser is not known to support 'svg'.
     *
     * @param string $src the image URL
     * @param string $alt the alternative text display
     * @param array $htmlOptions additional HTML attributes (see {@link tag}).
resizing.
     * @return string the generated image tag
     */
    public static function image($src,$alt='',$htmlOptions=array())
    {
        if(!isset(self::$svgAsImg)) {
                if(isset($_COOKIE['svgasimg'])) { // Can't use cookie manager, this is an unsafe cookie
                    self::$svgAsImg=($_COOKIE['svgasimg']==='true');
                } else {
                    self::$svgAsImg=false;
                }
        }
        if(!self::$svgAsImg&&!self::$jsSvgAsImg) {
            Yii::app()->clientScript->registerCoreScript('jquery');
                Yii::app()->clientScript->registerScript('svgasimg',
'jQuery.cookie("svgasimg",document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image", "1.1"));');
                self::$jsSvgAsImg=true;
        }
        if(substr($src,-4)==='.svg') {
            if(!self::$svgAsImg) {
                // svg file, add alternative for browsers that do not support SVG.
                // See http://coding.smashingmagazine.com/2013/06/02/clown-car-technique-solving-for-adaptive-images-in-responsive-web-design/
                $imgHtmlOptions=$htmlOptions;
                //Next line is ok when method for converting svg on server side.
                //$imgHtmlOptions['src']=Yii::app()->createUrl('image/png',array('src'=>$src));
                $imgHtmlOptions['src']=substr($src,0,strlen($src)-4).'.png';
                $imgHtmlOptions['alt']=$alt;
 
                $img="<!--[if lte IE 8]>".self::tag('img',$imgHtmlOptions)."<![endif]-->";
                $htmlOptions['data']=$src;
                //$htmlOptions['alt']=$alt;
                $htmlOptions['type']='image/svg+xml';
                return self::tag('object',$htmlOptions,$img);
            } else {
                $htmlOptions['src']=$src;
                $htmlOptions['alt']=$alt;
                return self::tag('img',$htmlOptions);
            }
        }
        $htmlOptions['src']=$src;
        $htmlOptions['alt']=$alt;
        return self::tag('img',$htmlOptions);
    }

Viewing all articles
Browse latest Browse all 2941

Trending Articles