Skip to content
PHP Logo

How can PHP be used to dynamically adjust image sizes based on user screen resolution?

To dynamically adjust image sizes based on user screen resolution using PHP, you can detect the user's screen resolution using JavaScript, pass this information to a PHP script via AJAX, and then use PHP to generate the appropriate image sizes based on the received screen resolution.

php
<?php
// Get the user's screen resolution from JavaScript
$screenResolution = $_POST['screenResolution'];

// Determine the appropriate image sizes based on the screen resolution
if ($screenResolution == 'low') {
    $imageSize = 'small.jpg';
} elseif ($screenResolution == 'medium') {
    $imageSize = 'medium.jpg';
} else {
    $imageSize = 'large.jpg';
}

// Output the image tag with the dynamically adjusted image size
echo '<img src="' . $imageSize . '" alt="Dynamic Image">';
?>