content negotiation - displaying images in firefox

156 views Asked by At

I have a gallery of miniatures of images. After clicking on one of them, a full size equivalent opens in a new tab. Let's say I have many versions for the image .jpg,.gif and .png . How can I modify my file so that a proper (most wanted by the browser) file is displayed?

My firefox's preferences are:

user_pref("network.http.accept.default", "text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg;q=0.8, image/gif, image/x-xbitmap, */*;q=0.1");

and my .php file:

<?php 
    $source = $_GET['source'];
    echo '<img src="images/watermarked/'.$source.'" alt="File not found."></img>';

?>
1

There are 1 answers

0
kdani On

As it seems, your PHP file is useless, because you can do this logic in the gallery code as well.

Also, I don't see the point, why do you wan't to send the most wanted format. I would rather choose my format according to the content, instead the browser. JPEG files are perfect for photos and most paintings, PNG files are perfect for non-vector graphics, SVG with a PNG fallback for vectors and GIF for animations. I'd choose this way. Also, you should not send extremely large files, because some devices have limitations. An image below 3 megapixels seems to be OK in most cases.

Anyway, if you have access to those preferences based on request headers, you can split by comma, filter for your available file formats, and choose the first value in remainder array to get the desired value. If no values in array, you need to use a fallback.