Every image file has a header section in it which describes the size of the image, its type and other parameters. This header can be used to retrieve the image details. It can be helpful in a PHP program either to validate the file type or to prepare the image for output. This function can be used to find the actual file type (GIF, JPEG...) eventhough the file extention provided is different or is not provided. This function can be used along with file upload scripts so that the actual file type can be verified.
The function along with its usage is given in this post.The general synatx of this function is:
array getimagesize(string filename);
Where filename is the name of the image file to be checked. It will return an array containing the image parameters. The returned array will contain 7 elements which are as follows.
[0] => Width
[1] => Height
[2] => Image Type (Refer last section)
[3] => Height and with formatted such that it can be directly used inside a <img> tag.
['mime'] => Gives the MIME type of the image which can be used to set HTTP "Content-type"
['channels'] => Gives the number of channels(3->RGB, 4->CMYK)
['bits'] => Gives the bit depth for each colour, that is number of bits per colour.
The following code can be used to get the image parameters and display the array contents.
The following code can be used to set the MIME type and display the image.
1 -> GIF
2 -> JPEG
3 -> PNG
4 -> SWF
5 -> PSD
6 -> BMP
7 -> TIFF
8 -> TIFF
17 -> ICO
The function along with its usage is given in this post.The general synatx of this function is:
array getimagesize(string filename);
Where filename is the name of the image file to be checked. It will return an array containing the image parameters. The returned array will contain 7 elements which are as follows.
[0] => Width
[1] => Height
[2] => Image Type (Refer last section)
[3] => Height and with formatted such that it can be directly used inside a <img> tag.
['mime'] => Gives the MIME type of the image which can be used to set HTTP "Content-type"
['channels'] => Gives the number of channels(3->RGB, 4->CMYK)
['bits'] => Gives the bit depth for each colour, that is number of bits per colour.
The following code can be used to get the image parameters and display the array contents.
<?php $filename = image.jpg; //Path to the image file $param = getimagesize($filename); print_r($param); ?>
The following code can be used to set the MIME type and display the image.
<?php $filename = image.jpg; //Path to the image file $param = getimagesize($filename); $fp = fopen($filename , "r"); if($fp) { header("Content-type: " . $param['mime']); fpassthru($fp); exit; } else { header("Content-type: text/plain"); print("Unable to read image"); } ?>
Image Type
The image type found at array index 2 will be an integer between 1 and 17. The list below can be used to find the common image type from the integer.1 -> GIF
2 -> JPEG
3 -> PNG
4 -> SWF
5 -> PSD
6 -> BMP
7 -> TIFF
8 -> TIFF
17 -> ICO
No comments:
Post a Comment