【问题标题】:php get image size from a string of image contentphp从一串图像内容中获取图像大小
【发布时间】:2012-01-19 05:12:49
【问题描述】:

我有一个函数可以通过 CURL 获取远程图像,返回远程图像内容的字符串变量。

我不希望将内容写入文件,但希望获取图像的大小。

由于getimagesize()只支持文件,有没有类似getimagesize()的函数可以支持图片内容的字符串?

澄清:

$image_content = file_get_contents('http://www.example.com/example.jpg');

如何获取$image_content的图像大小而不是运行getimagesize('http://www.example.com/example.jpg');

提前致谢

【问题讨论】:

  • 图像尺寸或文件大小?
  • @Dagon 图像尺寸。我怀疑文件大小可以通过strlen() 来实现。

标签: php imagemagick gd


【解决方案1】:

PHP 函数 imagecreatefromstringimagesximagesy

类似的东西;

$image_content = file_get_contents('http://www.example.com/example.jpg');
$image = imagecreatefromstring($image_content);
$width = imagesx($image);
$height = imagesy($image);

【讨论】:

    【解决方案2】:

    仅供参考,对于那些迟到的人,PHP >= 5.4 现在包括 getimagesizefromstring(),它与 getimagesize() 相同,但接受第一个参数的字符串而不是位置。

    http://php.net/manual/en/function.getimagesizefromstring.php

    【讨论】:

      【解决方案3】:

      好的,我找到了 PHP 5.3 及以下版本的答案

      if (!function_exists('getimagesizefromstring')) {
          function getimagesizefromstring($data)
          {
              $uri = 'data://application/octet-stream;base64,' . base64_encode($data);
              return getimagesize($uri);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-24
        • 1970-01-01
        • 2020-03-14
        • 2011-12-15
        • 1970-01-01
        相关资源
        最近更新 更多