php - Detect if browser supports WebP format? (server side) -


there thread detecting webp support using client-side. how detect webp support using server side?

today, should check accept header image/webp. browsers support webp send part of accept string requests (images , non-images). in short:

if( strpos( $_server['http_accept'], 'image/webp' ) !== false ) {     // webp supported! } 

(you might want use preg_match instead , add word boundary checks , case insensitivity, in real world should fine)


here's original answer several years ago, when above not reliable

the "proper" way check accept header sent, bug in chrome means won't list image/webp though support it.

this relevant forum thread: https://groups.google.com/a/webmproject.org/forum/#!topic/webp-discuss/6nyupcsaors

which links bugtracker: https://code.google.com/p/chromium/issues/detail?id=169182 in turn links one: https://code.google.com/p/chromium/issues/detail?id=267212

end result? while isn't implemented yet, google chrome explicitly list image/webp accepted type both image and non-image requests. script serves html can check that. opera sends image/webp part of standard accept header (again regardless of whether image request or not).

so, check so:

if( strpos( $_server['http_accept'], 'image/webp' ) !== false || strpos( $_server['http_user_agent'], ' chrome/' ) !== false ) {     // webp supported! } 

(you might want use preg_match instead , add word boundary checks , case insensitivity, in real world should fine. might want check @ least version 6 of chrome, there's pretty nobody running out-of-date version there's not lot of point)


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -