在 PHP 或 Unix 命令行中确定图像分辨率和文件类型的最快方法?

发布于 2024-07-06 06:16:02 字数 399 浏览 3 评论 0原文

我目前正在使用 ImageMagick 来确定上传到网站的图像的大小。 通过在命令行上调用 ImageMagick 的“identify”,大约需要 0.42 秒才能确定 1MB JPEG 的尺寸以及它是 JPEG 的事实。 我觉得有点慢。

使用 Imagick PHP 库甚至更慢,因为它在对图像进行任何处理之前尝试将整个 1MB 加载到内存中(在本例中,只需确定其大小和类型)。

是否有任何解决方案可以加快确定任意图像文件的文件类型和尺寸的过程? 我可以忍受它只支持 JPEG 和 PNG。 对我来说很重要的是,文件类型是通过查看文件头而不仅仅是扩展名来确定的。

编辑:解决方案可以是 PHP 调用的 UNIX 命令行工具,很像我现在使用 ImageMagick 的方式

I'm currently using ImageMagick to determine the size of images uploaded to the website. By calling ImageMagick's "identify" on the command line it takes about 0.42 seconds to determine a 1MB JPEG's dimensions along with the fact that it's a JPEG. I find that a bit slow.

Using the Imagick PHP library is even slower as it attemps to load the whole 1MB in memory before doing any treatment to the image (in this case, simply determining its size and type).

Are there any solutions to speed up this process of determining which file type and which dimensions an arbitrary image file has? I can live with it only supporting JPEG and PNG. It's important to me that the file type is determined by looking at the file's headers and not simply the extension.

Edit: The solution can be a command-line tool UNIX called by PHP, much like the way I'm using ImageMagick at the moment

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

掩于岁月 2024-07-13 06:16:02

如果您使用支持 GD 的 PHP,可以尝试 getimagesize()。

If you're using PHP with GD support, you can try getimagesize().

苏辞 2024-07-13 06:16:02

你有没有尝试过

identify -ping filename.png

Have you tried

identify -ping filename.png

?

〃温暖了心ぐ 2024-07-13 06:16:02

抱歉,我无法将此作为评论添加到之前的答案中,但我没有代表。 做了一些快速而肮脏的测试,我还发现 exec("identify -ping... 比没有 -ping 快大约 20 倍。但是 getimagesize() 似乎仍然快大约 200 倍。

所以我会说 getimagesize()是更快的方法,我只在 jpg 上进行了测试,而没有在 png 上进行

了测试。

$files = array('2819547919_db7466149b_o_d.jpg', 'GP1-green2.jpg', 'aegeri-lake-switzerland.JPG');
foreach($files as $file){
  $size2 = array();
  $size3 = array();
  $time1 = microtime();
  $size = getimagesize($file);
  $time1 = microtime() - $time1;
  print "$time1 \n";
  $time2 = microtime();
  exec("identify -ping $file", $size2);
  $time2 = microtime() - $time2;
  print $time2/$time1 . "\n";
  $time2 = microtime();
  exec("identify $file", $size3);
  $time2 = microtime() - $time2;
  print $time2/$time1 . "\n";
  print_r($size);
  print_r($size2);
  print_r($size3);
}

Sorry I can't add this as a comment to a previous answer but I don't have the rep. Doing some quick and dirty testing I also found that exec("identify -ping... is about 20 times faster than without the -ping. But getimagesize() appears to be about 200 times faster still.

So I would say getimagesize() is the faster method. I only tested on jpg and not on png.

the test is just

$files = array('2819547919_db7466149b_o_d.jpg', 'GP1-green2.jpg', 'aegeri-lake-switzerland.JPG');
foreach($files as $file){
  $size2 = array();
  $size3 = array();
  $time1 = microtime();
  $size = getimagesize($file);
  $time1 = microtime() - $time1;
  print "$time1 \n";
  $time2 = microtime();
  exec("identify -ping $file", $size2);
  $time2 = microtime() - $time2;
  print $time2/$time1 . "\n";
  $time2 = microtime();
  exec("identify $file", $size3);
  $time2 = microtime() - $time2;
  print $time2/$time1 . "\n";
  print_r($size);
  print_r($size2);
  print_r($size3);
}
深白境迁sunset 2024-07-13 06:16:02

对我来说很重要的是,文件类型是通过查看文件头而不仅仅是扩展名来确定的。

为此,您可以使用 'file' unix 命令(或一些实现相同功能的 php 函数)。

/tmp$ 文件 stackoverflow-logo-250.png
stackoverflow-logo-250.png:PNG 图像数据,250 x 70,8 位色彩图,非隔行扫描

It's important to me that the file type is determined by looking at the file's headers and not simply the extension.

For that you can use 'file' unix command (orsome php function that implements the same functionality).

/tmp$ file stackoverflow-logo-250.png
stackoverflow-logo-250.png: PNG image data, 250 x 70, 8-bit colormap, non-interlaced

棒棒糖 2024-07-13 06:16:02

实际上,要使用 getimagesize(),您不需要需要编译 GD。

您还可以使用 mime_content_type() 来获取 MIME 类型。

Actually, to use getimagesize(), you do NOT need to have GD compiled in.

You can also use mime_content_type() to get the MIME type.

念﹏祤嫣 2024-07-13 06:16:02

exif_imagetype() 比 getimagesize() 更快。

$filename = "somefile";
$data = exif_imagetype($文件名);
echo "

";
print_r($data);
echo "

";

输出:

Array (
        [FileName] => somefile
        [FileDateTime] => 1234895396
        [FileSize] => 15427
        [FileType] => 2
        [MimeType] => image/jpeg
        [SectionsFound] => 
        [COMPUTED] => Array
            (
                [html] => width="229" height="300"
                [Height] => 300
                [Width] => 229
                [IsColor] => 1
        )
)

exif_imagetype() is faster than getimagesize().

$filename = "somefile";

$data = exif_imagetype($filename);

echo "<PRE>";

print_r($data);

echo "</PRE>";

output:


Array (
[FileName] => somefile
[FileDateTime] => 1234895396
[FileSize] => 15427
[FileType] => 2
[MimeType] => image/jpeg
[SectionsFound] =>
[COMPUTED] => Array
(
[html] => width="229" height="300"
[Height] => 300
[Width] => 229
[IsColor] => 1
)
)

够钟 2024-07-13 06:16:02

如果您使用 PHP,我建议使用 Imagick 库,而不是调用 exec()。 您正在寻找的功能是 Imagick::pingImage()

If you're using PHP I'd suggest using the Imagick library rather than calling exec(). The feature you're looking for is Imagick::pingImage().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文