Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中的图像格式自动检测_Php_Image_Format_Jpeg - Fatal编程技术网

PHP中的图像格式自动检测

PHP中的图像格式自动检测,php,image,format,jpeg,Php,Image,Format,Jpeg,我正在寻找一种方法来获取用户上传的图像,该图像当前放在临时位置ex:/tmp/jkhj78,并从中创建一个php图像,自动检测格式 有没有比使用imagefromjpeg、imagefrompng等进行一系列尝试/捕获更聪明的方法呢?如果您喜欢,可以调用系统命令(如果您在linux/unix下),文件: kender@eira:~$ file a a: JPEG image data, EXIF standard 2.2 这是的功能之一。他们可能应该称之为“getimageinfo”,但这是适

我正在寻找一种方法来获取用户上传的图像,该图像当前放在临时位置ex:/tmp/jkhj78,并从中创建一个php图像,自动检测格式


有没有比使用imagefromjpeg、imagefrompng等进行一系列尝试/捕获更聪明的方法呢?

如果您喜欢,可以调用系统命令(如果您在linux/unix下),
文件

kender@eira:~$ file a
a: JPEG image data, EXIF standard 2.2

这是的功能之一。他们可能应该称之为“getimageinfo”,但这是适合您的PHP。

您可以尝试,显然是的改进版本

编辑:好的,
getimagesize()
更好。

使用
exif\u imagetype()
如果可用的话。:


我非常确定在安装php时默认情况下exif函数是可用的(即,您必须明确排除它们,而不是明确包含它们)。这将帮助您了解扩展以及基于条件的结果

$image_文件=“”
$extension=substr($image\u file,-4)
如果($extension==“.jpg”){echo'是jpg图像。;}否则{echo'不是jpg图像。;}


这将使图像名称看起来既酷又独特

人们建议使用以下文字:

注意此函数要求文件名为有效的图像文件。如果 如果提供了非图像文件,则可能会错误地将其检测为图像 函数将成功返回,但数组可能包含 荒谬的价值观

不要使用
getimagesize()
检查给定文件是否为有效图像。 使用专门构建的解决方案,例如扩展

Fileinfo扩展中的相关功能是:

返回
文件名
参数,如果发生错误,则为FALSE

给出的示例返回值有:
text/html
image/gif
application/vnd.ms excel


然而,官方文档页面上的评论警告说,验证也不应依赖于此。

首先,没有检查扩展名jpeg、jpe、jif和jfif。虽然不常见,但它们仍然被一些人使用(尽管大部分是jpeg)。此外,扩展名不一定反映图像格式,如果网站易受本地文件包含的攻击,则上载带有JPG扩展名的PHP文件可能是致命的。@TimeSheep jif、jifif和jpe扩展名可以随时转换为jpeg,只需重命名即可。由于旧的windows系统无法识别4位扩展名,jif比jifif短。所以这不会像.gif.svg.png.psd那样成为问题(简单的分层psd可以通过浏览器查看)。。。任何好的逻辑怀特霍斯,保持下去:DIf我认为格式的事情是重要的,我可能会编辑它:)它只是一些值得注意的东西。我提到的更重要的一点是,
end(explode($image\u file,“.”)是一种更智能的方法,因为您可以处理任意长度的扩展。最后,使用try/catch实际上稍微安全一些,因为某些人由于某种原因具有执行权限但没有写入权限,可以上传伪装成图像的脚本,然后执行它。但这可能是一个愚蠢的边缘案例:)我不确定,但是
getimagesize()
也可能防止这个问题。
   //Image Processing
    $cover = $_FILES['cover']['name'];
    $cover_tmp_name = $_FILES['cover']['tmp_name'];
    $cover_img_path = '/images/';
    $type = exif_imagetype($cover_tmp_name);

if ($type == (IMAGETYPE_PNG || IMAGETYPE_JPEG || IMAGETYPE_GIF || IMAGETYPE_BMP)) {
        $cover_pre_name = md5($cover);  //Just to make a image name random and cool :D
/**
 * @description : possible exif_imagetype() return values in $type
 * 1 - gif image
 * 2 - jpg image
 * 3 - png image
 * 6 - bmp image
 */
        switch ($type) {    #There are more type you can choose. Take a look in php manual -> http://www.php.net/manual/en/function.exif-imagetype.php
            case '1' :
                $cover_format = 'gif';
                break;
            case '2' :
                $cover_format = 'jpg';
                break;
            case '3' :
                $cover_format = 'png';
                break;
            case '6' :
                $cover_format = 'bmp';
                break;

            default :
                die('There is an error processing the image -> please try again with a new image');
                break;
        }
    $cover_name = $cover_pre_name . '.' . $cover_format;
      //Checks whether the uploaded file exist or not
            if (file_exists($cover_img_path . $cover_name)) {
                $extra = 1;
                while (file_exists($cover_img_path . $cover_name)) {
        $cover_name = md5($cover) . $extra . '.' . $cover_format;
                    $extra++;
                }
            }
     //Image Processing Ends
string finfo_file ( resource $finfo , string $file_name = NULL 
    [, int $options = FILEINFO_NONE [, resource $context = NULL ]] )