1

我正在为我的上传服务(在 PHP 中)开发一个 API 系统。目前,我支持将图像数据作为二进制数据从 file_get_contents、fread 等发送的选项,或者通过基于 64 的系统对其进行编码。我试图从这个二进制/base64 数据中确定上传到我的服务的图像的扩展类型。如果它是base64,那么我对其进行解码然后处理它。

我目前有以下内容:

// We need to figure it out ourselves
if ($type === "")
{
    // Let's see if it is a base64 file
    $base64 = $this->checkBase64Encoded($file_data);

    // We got a 64 based file or binary
    $type = $base64 === TRUE ? "base64" : "binary";
}

if ($type == "binary")
{
    $im = imagecreatefromstring($file_data);

}

我想看看是否可以确定保存文件的图像扩展类型。你们有什么建议?我读了一些关于使用 getimagesize() 的内容?虽然我不确定这一点。有没有办法暂时保存文件,处理它,然后重命名它?

在我检查扩展之前,我还计划使用它来测试图像是否是有效图像,但我不确定如何使用 getimagesize() 函数:

try
        {
            // Get the width and height from the uploaded image
            list($width, $height) = getimagesize($file['tmp_name']); // I'm not sure what to put here instead of $file['tmp_name']
        }
        catch (ErrorException $e)
        {
            // Ignore read errors
        }

        if (empty($width) OR empty($height))
        {
            // Cannot get image size, cannot validate
            return FALSE;
        }

如果我不清楚,请随时要求任何澄清。非常感谢 :)

4

1 回答 1

4

您正在寻找fileinfo函数,尤其是finfo_buffer().

于 2012-04-13T23:22:06.343 回答