YogeshChauhan . com

How to detect if browser supports WebP format on server side PHP script?

in PHP on April 25, 2020

We can make use of PHP global variable $_SERVER to do so.

Here’s another related post about PHP global variable $_SERVER.

How to get the full URL of current page in PHP?

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


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

The flex-grow, flex-shrink and flex-basis Properties in CSSCSSHow to create a vertically rotating texts using CSS?CSSPHP arrays for US states’ full names and abbreviationsPHPIntroduction to components and templates Part 4: Pipes and DirectivesAngularALTER DATABASE in PostgreSQLPostgresHow to get the first element with a class name xyz using JavaScript?JavaScriptHow to center an image horizontally and vertically?CSSCommon Table Expressions (CTE) in PostgreSQLPostgresWhat is the best way to add JavaScript Code into HTML?HTMLSQL GROUP BY StatementSQL/MySQLHow to select an element using its ID without the high specificity of the ID selector?CSSWhat are Class Constants in PHP?PHP