tom_nb_ny
tom_nb_ny

Reputation: 180

How do I detect the mime-type of an image that is already a resource variable?

Is it possible to determine the mime-type of an image resource, while it is still a resource variable? If I output the resource variable and use mime_content_type() or getimagesize(), its mime type is already set via whatever output function I use (imagejpeg(), imagepng() etc).

The reason I need to know this is to determine if the image may have transparency - if it was a JPEG, i know it can't, if it was a PNG or GIF, I know it potentially could have transparency.

Any advice would be appreciated!

Upvotes: 1

Views: 116

Answers (1)

Juan
Juan

Reputation: 5589

I just googled for magic numbers for jpg, png and I found this site:

https://asecuritysite.com/forensics/magic

which states the following magic numbers these file types:

.jpg  =>  FFD8
.gif  =>  47 49 46 38
.png  =>  89 50 4E 47

These numbers are the values of the first n bytes of the file which work as a signature of the file type. The values are expressed in hexadecimal.

By peeking into these values you can determine the type of the file.

Upvotes: 2

Related Questions