Hi,
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.gif' then the function should return 'gif'.
Thanks
Hi,
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.gif' then the function should return 'gif'.
Thanks
$ext = pathinfo('/testdir/dir2/image.gif', PATHINFO_EXTENSION); //$ext will be gif
UPDATE: I see a lot of downvotes. What's the problem with my approach? Please let me know.
Basic string functions, strrpos()
and substr()
can do that for you. As well as many other fancy ways.
As Col. Shrapnel mentioned; there's quite a few ways
$path = '/some/where/img.gif';
$path = explode('.',$path);
$path = end($path);
It's usually more desirable to detect the actual image type (not by extension but by its contents). For that, use getimagesize()
.
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.
http://www.php.net/imagepng
http://www.php.net/imagegif
http://www.php.net/imagejpeg