如何验证存储在 ZIP 存档中的图像文件的内容类型


How to verify the content type of the image file stored in ZIP archive?

下面是我用来读取zip文件的代码。压缩文件包含图像。下面的代码工作正常。我也浏览了PHP文档。没有办法验证您使用zip_entry_read读取的特定条目的类型

if ($zip) 
{
    while ($zip_entry = zip_read($zip)) 
    {
        if (zip_entry_open($zip, $zip_entry, "r")) 
        {
            $imgContent = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); // File Contents
            /* how to verify it's an image before image manipulation ? */
            /* image manipulation code goes here */
            zip_entry_close($zip_entry);
        }
    }
    zip_close($zip);
}

我的问题是我如何验证它是我使用zip_entry_read函数读取的图像?

一种可能的方法:

$imageInfo = getimagesizefromstring($imgContent);
if ($imageInfo !== false) { 
  // that's an image
}

此外,您可能需要检查图像的 MIME 类型(例如,用于正确设置Content-Type标头):

$imageMimeType = image_type_to_mime_type($imageInfo[2]);

请注意,getimagesizefromstring()image_type_to_mime_type() 都需要GD扩展名 - 但话又说回来,您可能已经加载了它。