doufu1939 2012-04-12 19:55
浏览 60
已采纳

检查图像文件类型

I'm attempting to add a function to my site where users can set their profile picture as an image from an external url, instead of saving it to their pc, then uploading it to my server.

This is what I've come up with so far:

            $filename = $inputs['image_url'];

            if(getimagesize($filename)){
                // Get new dimensions
                list($width, $height) = getimagesize($filename);
                $new_width = 100;
                $new_height = 100;

                // Resample
                $image_p = imagecreatetruecolor($new_width, $new_height);                                           

                //a check needs to be done here $image = imagecreatefromjpeg($filename);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

                $filename = "profile_pictures/". md5( uniqid(rand(),true) ).".jpg";

                imagejpeg($image_p,$filename,100);
            }else{
                echo 'This is not an image';
            }

The code can download .jpg images fine, but I want to support other filetypes too.

I need to somehow check the file type of the image so I can save other images with different filetypes...

                if($extension=="jpg" || $extension=="jpeg"){
                    $image = imagecreatefromjpeg($filename);
                }else if($extension=="png"){
                    image = imagecreatefrompng($filename);
                }else{
                    $image = imagecreatefromgif($filename);
                }

Does anyone know of a function that would allow me to do this?

Thanks in advance for any replies.

  • 写回答

3条回答 默认 最新

  • duanshang3230 2012-04-12 20:04
    关注

    Most straight forward and for GD compatiblity checking you can just create it from string:

    $image = imagecreatefromstring(file_get_contents($filename));
    

    If this fails to load, you know that you can not process that image.

    But see as well getimagesize, it inspects the filetype as well, not only width and height:

    list($width, $height, $type) = getimagesize($filename);
    

    The third element, $type, then is any of these IMAGETYPE_XXX constants:

    Value   Constant
    1     IMAGETYPE_GIF
    2     IMAGETYPE_JPEG
    3     IMAGETYPE_PNG
    4     IMAGETYPE_SWF
    5     IMAGETYPE_PSD
    6     IMAGETYPE_BMP
    7     IMAGETYPE_TIFF_II (intel byte order)
    8     IMAGETYPE_TIFF_MM (motorola byte order)
    9     IMAGETYPE_JPC
    10    IMAGETYPE_JP2
    11    IMAGETYPE_JPX
    12    IMAGETYPE_JB2
    13    IMAGETYPE_SWC
    14    IMAGETYPE_IFF
    15    IMAGETYPE_WBMP
    16    IMAGETYPE_XBM
    17    IMAGETYPE_ICO
    

    From exif_imagetype. If you combine both, you could first check if the type is okay to load for you and you then check if you can load it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题