Search code examples
phpgoogle-chromesvgresponsive-designuser-agent

Serve different images according to browser?


Chrome is rendering my SVG incorrectly, so I'd like to serve Chrome a PNG instead. The SVG looks beautiful in other modern browsers, especially Mobile Safari on iOS where users are likely to pinch to zoom in - so everyone else gets an SVG, but Chrome gets a PNG. How can I do this?


Solution

  • Read the user agent string and serve the content conditionally:

    <?php
    
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false)
    {
        ?>
            <img src="mycontent.png">
        <?php
    } else {
        ?>
            <svg>mycontent</svg>
        <?php
    }
    
    ?>