从数据URI图像中获取(文件)大小,尺寸和mime类型?

时间:2013-12-26 12:10:01

标签: php image canvas data-uri

我有一个壁纸上传表格,可以自动生成他希望上传的访客所选壁纸的拇指。使用画布生成此拇指并将其作为数据URI保存到文本输入中。

如果我解码数据URI并将其保存在硬盘驱动器上(我之前),我想提取输出文件将具有的大小。我也想得到mime类型(但我想我可以自己找到),如果可能的话,还有图像尺寸。

我该怎么办?请注意,我不是在询问如何保存数据URI编码图像,而是在保存之前如何提取有关它的详细信息。

谢谢:)

1 个答案:

答案 0 :(得分:0)

首先,在allow_url_fopen中启用php.ini

; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
; http://php.net/allow-url-fopen
allow_url_fopen = On

然后,您可以使用fopenfile_get_contents等直接从数据uri中读取数据。

例如,这将使gegerate成为demo.png并输出“hello world”:

<?php

file_put_contents("demo.png", file_get_contents("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPgAAABICAIAAABUea8pAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAPXUlEQVR4nO1dXWwc1RU+G4h/yCb2JGySgkPSMaICJAcyUhB5wFUZgtSq6QuzL6h9gLCuCn0oVNpVQRQoVF6pifpQpO4CUkXoQz3lpX0hYUCNK4UGNKGxBJWjZBKTBGwMGa+zidd28PThsMdn78zO/njXXsX3e9q5c3/Ovfe75557zh074nkeSEhc71iz0gJISCwHJNElVgUk0SVWBSTRJVYFJNElVgVuXGkBFuFdOgenh+Hzk/DALyMbd6y0OBLXFVqC6N7h38HpozCTA3cMAKC3HyTRJRqKliA6XDwJn48sPl46t2KSSFynaA0b/dadJY+c9BISjUBrEP2WUqJLjS7RaCwH0b3Rt72CG5Zj4/aSx0tj3sxUU0WSWG1oLtG9KWdh6IfePx71jv8hLN/G7dDRtfhYyMGlsaYKJrHa0ESieydf997cA2PvAwB8eMAbP1EuZ6SzW1TqF082TzCJVYhmEd0bP+EdeQpmFy0Q7x+PhhUQ/Imu1OgSjUSziB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