Created
May 11, 2012 18:42
-
-
Save moriartiegist/2661619 to your computer and use it in GitHub Desktop.
PHP: responsive image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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