default
menu home
图标库 注册 登录 favorite_border message help_outline
在线笔记 - 人性化网络收藏夹

A PHP method for determining whether a browser supports AVIF images

style分类标签: 全部 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_copy复制代码
<?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 620


- 重度网络用户必备 在线笔记
adimg
logo 发表评论

captcha
请正确填写验证码
取 消