How to find the extension of an image from path in PHP?

前端 未结 9 681
抹茶落季
抹茶落季 2021-01-04 12:52

Is there any standard function in PHP to find only extension of an image from the corresponding file path?

For example ff my image path is like \'/testdir/dir2/image

9条回答
  •  一整个雨季
    2021-01-04 13:28

    I think the most correct way is using echo exif_imagetype function:

       exif_imagetype("/testdir/dir2/image.gif");
    
       function get_image_type($image_path){
    
            $extension  = array(IMAGETYPE_GIF => "gif",
            IMAGETYPE_JPEG => "jpeg",
            IMAGETYPE_PNG => "png",
            IMAGETYPE_SWF => "swf",
            IMAGETYPE_PSD => "psd",
            IMAGETYPE_BMP => "bmp",
            IMAGETYPE_TIFF_II => "tiff",
            IMAGETYPE_TIFF_MM => "tiff",
            IMAGETYPE_JPC => "jpc",
            IMAGETYPE_JP2 => "jp2",
            IMAGETYPE_JPX => "jpx",
            IMAGETYPE_JB2 => "jb2",
            IMAGETYPE_SWC => "swc",
            IMAGETYPE_IFF => "iff",
            IMAGETYPE_WBMP => "wbmp",
            IMAGETYPE_XBM => "xbm",
            IMAGETYPE_ICO => "ico");
    
            return $extension[exif_imagetype($image_path)];
    }
    

提交回复
热议问题