How can PHP developers address compatibility issues with WebP images not displaying in certain browsers, such as Safari on Mac?
WebP images are not natively supported in Safari on Mac, so PHP developers can address compatibility issues by detecting the user agent and serving a fallback image format, such as JPEG or PNG, for Safari users. This can be achieved by checking the user agent string in PHP and conditionally serving a different image format based on the browser being used.
<?php
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($user_agent, 'Safari') !== false && strpos($user_agent, 'Macintosh') !== false) {
// Serve JPEG or PNG image as fallback for WebP
echo '<img src="fallback_image.jpg" alt="Fallback Image">';
} else {
// Serve WebP image for other browsers
echo '<img src="webp_image.webp" alt="WebP Image">';
}
?>