IOS6がリリースされました。写真のアップロードをテストしています。IOS6とSafari写真のアップロード - ファイルAPI + Canvas + jQuery Ajax非同期にファイルをアップロードしてサイズ変更する
これはうまくいきますが、3Gを超える大きな画像では期待どおりに動きません。
File APIとCanvasのおかげで、JavaScriptを使用して画像のサイズを変更することができます。画像をアップロードしようとする前に画像のサイズを変更すると、アップロードが速くなり、ユーザーの利便性が向上します。スマートフォンプロセッサがネットワークの速度よりも指数関数的に速く改善されているので、私はこのソリューションが勝者だと考えています。しかし、私はjQueryのアヤックスでそれを実装する最も困難な時間を過ごしてい
:
ニコラスは、画像リサイズのための優れたソリューションを提供してきました。このコードはおそらくIOS6以降のモバイルWebアプリケーション開発に非常に役立つだろうから、助言や助言があれば歓迎されます。
var fileType = file.type,
reader = new FileReader();
reader.onloadend = function() {
var image = new Image();
image.src = reader.result;
image.onload = function() {
//Detect image size
var maxWidth = 960,
maxHeight = 960,
imageWidth = image.width,
imageHeight = image.height;
if (imageWidth > imageHeight) {
if (imageWidth > maxWidth) {
imageHeight *= maxWidth/imageWidth;
imageWidth = maxWidth;
}
} else {
if (imageHeight > maxHeight) {
imageWidth *= maxHeight/imageHeight;
imageHeight = maxHeight;
}
}
//Create canvas with new image
var canvas = document.createElement('canvas');
canvas.width = imageWidth;
canvas.height = imageHeight;
var ctx = canvas.getContext("2d");
ctx.drawImage(this, 0, 0, imageWidth, imageHeight);
// The resized file ready for upload
var finalFile = canvas.toDataURL(fileType);
if (formdata) {
formdata.append("images[]", finalFile);
$.ajax({
url: "upload.php",
type: "POST",
data: formdata,
dataType: 'json',
processData: false,
contentType: false,
success: function (res) {
//successful image upload
}
});
}
}
}
reader.readAsDataURL(file);
この問題に対する解決策をお探しですか?私は、大きな画像をカメラからまっすぐに、または過去にカメラから撮ったものをリサイズするときに、より問題があることを発見しました。 – NimmoNet