检查图像文件类型 [英] Checking image file type

查看:114
本文介绍了检查图像文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我的网站添加一个功能,用户可以将其个人资料图片设置为外部网址中的图片,而不是将其保存到自己的电脑上,然后将其上传到我的服务器。

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

该代码可以下载.jpg图片,但我想支持其他文件类型

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?

提前感谢任何回复。

推荐答案

最直接的和GD兼容性检查您可以从字符串创建它:

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.

但请参阅 getimagesize ,它还检查文件类型,不仅是宽度和高度:

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

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

第三个元素, $ type ,然后是以下任何 IMAGETYPE_XXX 常数:

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

来自 exif_imagetype 。如果你将两者结合起来,你可以先检查一下你的类型是否可以加载,然后检查是否可以加载它。

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.

这篇关于检查图像文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆