Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Getting an Image's MIME Type

So far we have been handcrafting the header() function call in each of the image scripts, but many people find MIME types hard to remember and/or clumsy to use. If you fit into this category, you should be using the image_type_to_mime_type() function, as it takes a constant as its only parameter and returns the MIME type string. For example, passing in IMAGETYPE_GIF will return image/gif, passing in IMAGETYPE_JPEG will return image/jpeg, and passing in IMAGETYPE_PNG will return image/png.

If you think these constants sound as hard to remember as the MIME types, you're probably right. However, a while back we looked at the getimagesize() function, and I mentioned that the third element in the array returned by that function is the type of file it is. These two functions both use the same constant, which means you can use getimagesize() and pass the third element into image_type_to_mime_type() to have it get the appropriate MIME type for your image—no memorization of constants required.

    $info = getimagesize("button.png");
    print image_type_to_mime_type($info[2]);
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page