-3

Is there a way to detect if the device has a Retina display using PHP not JavaScript? I would like to deliver a different image according to the screen.

Bulat
  • 720
  • 7
  • 15
A.Madi
  • 11
  • 2
  • Post what you have tried? –  Apr 18 '18 at 07:01
  • @Bulat why did you mark it as a duplocate? You have added duplicate answer for javascript solution, when the question is actually asked about php solution. – Alexandr Nov 29 '19 at 09:52

1 Answers1

1

I tried the same thing with the combination of JS, PHP and Cookies.

Try the below code and check whether it's useful for you.

<?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 } ?>

in 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;
 }

Then after use the following GLOBAL:

global $is_retina; or $GLOBALS['is_retina'];