We can make use of PHP global variable $_SERVER to do so.
Here’s another related post about PHP global variable $_SERVER.
Using $_SERVER[‘HTTP_ACCEPT’] we can get the detailed contents of the Accept: header, if there is one available.
Then we can use PHP strpos() function to check if it has image/webp in it. If so, it’s supported.
Here’s how we can do that:
if( strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) === true ) {
// webp is supported!
}
Just change the true to false if you want to check if webp is not supported OR just use else in the previous example.
if( strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) === false ) {
// webp is supported!
}
Use this code to check if webp is supported or the browser is chrome
if( strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) == true || strpos( $_SERVER['HTTP_USER_AGENT'], ' Chrome/' ) == true ) {
// webp is supported!
}
Credit: Stack Overflow
browsers detection images server WebP