default
menu home
Icon library Sign up Login favorite_border message help_outline
Online notes - online bookmark

A PHP method for determining whether a browser supports AVIF images

styleTags: All CSS html javascript php/other IT
by: fanshome  2023-06-14 02:49(UTC)

The latest generation of image compression format AVIF is very good, the effect of reducing the size of the picture is very obvious, and the new version of the browser of major companies has been supported.

But, but! Microsoft's Edge so far (2023-06) does not support AVIF, only WebP, so what to do?

It is not advisable to directly determine whether the browser is edge, because there is more than one browser that does not support avif, and a more scientific and trouble-saving approach is to determine whether the accept in the browser request header accepts avif.

Let's take PHP as an example:

php/othercontent_copyCopy codes
<?php
	$AVIF = (preg_match ("/image\/avif/", $_SERVER['HTTP_ACCEPT'] )) ? true : false;
	$WEBP = (preg_match ("/image\/webp/", $_SERVER['HTTP_ACCEPT'] )) ? true : false;
?>

If the browser request header accepts an AVIF or WebP image, it will have the words "image/avif, image/webp", so it is more scientific to let the browser tell us whether it supports a certain image format.

With judgment, you know what to do.

You can find practical examples on this page , if the browser supports avif images, then the format of the first large image on the page will be avif; If it is a browser such as Microsoft's Edge that does not support AVIF format images, then it will be replaced by WebP format images.

visibility 715


- for heavy web user Online notes
adimg
logo Post a comment

captcha
Please check the captcha code
Cancel