Search code examples
htmlcssantialiasing

Prevent browser anti-aliasing when upscaling?


With Google's Chart Image API (which was unfortunately deprecated April 2012), you can generate QR codes.

QR code

I'm sure I can change the size of the resulting image using the API, but I'd rather just use CSS and the width and height properties to make it a little bigger.
In Chrome at least, this results in nasty anti-aliasing (not preferable, since it needs to be parsable by machines). Is there a way to tell browsers not to anti-alias upscaled images?


Solution

  • This guy has the solution: http://nullsleep.tumblr.com/post/16417178705/how-to-disable-image-smoothing-in-modern-web-browsers

    img { 
        image-rendering: optimizeSpeed;             /* PREFER SPEED OVER SMOOTHING  */
        image-rendering: -moz-crisp-edges;          /* Firefox                        */
        image-rendering: -o-crisp-edges;            /* Opera                          */
        image-rendering: -webkit-optimize-contrast; /* Chrome (and eventually Safari) */
        image-rendering: optimize-contrast;         /* CSS3 Proposed                  */
        -ms-interpolation-mode: nearest-neighbor;   /* IE8+                           */
      }
    

    Sooo for you:

    imgQR { 
        width:100px;
        height:100px;
        image-rendering: optimizeSpeed;             /* PREFER SPEED OVER SMOOTHING  */
        image-rendering: -moz-crisp-edges;          /* Firefox                        */
        image-rendering: -o-crisp-edges;            /* Opera                          */
        image-rendering: -webkit-optimize-contrast; /* Chrome (and eventually Safari) */
        image-rendering: optimize-contrast;         /* CSS3 Proposed                  */
        -ms-interpolation-mode: nearest-neighbor;   /* IE8+                           */
      }