Automatic image format detection in PHP

假如想象 提交于 2019-12-17 04:08:25

问题


I am looking for a way to take a user uploaded image that is currently put in a temporary location ex: /tmp/jkhjkh78 and create a php image from it, autodetecting the format.

Is there a more clever way to do this than a bunch of try/catching with imagefromjpeg, imagefrompng, etc?


回答1:


This is one of the functions of getimagesize. They probably should have called it "getimageinfo", but that's PHP for you.




回答2:


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

this will make image name look cool and unique




回答3:


Use exif_imagetype() if it's available ..:

http://www.php.net/manual/en/function.exif-imagetype.php

I'm pretty sure exif functions are available by default (i.e. you have to specifically exclude them rather than specifically include them) when you install php




回答4:


You could try finfo_file(), apparently an improved version of mime_content_type().

Edit: OK, getimagesize() is better..




回答5:


You can call a system command (if you're under linux/unix), file if you like:

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



回答6:


This will help you to know the Extension as well as result based on condition

$image_file = 'http://foo.com/images.gif';
$extension = substr($image_file, -4);
if($extension == ".jpg"){ echo 'Its a JPG Image.'; } else { echo 'Its not a JPG Image.'; }




回答7:


People are recommending using getimagesize() but the documentation reads:

Caution This function expects filename to be a valid image file. If a non-image file is supplied, it may be incorrectly detected as an image and the function will return successfully, but the array may contain nonsensical values.

Do not use getimagesize() to check that a given file is a valid image. Use a purpose-built solution such as the Fileinfo extension instead.

The relevant function in the Fileinfo extension is finfo_file():

string finfo_file ( resource $finfo , string $file_name = NULL 
    [, int $options = FILEINFO_NONE [, resource $context = NULL ]] )

Returns a textual description of the contents of the file_name argument, or FALSE if an error occurred.

Example return values given are: text/html, image/gif, application/vnd.ms-excel

However, comments on the official documentation page warn that this shouldn't be relied on for validation either.



来源:https://stackoverflow.com/questions/189391/automatic-image-format-detection-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!