English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and examples of the php getimagesize () function

PHP Image Processing

The getimagesize() function is used to obtain image size and related information. It returns an array on success and FALSE on failure, generating an E_WARNING level error message.

Syntax format:

array getimagesize ( string $filename [, array &$imageinfo ] )

The getimagesize() function determines any GIF, JPG, PNG, SWF, SWC, PSD, TIFF, BMP, IFF, JP2JPX, JB2The size of the JPC, XBM, or WBMP image file is determined and the image dimensions, file type, and height and width of the image are returned.

Example1:local image file

<?php
list($width, $height, $type, $attr) = getimagesize("w3codebox-logo.png");
echo "Width: " . $width;
echo "Height: " . $height;
echo "Type: " . $attr;
?>

The following example output results are:

Width:290
Height:69
Type:3
Attributes: width="290" height="69"

Example2:remote image file

<?php
$remote_png_url = 'http://www.oldtoolbag.com/wp-content/themes/oldtoolbag.com/assets/img/logo-domain-green2.png';
$img_data = getimagesize($remote_png_url);
print_r($img_data );
?>

The following example output results are:

Array
(
    [0] => 290
    [1] => 69
    [2] => 3
    [3] => width="290" height="69"
    [bits] => 8
    [mime] => image/png
)

Return result explanation

  • The index 0 provides the pixel value of the image width
  • index 1 This provides the pixel value of the image height
  • index 2 This provides the image type, returns a number, where1 = 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 This provides a string of width and height, which can be directly used in the HTML <image> tag
  • The index bits provides the number of bits for each color of the image, in binary format
  • The index channels provides the channel values of the image, RGB images are default to 3
  • The index mime provides the MIME information of the image, this information can be used in the HTTP Content-type header information to send the correct information, such as: header("Content-type: image/jpeg");

PHP Image Processing