我正在添加
部分
中使用 Shrine、jquery.fileupload 和cropper.js 实现直接上传我正在将图像从文件上传加载到模态,定义裁剪器并显示模态
if (data.files && data.files[0]) {
var reader = new FileReader();
var $preview = $('#preview_avatar');
reader.onload = function(e) {
$preview.attr('src', e.target.result); // insert preview image
$preview.cropper({
dragMode: 'move',
aspectRatio: 1.0 / 1.0,
autoCropArea: 0.65,
data: {width: 270, height: 270}
})
};
reader.readAsDataURL(data.files[0]);
$('#crop_modal').modal('show', {
backdrop: 'static',
keyboard: false
});
}
然后在模态按钮上单击我得到裁剪的画布调用 toBlob 并提交到 S3
$('#crop_button').on('click', function(){
var options = {
extension: data.files[0].name.match(/(\.\w+)?$/)[0], // set extension
_: Date.now() // prevent caching
};
var canvas = $preview.cropper('getCroppedCanvas');
$.getJSON('/images/cache/presign', options).
then(function (result) {
data.formData = result['fields'];
data.url = result['url'];
data.paramName = 'file';
if (canvas.toBlob) {
canvas.toBlob(
function (blob) {
var file = new File([blob], 'cropped_file.jpeg');
console.log('file', file);
data.files[0] = file;
data.originalFiles[0] = data.files[0];
data.submit();
},
'image/jpeg'
);
}
});
});
上传到 S3 完成后,我正在将图像属性写入隐藏字段,关闭模式并销毁裁剪器
done: function (e, data) {
var image = {
id: data.formData.key.match(/cache\/(.+)/)[1], // we have to remove the prefix part
storage: 'cache',
metadata: {
size: data.files[0].size,
filename: data.files[0].name.match(/[^\/\\]*$/)[0], // IE returns full path
// mime_type: data.files[0].type
mime_type: 'image/jpeg'
}
};
console.log('image', image);
$('.cached-avatar').val(JSON.stringify(image));
$('#crop_modal').modal('hide');
$('#preview_avatar').cropper('destroy');
}
chrome 从一开始就一切正常,但后来我发现 safari 没有 toBlob 功能。
我找到了这个:
https ://github.com/blueimp/JavaScript-Canvas-to-Blob
而 toBlob is not a function 错误消失了。
现在由于某些 mime 类型相关的问题,我无法保存图像。
我能够找到它在 safari 上失败但不是 chrome 的确切位置。
在 io.read 之后 stdin_data中的第 139 行中的 第
142 行确定
_mime_type.rb 为空 options = {stdin_data: io.read(MAGIC_NUMBER), binmode: true}
有任何想法吗?
谢谢!
更新
我能够弄清楚由返回的缓存图像的 url
$.getJSON('/images/cache/presign', options)
从 safari 裁剪和上传时返回空文件。