chr*_*tfr 20 php retina-display
我发现了许多关于Retina Display的问题,但没有一个答案是在服务器端.
我想根据屏幕提供不同的图像,ex(在PHP中):
if( $is_retina)
$thumbnail = get_image( $item_photo, 'thumbnail_retina' ) ;
else
$thumbnail = get_image( $item_photo, 'thumbnail' ) ;
Run Code Online (Sandbox Code Playgroud)
你能看到解决这个问题的方法吗?
我只能想象在JavaScript中进行测试,设置Cookie.然而,这需要初始交换来设置它.谁有更好的解决方案?
Cookie设置代码:
(function(){
if( document.cookie.indexOf('device_pixel_ratio') == -1
&& 'devicePixelRatio' in window
&& window.devicePixelRatio == 2 ){
document.cookie = 'device_pixel_ratio=' + window.devicePixelRatio + ';';
window.location.reload();
}
})();
Run Code Online (Sandbox Code Playgroud)
chr*_*tfr 19
好吧,因为目前似乎没有更好的方法,这里是我的解决方案,结合了JS,PHP和Cookies.
我希望将来有更好的答案
<?php
if( isset($_COOKIE["device_pixel_ratio"]) ){
$is_retina = ( $_COOKIE["device_pixel_ratio"] >= 2 );
if( $is_retina)
$thumbnail = get_image( $item_photo, 'thumbnail_retina' ) ;
else
$thumbnail = get_image( $item_photo, 'thumbnail' ) ;
}else{
?>
<script language="javascript">
(function(){
if( document.cookie.indexOf('device_pixel_ratio') == -1
&& 'devicePixelRatio' in window
&& window.devicePixelRatio == 2 ){
var date = new Date();
date.setTime( date.getTime() + 3600000 );
document.cookie = 'device_pixel_ratio=' + window.devicePixelRatio + ';' + ' expires=' + date.toUTCString() +'; path=/';
//if cookies are not blocked, reload the page
if(document.cookie.indexOf('device_pixel_ratio') != -1) {
window.location.reload();
}
}
})();
</script>
<?php } ?>
Run Code Online (Sandbox Code Playgroud)
在function.php中:
add_action( 'init', 'CJG_retina' );
function CJG_retina(){
global $is_retina;
$is_retina = isset( $_COOKIE["device_pixel_ratio"] ) AND $_COOKIE["device_pixel_ratio"] >= 2;
}
Run Code Online (Sandbox Code Playgroud)
然后我可以使用以下GLOBAL:
global $is_retina;
要么 $GLOBALS['is_retina'];
因为你没有指定你需要的确切用例,我真的没有看到服务器的用例知道客户想要它的图像(我认为客户应该决定)这里是我的建议:
使用类似Retina.js的东西或使用srcset属性<img src="low-res.jpg" srcset="medium-res.jpg 1.5x, high-res.jpg 2x">
这样您还可以利用图像的浏览器缓存.如果您有两个不同图像尺寸的网址,则无法执行此操作.即使其自动创建/更新的图像缓存使用最后修改或etag标头.