JavaScript,如何从斑点获得图像宽度和高度?
因此,这是我的代码段(删除了不必要的代码行):
let imgInput = document.getElementById('id_photo');
imgInput.addEventListener('change', function (e) {
if (e.target.files) {
for (let i = 0; i < e.target.files.length; i++) {
let imageFile = e.target.files[i];
var reader = new FileReader();
reader.onload = function (e) {
var img = document.getElementById(nevermindID);
img.onload = function() {
var shape = resizeImage(img) // resizing image for future canvas drawing, returns [width, height] (resized)
var canvas = document.createElement("canvas")
canvas.width = shape[0]
canvas.height = shape[1]
var ctx = canvas.getContext("2d")
ctx.drawImage(img, 0, 0, shape[0], shape[1])
// converting to base64 logic and the rest code
}
img.src = e.target.result;
}
reader.readAsDataURL(imageFile);
}
}
});
我想获取上载的原始图像的宽度和高度,以便我可以调整它。我不能从 imagefile 返回 不确定的,并且无法从 img 变量中获取它,因为它最初取决于我的CSS (在CSS中,&lt; img&gt;
标签位于&lt; div; gt;
标签中,而不是全屏幕,可以说是400像素)。因此,相应地,如果我上传5000x5000映像,那将不是原始大小(希望您得到的)。我的问题是,因为我的CSS调整了 div 和内部元素,所以我无法获得原始图像的正常宽度和高度,因此我可以调整它,然后用 canvas 然后将其转换为base64。
我尝试将此代码添加到 img.onload :
var blobURL = URL.createObjectURL(imageFile)
在这里,我访问我的原始映像( ImageFile ),为其创建Blob并获得Blob链接。但是我无法访问Blob Link图像的宽度和高度。有什么建议吗?
注意:我认为问题在于 img.onload 本身。就像在这一行中,我通过了 img :
ctx.drawImage(img, 0, 0, shape[0], shape[1])
fyi。这就是我上传图像时的外观。这里大约是90x90像素。这会导致画布问题,因为img.onload采用这些尺寸(90x90),而不是原始尺寸(约为5000x5000)
编辑:
如果有人好奇,我可以在这里粘贴我的已解决的代码版本,以便您可以对其进行调整。在下面发表评论。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用图像构造函数
You can use the Image constructor
最好的是创建一个 imageBitmapmap 来自这个blob。
在某些实现中,这避免了在主线程上制作的图像并阻止您的页面的解码,无论如何,与
htmlimageelement
在每个实现中相比,这是一个轻得多的路径(避免网络请求,避免使用网络请求,创建DOM对象,大量内部检查等)。如果您只想获取图像的大小,这还允许您从浏览器的内存中释放位图数据,这要归功于
.close()
方法,而HTMLIMLIMAGEELEMENT通常也会尝试缓存其位图数据被摧毁。作为奖励,它甚至可以在网络工作人员中工作。
如果您需要定位较旧的浏览器,则可以使用我的这个矿山 。
该方法的唯一缺点是它仅适用于位图映像,您无法从SVG图像的斑点创建一个ImageBitMap,因为这种图像不能具有其本质的固有尺寸。
而且,由于您要在画布上绘制该图像,因此请注意,您也可以显然将此imagebitmap传递到
drawimage()
(在关闭它之前),甚至还允许您进行调整尺寸直接地:The best is to create an ImageBitmap from this Blob.
In some implementations this avoids having the decoding of the image being made on the main thread and blocking your page, and is anyway a much lighter path than the
HTMLImageElement
one in every implementation (avoids a network request, the creation of a DOM object, a lot of internal checks etc.).If you only want to get the size of the image, this also allows you to release the bitmap data from the browser's memory, thanks to the
.close()
method, while the HTMLImageElement will generally try to cache its bitmap data even after the original DOM element has been destroyed.And as a bonus, it even works in Web Workers.
If you need to target older browsers, you can use this polyfill of mine.
The only drawback of this method is that it works only for bitmap images, you can't create an ImageBitmap from a Blob of an SVG image, because this kind of image can have no intrinsic dimensions of its own.
And since you are gonna draw that image on a canvas, note that you can also obviously pass this ImageBitmap to
drawImage()
(before you close it), and that it also even allows you to do the resizing directly:clientHeight
,clientwidth
,OffSetheight
,OffsetWidth
,height> height
,width width <
/代码>将为您提供相对尺寸。要获取原始尺寸,您必须使用.naturalheight
和.naturalAlalWidth
。href =“ https://developer.mozilla.org/en-us/docs/web/api/htmlemageelement/naturalalwidth” rel =“ noreferrer /code>和
.naturalheight
as另外,如果仅添加了一个图像以获取SRC,则可以尝试
new Image()
而不是跳过创建额外的DOM元素。clientHeight
,clientWidth
,offsetHeight
,offsetWidth
,height
,width
would give you relative dimensions. To get original dimensions you must use.naturalHeight
and.naturalWidth
.You snippet can be updated with
.naturalWidth
and.naturalHeight
asAlso if you added an image just to get src you can try
new Image()
instead to skip creating an extra DOM element.