exif_imagetype() 函数在linux下的php中不存在

1.问题,项目中上传文件使用插件时,windows上支持函数exif_imagetype(),而在linux上不支持。

2.PHP exif_imagetype的本质

PHP exif_imagetype note #1
Windows users: If you get the fatal error "Fatal error:  Call to undefined function exif_imagetype()", and you have enabled php_exif.dll, make sure you enable php_mbstring.dll.
You must put mbstring before exif in the php.ini, i.e.: extension=php_mbstring.dll extension=php_exif.dll You can check whether this has worked by calling phpinfo() and searching for exif. PHP exif_imagetype note #2 If the function exif_imagetype() is not available, you can try the following workaround: if ( ! function_exists( 'exif_imagetype' ) ) { function exif_imagetype ( $filename ) { if ( ( list($width, $height, $type, $attr) = getimagesize( $filename ) ) !== false ) { return $type; } return false; } } PHP exif_imagetype note #3 By trial and error, it seems that a file has to be 12 bytes or larger in order to avoid a "Read error!". Here's a work-around to avoid an error being thrown: // exif_imagetype throws "Read error!" if file is too small if (filesize($uploadfile) > 11) $mimetype = exif_imagetype($uploadfile); else $mimetype = false; PHP exif_imagetype note #4 Seems to give a 'Read error' warning if the size of the file is very small (2 bytes). I think this is because it needs a min 3 bytes to determine the file type PHP exif_imagetype note #5 libexif can also be used to parse image info out of id3 tags: exif_read_data("mp3_with_2.4ID3TAGS, '', true, false); PHP exif_imagetype note #6 After looking for hours, I found a very good source for exif related programs here: http://drewnoakes.com/code/exif/index.html It lists exif specifications (pdf), a few good links to exif related stuff. The best source I have found in my quest to understand exif better for use in php based exif tools.

3.解决方案:

  if ( ! function_exists( 'exif_imagetype' ) ) {

          // $currFile = $file['tmp_name'];
          list($width, $height, $type2, $attr) = getimagesize($file['tmp_name']);
           $type = $type2;
          // function exif_imagetype ($currFile) {
          //   if ( ( list($width, $height, $type2, $attr) = getimagesize($currFile) ) !== false ) {
          //       // return $type;
          //     $type = $type2;
          //   }
          //   return false;
          // }
        }else{
           // $type = exif_imagetype($src);
          $type = exif_imagetype($file['tmp_name']);
        }
原文地址:https://www.cnblogs.com/wuheng1991/p/6641187.html