Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting SVG image in PHP

Tags:

php

image

svg

Using PHP, I'd like to check if a uploaded image is in jpg, gif, png, or svg format. I found exif_imagetype function could check the file format, but it does not support svg format. Is there any way to detect svg format in PHP?

like image 619
mmrn Avatar asked Dec 17 '15 21:12

mmrn


1 Answers

To extend Victors answer, you can use mime_content_type('/path/to/file'); (see php.net manual for the function) and check if the return string is equal to image/svg+xml. Something like:

public function isSvg($filePath = '/path/to/file')
{
    return 'image/svg+xml' === mime_content_type($filePath);
}
like image 105
krle Avatar answered Oct 05 '22 23:10

krle