Call to undefined function exif_imagetype()

2020-02-12 00:17发布

问题:

I am trying to get Mime-Type for image-types as follow:

if(!empty($_FILES['uploadfile']['name']) && $_FILES['uploadfile']['error'] == 0){    

    $file = $_FILES['uploadfile']['tmp_name'];
    $file_type = image_type_to_mime_type(exif_imagetype($file));

    switch($file_type){

        // Codes Here

    }

}

But it always gives the error Call to undefined function exif_imagetype(). What am I doing wrong here?

回答1:

Enable the following extensions in php.ini and restart your server.

extension=php_mbstring.dll
extension=php_exif.dll

Then check phpinfo() to see if it is set to on/off



回答2:

I think the problem is PHP config and/or version, for example, in my case:

We know exif_imagetype() takes a file path or resource and returns a constant like IMAGETYPE_GIF and image_type_to_mime_type() takes that constant value and returns a string 'image/gif', 'image/jpeg', etc. This didn't work (missing function exif_imagetype), so I've found that image_type_to_mime_type() can also take an integer 1, 2, 3, 17, etc. as input, so solved the problem using getimagesize, which returns an integer value as mime type:

function get_image_type ( $filename ) {
    $img = getimagesize( $filename );
    if ( !empty( $img[2] ) )
        return image_type_to_mime_type( $img[2] );
return false;
}

echo get_image_type( 'my_ugly_file.bmp' );
// returns image/x-ms-bmp
echo get_image_type( 'path/pics/boobs.jpg' );
// returns image/jpeg


回答3:

Add this to your code so as we could know which version of php you do have because this function is only supported by (PHP version 4 >= 4.3.0, PHP 5).

<?php 
    phpinfo(); 
?> 

It may be not installed, you can add this part of code to make sure it is :

<?php
if (function_exists('exif_imagetype')) {
    echo "This function is installed";
} else {
    echo "It is not";
}
?>