php getimagesize function - Get image info
The getimagesize() function is used to get image size and related information. On success, it returns an array; on failure, it returns FALSE and generates an E_WARNING level error message.
Syntax format:
array getimagesize ( string $filename [, array &$imageinfo ] )
The getimagesize() function will determine the size of any GIF, JPG, PNG, SWF, SWC, PSD, TIFF, BMP, IFF, JP2, JPX, JB2, JPC, XBM or WBMP image file and return the image dimensions as well as the file type and the image height and width.
Example 1: Local image file
<?php
list($width, $height, $type, $attr) = getimagesize("example-logo.png");
echo "宽度为:" . $width;
echo "高度为:" . $height;
echo "类型为:" . $type;
echo "属性:" . $attr;
?>
The output of the above example is:
宽度为:290 高度为:69 类型为:3 属性:width="290" height="69"
Example 2: Remote image file
<?php $remote_png_url = 'http://www.example.com/wp-content/themes/w3cschool.cc/assets/img/logo-domain-green2.png'; $img_data = getimagesize($remote_png_url); print_r($img_data ); ?>
The output of the above example is:
Array
(
[0] => 290
[1] => 69
[2] => 3
[3] => width="290" height="69"
[bits] => 8
[mime] => image/png
)
Return result description
- Index 0 gives the pixel value of the image width
- Index 1 gives the pixel value of the image height
- Index 2 gives the type of the image, returned as a number, where 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF (Intel byte order), 8 = TIFF (Motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM
- Index 3 gives a string of width and height, which can be used directly in HTML <image> tag
- The index 'bits' gives the number of bits for each color of the image, in binary format
- The index 'channels' gives the channel value of the image; RGB images default to 3
- The index 'mime' gives the MIME information of the image. This information can be used to send the correct information in the HTTP Content-type header, for example: header("Content-type: image/jpeg");
PHP Image Processing