php 如何从PHP中的路径找到图像的扩展名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3179294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:56:16  来源:igfitidea点击:

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

phpimage

提问by Siva

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

PHP 中是否有任何标准函数可以仅从相应的文件路径中查找图像的扩展名?

For example ff my image path is like '/testdir/dir2/image.gif' then the function should return 'gif'.

例如ff我的图像路径就像'/testdir/dir2/image.gif'那么函数应该返回'gif'。

Thanks

谢谢

回答by fabrik

$ext = pathinfo(
    parse_url('/testdir/dir2/image.gif?foo=bar', PHP_URL_PATH), 
    PATHINFO_EXTENSION
); //$ext will be gif

回答by Pekka

It's usually more desirable to detect the actual image type(not by extension but by its contents). For that, use getimagesize().

通常更需要检测实际的图像类型(不是通过扩展,而是通过其内容)。为此,请使用getimagesize().

回答by Nightmare

I had problem with first answer and url with anchor ex. google.com/image.jpg#anchor

我对第一个答案和锚点前的网址有问题。google.com/image.jpg#anchor

better solution

更好的解决方案

$filename_from_url = parse_url($url);
$ext = pathinfo($filename_from_url['path'], PATHINFO_EXTENSION);

回答by Robus

As Col. Shrapnel mentioned; there's quite a few ways

正如弹片上校提到的那样;有很多方法

$path = '/some/where/img.gif';
$path = explode('.',$path);
$path = end($path);

回答by rekobeko

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

我认为最正确的方法是使用 echo exif_imagetype 函数:

   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)];
}

回答by ahmed hamdy

I think the easy way using strrpos()and substr()methods

我认为简单的使用方法strrpos()substr()方法

$path = "/testdir/dir2/image.gif";
$ext = substr($path, strrpos($path, '.')+1);
echo $ext; // output will be gif

more answers for Another Questionrelated to this Question

与此问题相关的另一个问题的更多答案

回答by Mudassar Ali Sahil

I would recommend you perfect way

我会推荐你​​完美的方式

$file_path = '/some/where/img.gif';  
$info = new SplFileInfo($file_path);  
$file_extension = $info->getExtension(); 
var_dump($file_extension);

for more detail here The SplFileInfo class

有关更多详细信息,请参阅 SplFileInfo 类

I hope this will help you.

我希望这能帮到您。

Cheers!

干杯!

Mudassar Ali

穆达萨阿里

回答by eklundchristopher

I would recommend you to run any uploaded/linked image(s) through a GD/ImageMagick check and re-save it to prevent any malicious codes hidden within the images. This would also allow you to save all of the images with the same extension to make things easier for you.

我建议您通过 GD/ImageMagick 检查运行任何上传/链接的图像并重新保存它以防止任何隐藏在图像中的恶意代码。这也将允许您使用相同的扩展名保存所有图像,以使您的工作更轻松。

http://www.php.net/imagepng
http://www.php.net/imagegif
http://www.php.net/imagejpeg

http://www.php.net/imagepng
http://www.php.net/imagegif
http://www.php.net/imagejpeg

回答by Your Common Sense

Basic string functions, strrpos()and substr()can do that for you. As well as many other fancy ways.

基本字符串函数,strrpos()并且substr()可以为你做的。以及许多其他花哨的方式。