dsgk40568 2012-04-13 23:09
浏览 75
已采纳

使用图像流确定扩展类型

I am developing an API system for my uploading service (in PHP). At the current moment I support the option to send image data as binary data from file_get_contents, fread, etc, or by encoding it with 64 based system. I am attempting to determine the extension type of the image being uploaded to my service from this binary/base64 data. If it is base64 then I decode it and then process it.

I have the following at the moment:

// 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);

}

I want to see if it is possible to determine the image extension type for saving the file. What do you guys suggest? I read something about using getimagesize()? Although I am not sure about this. Is there no way to get around temporarily saving the file, processing it, and then renaming it?

I also planned to use this to test that the image was a valid image before i checked for extension but I wasn't exactly sure how to use the getimagesize() function:

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;
        }

Please feel free to ask for any clarifications if I was unclear. Thanks so much :)

  • 写回答

1条回答 默认 最新

  • douxi5940 2012-04-13 23:22
    关注

    You're looking for the fileinfo functions, particularly finfo_buffer().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?