Convert an image to grayscale in HTML/CSS

前端 未结 25 1449
青春惊慌失措
青春惊慌失措 2020-11-22 10:22

Is there a simple way to display a color bitmap in grayscale with just HTML/CSS?

It doesn\'t need to be IE-compatible (and I imagine it won\'t be) -- if

相关标签:
25条回答
  • 2020-11-22 10:34

    Doesn't look like it's possible (yet), even with CSS3 or proprietary -webkit- or -moz- CSS properties.

    However, I did find this post from last June that used SVG filters on HTML. Not available in any current browser (the demo hinted at a custom WebKit build), but very impressive as a proof of concept.

    0 讨论(0)
  • 2020-11-22 10:34

    You don't need use so many prefixes for full use, because if you choose prefix for old firefox, you don't need use prefix for new firefox.

    So for full use, enough use this code:

    img.grayscale {
        filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\'/></filter></svg>#grayscale"); /* Firefox 10+, Firefox on Android */
        filter: gray; /* IE6-9 */
        -webkit-filter: grayscale(100%); /* Chrome 19+, Safari 6+, Safari 6+ iOS */
    }
    
    img.grayscale.disabled {
        filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'1 0 0 0 0, 0 1 0 0 0, 0 0 1 0 0, 0 0 0 1 0\'/></filter></svg>#grayscale");
        filter: none;
        -webkit-filter: grayscale(0%);
    }
    
    0 讨论(0)
  • 2020-11-22 10:37

    If you, or someone else facing a similar problem in future are open to PHP. (I know you said HTML/CSS, but maybe you are already using PHP in the backend) Here is a PHP solution:

    I got it from the PHP GD library and added some variable to automate the process...

    <?php
    $img = @imagecreatefromgif("php.gif");
    
    if ($img) $img_height = imagesy($img);
    if ($img) $img_width = imagesx($img);
    
    // Create image instances
    $dest = imagecreatefromgif('php.gif');
    $src = imagecreatefromgif('php.gif');
    
    // Copy and merge - Gray = 20%
    imagecopymergegray($dest, $src, 0, 0, 0, 0, $img_width, $img_height, 20);
    
    // Output and free from memory
    header('Content-Type: image/gif');
    imagegif($dest);
    
    imagedestroy($dest);
    imagedestroy($src);
    
    ?>
    
    0 讨论(0)
  • 2020-11-22 10:39

    Support for CSS filters has landed in Webkit. So we now have a cross-browser solution.

    img {
      filter: gray; /* IE6-9 */
      -webkit-filter: grayscale(1); /* Google Chrome, Safari 6+ & Opera 15+ */
      filter: grayscale(1); /* Microsoft Edge and Firefox 35+ */
    }
    
    /* Disable grayscale on hover */
    img:hover {
      -webkit-filter: grayscale(0);
      filter: none;
    }
    <img src="http://lorempixel.com/400/200/">


    What about Internet Explorer 10?

    You can use a polyfill like gray.

    0 讨论(0)
  • 2020-11-22 10:39

    If you are able to use JavaScript, then this script may be what you are looking for. It works cross browser and is working fine for me so far. You can't use it with images loaded from a different domain.

    http://james.padolsey.com/demos/grayscale/

    0 讨论(0)
  • 2020-11-22 10:39

    One terrible but workable solution: render the image using a Flash object, which then gives you all the transformations possible in Flash.

    If your users are using bleeding-edge browsers and if Firefox 3.5 and Safari 4 support it (I don't know that either do/will), you could adjust the CSS color-profile attribute of the image, setting it to a grayscale ICC profile URL. But that's a lot of if's!

    0 讨论(0)
提交回复
热议问题