【问题标题】:File size of a PNG image created with data URL?使用数据 URL 创建的 PNG 图像的文件大小?
【发布时间】:2015-03-03 19:47:28
【问题描述】:

我可以安全地假设使用以下 PHP 代码创建的 PNG 图像文件的大小(以字节为单位):

$f = fopen("newImageFile.png", 'w');
$content = base64_decode(substr($dataURL, strpos($dataURL, ',') + 1));
fwrite($f, $content);

$dataURL 是从 HTML 的 canves.toDataURL() 发布的,完全等于 strlen($content)

【问题讨论】:

  • 鉴于“strlen() returns the number of bytes rather than the number of characters in a string”,我认为这是查找数据长度的合理方法。我不确定文件大小会有什么不同,但我对此并不完全确定,所以我不会回答这个问题:) 不过我肯定会用“wb”打开你的文件,而不仅仅是“w”,因为您正在编写二进制数据。
  • 谢谢。我真的不知道在写二进制数据时我应该使用“wb”模式。我刚刚重读了文档,谢谢。

标签: php png filesize


【解决方案1】:

如果你有更新的 php 版本,你可以使用getimagesizefromstring()

但既然你正在解码,你也可以使用它。

    $uri = 'data://application/octet-stream;base64,' . base64_encode($data);
    getimagesize($uri);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    相关资源
    最近更新 更多