Skip to content

Instantly share code, notes, and snippets.

@moriartiegist
Created May 11, 2012 18:42
Show Gist options
  • Save moriartiegist/2661619 to your computer and use it in GitHub Desktop.
Save moriartiegist/2661619 to your computer and use it in GitHub Desktop.
PHP: responsive image
<?php
$_responsive = new Responsive;
echo $_responsive->get_cookie_code();
?>
<?=$_COOKIE['resolution'];?>
<?php
class Responsive
{
/**
* returns the code for setting resolution cookie
* @param integer $default_resolution fallback resolution if cookie not setable
* @return mixed code for setting cookie or null
*/
public function get_cookie_code($default_resolution = 1280)
{
if (!$_COOKIE['resolution'])
{
$_COOKIE['resolution'] = $default_resolution;
// workaround needed for android bug
$android = strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'android') !== false ? true : false;
return '<meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0"><script>if(navigator.cookieEnabled==true){'.($android ? 'window.setTimeout(function() {' : '').'document.cookie="resolution="+Math.max(screen.width,screen.height)+"; path=/";window.location="'.$_SERVER['REQUEST_URI'].'";'.($android ? '}, 1);' : '').'}</script>';
}
return null;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment