从base64字符串中获取图像尺寸 [英] Get image dimensions from base64 string

查看:479
本文介绍了从base64字符串中获取图像尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个图像的base64字符串,并将其用作html页面中的源代码。反过来,img是div的孩子。我想通过图像尺寸自动调整div的大小。尝试使用javascript(... = element.style.height)获取图像的尺寸,但它不起作用(我认为这是因为异步加载图像)。现在我想也许有可能提前获得图像尺寸并手动计算父div的大小。

I receive base64 string of an image and use it as source for in html page. In turn, img is a child of div. I want to get div automatically resized by the image dimensions. Tried to get dimensions of an image using javascript (... = element.style.height), but it didn't work (I think it's because asynchronous loading of an image). Now I think that maybe it is possible to get image dimensions in advance and calculate size of a parent div manually.

是否有可能从base64字符串中提取图像尺寸完全解码?

Is it somehow possible to extract image dimensions from base64 string without decoding it completely?

EDITED 我使用onload方法来调整父div的大小。

EDITED I used onload method to resize the parent div.

推荐答案

这取决于图像格式。

例如,在PNG中它非常简单:解码前32个base64字符24字节:检查位置1-3是否由ASCII字符串'PNG'组成,然后在16-19和20-23位读取宽度和高度 http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html

For example, in PNG it's quite simple: decode the first 32 base64 chars in 24 bytes: check that the positions 1-3 consists of ASCII string 'PNG' and then read width and height in positions 16-19 and 20-23 http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html

这篇关于从base64字符串中获取图像尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆