画像の空白を分析するには、私が知っている唯一の方法は、その画像を次のようにロードすることですcanvas
。
var img = new Image(),
$canvas = $("<canvas>"), // create an offscreen canvas
canvas = $canvas[0],
context = canvas.getContext("2d");
img.onload = function () {
context.drawImage(this, 0, 0); // put the image in the canvas
$("body").append($canvas);
removeBlanks(this.width, this.height);
};
// test image
img.src = 'http://images.productserve.com/preview/1302/218680281.jpg';
次に、getImageData()メソッドを使用します。このメソッドは、各ピクセル データ (色) の検査に使用できる ImageData オブジェクトを返します。
var removeBlanks = function (imgWidth, imgHeight) {
var imageData = context.getImageData(0, 0, canvas.width, canvas.height),
data = imageData.data,
getRBG = function(x, y) {
return {
red: data[(imgWidth*y + x) * 4],
green: data[(imgWidth*y + x) * 4 + 1],
blue: data[(imgWidth*y + x) * 4 + 2]
};
},
isWhite = function (rgb) {
return rgb.red == 255 && rgb.green == 255 && rgb.blue == 255;
},
scanY = function (fromTop) {
var offset = fromTop ? 1 : -1;
// loop through each row
for(var y = fromTop ? 0 : imgHeight - 1; fromTop ? (y < imgHeight) : (y > -1); y += offset) {
// loop through each column
for(var x = 0; x < imgWidth; x++) {
if (!isWhite(getRBG(x, y))) {
return y;
}
}
}
return null; // all image is white
},
scanX = function (fromLeft) {
var offset = fromLeft? 1 : -1;
// loop through each column
for(var x = fromLeft ? 0 : imgWidth - 1; fromLeft ? (x < imgWidth) : (x > -1); x += offset) {
// loop through each row
for(var y = 0; y < imgHeight; y++) {
if (!isWhite(getRBG(x, y))) {
return x;
}
}
}
return null; // all image is white
};
var cropTop = scanY(true),
cropBottom = scanY(false),
cropLeft = scanX(true),
cropRight = scanX(false);
// cropTop is the last topmost white row. Above this row all is white
// cropBottom is the last bottommost white row. Below this row all is white
// cropLeft is the last leftmost white column.
// cropRight is the last rightmost white column.
};
率直に言って、私は正当な理由でこのコードをテストできませんでした: 悪名高い " Unable to get image data from canvas because the canvas has been tainted by cross-origin data. " セキュリティ例外に遭遇しました。
これはバグではなく、意図された機能です。仕様から:
toDataURL()、toDataURLHD()、toBlob()、getImageData()、および getImageDataHD() メソッドはフラグをチェックし、クロスオリジン データをリークするのではなく、SecurityError 例外をスローします。
これは、 がdrawImage()
外部ドメインからファイルをロードしたときに発生します。これにより、キャンバスのorigin-cleanフラグが false に設定され、それ以上のデータ操作が妨げられます。
同じ問題が発生するのではないかと思いますが、コードは次のとおりです。
これがクライアント側で機能したとしても、パフォーマンスに関してどれほど惨めになるか想像できます。したがって、Jan が言ったように、画像をダウンロードしてサーバー側で前処理できれば、そのほうがよいでしょう。
編集:私のコードが実際に画像をトリミングするかどうかを知りたいと思っていましたが、実際にトリミングされています。

ここで確認できます
前述のように、ドメインの画像に対してのみ機能します。背景が白い独自の画像を選択して、最後の行を変更できます。
// define here an image from your domain
img.src = 'http://localhost/strawberry2.jpg';
明らかに、jsFiddle からではなく、ドメインからコードを実行する必要があります。
Edit2:同じ縦横比を維持するためにトリミングして拡大する場合は、これを変更します
var $croppedCanvas = $("<canvas>").attr({ width: cropWidth, height: cropHeight });
// finally crop the guy
$croppedCanvas[0].getContext("2d").drawImage(canvas,
cropLeft, cropTop, cropWidth, cropHeight,
0, 0, cropWidth, cropHeight);
に
var $croppedCanvas = $("<canvas>").attr({ width: imgWidth, height: imgHeight });
// finally crop the guy
$croppedCanvas[0].getContext("2d").drawImage(canvas,
cropLeft, cropTop, cropWidth, cropHeight,
0, 0, imgWidth, imgHeight);
Edit3:ブラウザーで画像をトリミングする 1 つの高速な方法は、この優れた記事で説明されているように、 Web Workersを使用してワークロードを並列化することです。