0

I'm trying to show jpg in browsers that don't support webp without using a plugin,

When uploading images, I upload both as webp and jpg.

İ add webp by default, I need to change the extension to .jpg when entered from a browser that does not support webp, i found a php code about it but my attempts were unsuccessful.

<?php
if (strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) !== false) {
  //do nothing when webp is supported
} else {
  //.webp replace .jpg
}
?>
1
  • Your not equal is incorrect. != is NOT EQUAL, where as == is EQUAL and === is identical, meaning if you try to match 1 (string) to 1 (integer) the === will return false, but if you use == it will return true. w3schools.com/php/php_operators.asp Feb 7 at 14:20

1 Answer 1

0

I think the best way to filter out the browsers for supporting webp is to do it on client-side width html element or Javascript, not with php on the server-side.

If you want to do it by php you need to look at:

$_SERVER['HTTP_USER_AGENT']

That will contain the User-Agent string for the browser. Beware that almost all browsers claim to be "Mozilla" for compatibility reasons - you need to look for specific text for each browser within that header, eg. "MSIE" for Internet Explorer.

Examples:

My Firefox calls itself Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 4.0.20506)

My IE7 calls itself Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; User-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; http://bsalsa.com) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 4.0.20506)

or use get_browser() php function.

Example from Manual:

echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";
$browser = get_browser(null, true);
print_r($browser);

Will return:

Array
(
    [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
    [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*/
    [parent] => Firefox 0.9
    [platform] => WinXP
    [browser] => Firefox
    [version] => 0.9
    [majorver] => 0
    [minorver] => 9
    [cssversion] => 2
    [frames] => 1
    [iframes] => 1
    [tables] => 1
    [cookies] => 1
    [backgroundsounds] =>
    [vbscript] =>
    [javascript] => 1
    [javaapplets] => 1
    [activexcontrols] =>
    [cdf] =>
    [aol] =>
    [beta] => 1
    [win16] =>
    [crawler] =>
    [stripper] =>
    [wap] =>
    [netclr] =>
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.