Get mimetype of an image in php

Mime type is an standard identifier of a file which indicates the type of data a file contains. It has two parts divided with a slash(/) like image/png.

The built in function of php getimagesize() gives lots of information about an image. And this function is enough to get the mime type of an image. It also returns Width and Height of the image.

GD Library is also not required to use this function.
   
   $image = getimagesize("imagename.jpg");
   echo $image['mime'];

It returns an array with upto 7 elements.
  Array
  (
    [0] => 200
    [1] => 125
    [2] => 2
    [3] => width="200" height="125"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
  )
Index 0 and 1 of the array contains the width and height of the array respectively.

This function can play a big role if you are creating an image upload script and want to limit the size of the image. Suppose if you don't want to upload an image greater than 400x250.

Just check width and height of the image using getimagesize() function and return respective messages to user to upload image smaller than the required size.
Get mimetype of an image in php Get mimetype of an image in php Reviewed by JS Pixels on November 16, 2013 Rating: 5

2 comments:

  1. Even I am in the same field, a software engineer. And programming interests me as well.
    And your blog is quite informative. Bookmarked!
    Keep up the good work Altaf!:)

    ReplyDelete
    Replies
    1. Thanks Nikhil you find it informative. Keep visiting for more.

      Delete

Altaf Web. Powered by Blogger.