Resizing an image in an HTML5 canvas

后端 未结 18 2671
半阙折子戏
半阙折子戏 2020-11-22 03:37

I\'m trying to create a thumbnail image on the client side using javascript and a canvas element, but when I shrink the image down, it looks terrible. It looks as if it was

相关标签:
18条回答
  • 2020-11-22 04:06

    If you're simply trying to resize an image, I'd recommend setting width and height of the image with CSS. Here's a quick example:

    .small-image {
        width: 100px;
        height: 100px;
    }
    

    Note that the height and width can also be set using JavaScript. Here's quick code sample:

    var img = document.getElement("my-image");
    img.style.width = 100 + "px";  // Make sure you add the "px" to the end,
    img.style.height = 100 + "px"; // otherwise you'll confuse IE
    

    Also, to ensure that the resized image looks good, add the following css rules to image selector:

    • -ms-interpolation-mode: bicubic: introduce in IE7
    • image-rendering: optimizeQuality: introduced in FireFox 3.6

    As far as I can tell, all browsers except IE using an bicubic algorithm to resize images by default, so your resized images should look good in Firefox and Chrome.

    If setting the css width and height doesn't work, you may want to play with a css transform:

    • -moz-transform: scale(sx[, sy])
    • -webkit-transform:scale(sx[, sy])

    If for whatever reason you need to use a canvas, please note that there are two ways an image can be resize: by resizing the canvas with css or by drawing the image at a smaller size.

    See this question for more details.

    0 讨论(0)
  • 2020-11-22 04:06

    Thanks @syockit for an awesome answer. however, I had to reformat a little as follows to make it work. Perhaps due to DOM scanning issues:

    $(document).ready(function () {
    
    $('img').on("load", clickA);
    function clickA() {
        var img = this;
        var canvas = document.createElement("canvas");
        new thumbnailer(canvas, img, 50, 3);
        document.body.appendChild(canvas);
    }
    
    function thumbnailer(elem, img, sx, lobes) {
        this.canvas = elem;
        elem.width = img.width;
        elem.height = img.height;
        elem.style.display = "none";
        this.ctx = elem.getContext("2d");
        this.ctx.drawImage(img, 0, 0);
        this.img = img;
        this.src = this.ctx.getImageData(0, 0, img.width, img.height);
        this.dest = {
            width: sx,
            height: Math.round(img.height * sx / img.width)
        };
        this.dest.data = new Array(this.dest.width * this.dest.height * 3);
        this.lanczos = lanczosCreate(lobes);
        this.ratio = img.width / sx;
        this.rcp_ratio = 2 / this.ratio;
        this.range2 = Math.ceil(this.ratio * lobes / 2);
        this.cacheLanc = {};
        this.center = {};
        this.icenter = {};
        setTimeout(process1, 0, this, 0);
    }
    
    //returns a function that calculates lanczos weight
    function lanczosCreate(lobes) {
        return function (x) {
            if (x > lobes)
                return 0;
            x *= Math.PI;
            if (Math.abs(x) < 1e-16)
                return 1
            var xx = x / lobes;
            return Math.sin(x) * Math.sin(xx) / x / xx;
        }
    }
    
    process1 = function (self, u) {
        self.center.x = (u + 0.5) * self.ratio;
        self.icenter.x = Math.floor(self.center.x);
        for (var v = 0; v < self.dest.height; v++) {
            self.center.y = (v + 0.5) * self.ratio;
            self.icenter.y = Math.floor(self.center.y);
            var a, r, g, b;
            a = r = g = b = 0;
            for (var i = self.icenter.x - self.range2; i <= self.icenter.x + self.range2; i++) {
                if (i < 0 || i >= self.src.width)
                    continue;
                var f_x = Math.floor(1000 * Math.abs(i - self.center.x));
                if (!self.cacheLanc[f_x])
                    self.cacheLanc[f_x] = {};
                for (var j = self.icenter.y - self.range2; j <= self.icenter.y + self.range2; j++) {
                    if (j < 0 || j >= self.src.height)
                        continue;
                    var f_y = Math.floor(1000 * Math.abs(j - self.center.y));
                    if (self.cacheLanc[f_x][f_y] == undefined)
                        self.cacheLanc[f_x][f_y] = self.lanczos(Math.sqrt(Math.pow(f_x * self.rcp_ratio, 2) + Math.pow(f_y * self.rcp_ratio, 2)) / 1000);
                    weight = self.cacheLanc[f_x][f_y];
                    if (weight > 0) {
                        var idx = (j * self.src.width + i) * 4;
                        a += weight;
                        r += weight * self.src.data[idx];
                        g += weight * self.src.data[idx + 1];
                        b += weight * self.src.data[idx + 2];
                    }
                }
            }
            var idx = (v * self.dest.width + u) * 3;
            self.dest.data[idx] = r / a;
            self.dest.data[idx + 1] = g / a;
            self.dest.data[idx + 2] = b / a;
        }
    
        if (++u < self.dest.width)
            setTimeout(process1, 0, self, u);
        else
            setTimeout(process2, 0, self);
    };
    
    process2 = function (self) {
        self.canvas.width = self.dest.width;
        self.canvas.height = self.dest.height;
        self.ctx.drawImage(self.img, 0, 0);
        self.src = self.ctx.getImageData(0, 0, self.dest.width, self.dest.height);
        var idx, idx2;
        for (var i = 0; i < self.dest.width; i++) {
            for (var j = 0; j < self.dest.height; j++) {
                idx = (j * self.dest.width + i) * 3;
                idx2 = (j * self.dest.width + i) * 4;
                self.src.data[idx2] = self.dest.data[idx];
                self.src.data[idx2 + 1] = self.dest.data[idx + 1];
                self.src.data[idx2 + 2] = self.dest.data[idx + 2];
            }
        }
        self.ctx.putImageData(self.src, 0, 0);
        self.canvas.style.display = "block";
    }
    });
    
    0 讨论(0)
  • 2020-11-22 04:06

    I just ran a page of side by sides comparisons and unless something has changed recently, I could see no better downsizing (scaling) using canvas vs. simple css. I tested in FF6 Mac OSX 10.7. Still slightly soft vs. the original.

    I did however stumble upon something that did make a huge difference and that was using image filters in browsers that support canvas. You can actually manipulate images much like you can in Photoshop with blur, sharpen, saturation, ripple, grayscale, etc.

    I then found an awesome jQuery plug-in which makes application of these filters a snap: http://codecanyon.net/item/jsmanipulate-jquery-image-manipulation-plugin/428234

    I simply apply the sharpen filter right after resizing the image which should give you the desired effect. I didn't even have to use a canvas element.

    0 讨论(0)
  • 2020-11-22 04:09

    So what do you do if all the browsers (actually, Chrome 5 gave me quite good one) won't give you good enough resampling quality? You implement them yourself then! Oh come on, we're entering the new age of Web 3.0, HTML5 compliant browsers, super optimized JIT javascript compilers, multi-core(†) machines, with tons of memory, what are you afraid of? Hey, there's the word java in javascript, so that should guarantee the performance, right? Behold, the thumbnail generating code:

    // returns a function that calculates lanczos weight
    function lanczosCreate(lobes) {
        return function(x) {
            if (x > lobes)
                return 0;
            x *= Math.PI;
            if (Math.abs(x) < 1e-16)
                return 1;
            var xx = x / lobes;
            return Math.sin(x) * Math.sin(xx) / x / xx;
        };
    }
    
    // elem: canvas element, img: image element, sx: scaled width, lobes: kernel radius
    function thumbnailer(elem, img, sx, lobes) {
        this.canvas = elem;
        elem.width = img.width;
        elem.height = img.height;
        elem.style.display = "none";
        this.ctx = elem.getContext("2d");
        this.ctx.drawImage(img, 0, 0);
        this.img = img;
        this.src = this.ctx.getImageData(0, 0, img.width, img.height);
        this.dest = {
            width : sx,
            height : Math.round(img.height * sx / img.width),
        };
        this.dest.data = new Array(this.dest.width * this.dest.height * 3);
        this.lanczos = lanczosCreate(lobes);
        this.ratio = img.width / sx;
        this.rcp_ratio = 2 / this.ratio;
        this.range2 = Math.ceil(this.ratio * lobes / 2);
        this.cacheLanc = {};
        this.center = {};
        this.icenter = {};
        setTimeout(this.process1, 0, this, 0);
    }
    
    thumbnailer.prototype.process1 = function(self, u) {
        self.center.x = (u + 0.5) * self.ratio;
        self.icenter.x = Math.floor(self.center.x);
        for (var v = 0; v < self.dest.height; v++) {
            self.center.y = (v + 0.5) * self.ratio;
            self.icenter.y = Math.floor(self.center.y);
            var a, r, g, b;
            a = r = g = b = 0;
            for (var i = self.icenter.x - self.range2; i <= self.icenter.x + self.range2; i++) {
                if (i < 0 || i >= self.src.width)
                    continue;
                var f_x = Math.floor(1000 * Math.abs(i - self.center.x));
                if (!self.cacheLanc[f_x])
                    self.cacheLanc[f_x] = {};
                for (var j = self.icenter.y - self.range2; j <= self.icenter.y + self.range2; j++) {
                    if (j < 0 || j >= self.src.height)
                        continue;
                    var f_y = Math.floor(1000 * Math.abs(j - self.center.y));
                    if (self.cacheLanc[f_x][f_y] == undefined)
                        self.cacheLanc[f_x][f_y] = self.lanczos(Math.sqrt(Math.pow(f_x * self.rcp_ratio, 2)
                                + Math.pow(f_y * self.rcp_ratio, 2)) / 1000);
                    weight = self.cacheLanc[f_x][f_y];
                    if (weight > 0) {
                        var idx = (j * self.src.width + i) * 4;
                        a += weight;
                        r += weight * self.src.data[idx];
                        g += weight * self.src.data[idx + 1];
                        b += weight * self.src.data[idx + 2];
                    }
                }
            }
            var idx = (v * self.dest.width + u) * 3;
            self.dest.data[idx] = r / a;
            self.dest.data[idx + 1] = g / a;
            self.dest.data[idx + 2] = b / a;
        }
    
        if (++u < self.dest.width)
            setTimeout(self.process1, 0, self, u);
        else
            setTimeout(self.process2, 0, self);
    };
    thumbnailer.prototype.process2 = function(self) {
        self.canvas.width = self.dest.width;
        self.canvas.height = self.dest.height;
        self.ctx.drawImage(self.img, 0, 0, self.dest.width, self.dest.height);
        self.src = self.ctx.getImageData(0, 0, self.dest.width, self.dest.height);
        var idx, idx2;
        for (var i = 0; i < self.dest.width; i++) {
            for (var j = 0; j < self.dest.height; j++) {
                idx = (j * self.dest.width + i) * 3;
                idx2 = (j * self.dest.width + i) * 4;
                self.src.data[idx2] = self.dest.data[idx];
                self.src.data[idx2 + 1] = self.dest.data[idx + 1];
                self.src.data[idx2 + 2] = self.dest.data[idx + 2];
            }
        }
        self.ctx.putImageData(self.src, 0, 0);
        self.canvas.style.display = "block";
    };
    

    ...with which you can produce results like these!

    img717.imageshack.us/img717/8910/lanczos358.png

    so anyway, here is a 'fixed' version of your example:

    img.onload = function() {
        var canvas = document.createElement("canvas");
        new thumbnailer(canvas, img, 188, 3); //this produces lanczos3
        // but feel free to raise it up to 8. Your client will appreciate
        // that the program makes full use of his machine.
        document.body.appendChild(canvas);
    };
    

    Now it's time to pit your best browsers out there and see which one will least likely increase your client's blood pressure!

    Umm, where's my sarcasm tag?

    (since many parts of the code is based on Anrieff Gallery Generator is it also covered under GPL2? I dunno)

    actually due to limitation of javascript, multi-core is not supported.

    0 讨论(0)
  • 2020-11-22 04:09

    I have a feeling the module I wrote will produce similar results to photoshop, as it preserves color data by averaging them, not applying an algorithm. It's kind of slow, but to me it is the best, because it preserves all the color data.

    https://github.com/danschumann/limby-resize/blob/master/lib/canvas_resize.js

    It doesn't take the nearest neighbor and drop other pixels, or sample a group and take a random average. It takes the exact proportion each source pixel should output into the destination pixel. The average pixel color in the source will be the average pixel color in the destination, which these other formulas, I think they will not be.

    an example of how to use is at the bottom of https://github.com/danschumann/limby-resize

    UPDATE OCT 2018: These days my example is more academic than anything else. Webgl is pretty much 100%, so you'd be better off resizing with that to produce similar results, but faster. PICA.js does this, I believe. –

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

    I converted @syockit's answer as well as the step-down approach into a reusable Angular service for anyone who's interested: https://gist.github.com/fisch0920/37bac5e741eaec60e983

    I included both solutions because they both have their own pros / cons. The lanczos convolution approach is higher quality at the cost of being slower, whereas the step-wise downscaling approach produces reasonably antialiased results and is significantly faster.

    Example usage:

    angular.module('demo').controller('ExampleCtrl', function (imageService) {
      // EXAMPLE USAGE
      // NOTE: it's bad practice to access the DOM inside a controller, 
      // but this is just to show the example usage.
    
      // resize by lanczos-sinc filter
      imageService.resize($('#myimg')[0], 256, 256)
        .then(function (resizedImage) {
          // do something with resized image
        })
    
      // resize by stepping down image size in increments of 2x
      imageService.resizeStep($('#myimg')[0], 256, 256)
        .then(function (resizedImage) {
          // do something with resized image
        })
    })
    
    0 讨论(0)
提交回复
热议问题