Reputation: 40
I am using following code to convert my image into grey scale, on local machine it is fine but when I deploy the same on server it passes Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1 I wonder is it due to different domain, I am not sure but problem is due to "getImageData". Also, this problem is only on chrome.
I have read lot of posts and tried a lot, but seems like nothing working for me.
Thanks in Advance...
function grayscale(img) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var imgObj = new Image();
imgObj.src = img;
canvas.width = imgObj.width;
canvas.height = imgObj.height;
ctx.drawImage(imgObj, 0, 0);
var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
for (var y = 0; y < imgPixels.height; y++) {
for (var x = 0; x < imgPixels.width; x++) {
var i = (y * 4) * imgPixels.width + x * 4;
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
}
}
ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
return canvas.toDataURL();
}
Upvotes: 0
Views: 5018
Reputation: 1937
I just had similar issue in IE9, IE10.
I would suggest using, in your case ( creating image object dynamically ):
imgObj.naturalWidth;
imgObj.naturalHeight;
instead of
imgObj.width;
imgObj.height;
Upvotes: 0
Reputation: 540
Most likely you are not calling the script using window.load and rather using on DOM load. I ran into the same problem using the grayscale script. Here's the code that works for me:
$(window).load(function(){
imageColorize();
});
//Black and white images to color
function imageColorize(){
// Fade in images so there isn't a color "pop" document load and then on window load
$(".colorize img").fadeIn(500);
// clone image
$('.colorize img').each(function(){
var el = $(this);
el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
var el = $(this);
el.parent().css({"width":this.width,"height":this.height});
el.dequeue();
});
this.src = grayscale(this.src);
});
// Fade image
$('.colorize img').mouseover(function(){
$(this).parent().find('img:first').stop().animate({opacity:1}, 1000);
});
$('.colorize .img_grayscale').mouseout(function(){
$(this).stop().animate({opacity:0}, 1000);
});
}
// Grayscale w canvas method
function grayscale(src){
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var imgObj = new Image();
imgObj.src = src;
canvas.width = imgObj.width;
canvas.height = imgObj.height;
ctx.drawImage(imgObj, 0, 0);
var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
for(var y = 0; y < imgPixels.height; y++){
for(var x = 0; x < imgPixels.width; x++){
var i = (y * 4) * imgPixels.width + x * 4;
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
}
}
ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
return canvas.toDataURL();
}
Upvotes: 2
Reputation: 928
This works fine for me
// img is HTMLImageElement
function makeGreyScale(img) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var imgObj = new Image();
imgObj.src = img.src;
imgObj.onload = function(){
canvas.width = imgObj.width;
canvas.height = imgObj.height;
ctx.drawImage(imgObj, 0, 0);
var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
for(var y = 0; y < imgPixels.height; y++){
for(var x = 0; x < imgPixels.width; x++){
var i = (y * 4) * imgPixels.width + x * 4;
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
}
}
ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
img.src= canvas.toDataURL();
};
}
Upvotes: 3