PHP | getimagesize() Function

The getimagesize() function in PHP is an inbuilt function which is used to get the size of an image. This function accepts the filename as a parameter and determines the image size and returns the dimensions with the file type and height/width of image.

Syntax:

array getimagesize( $filename, $image_info )

Parameters: This function accepts two parameters as mentioned above and described below:



  • $filename: It is a mandatory parameter which specifies the image file name.
  • $image_info: It is an optional parameter which allows you to extract some extended information from the image file such as the different JPG APP markers as associative array.

Return Value: It returns the dimensions along with the file type and a height/width text string.

Exceptions:

  • The getimagesize() function returns zero for width and height if the formats which may contain no image or multiple images.
  • The imageinfo parameter only supports JFIF files.
  • The getimagesize() function will generate an error of level E_WARNING, if accessing the filename image is impossible.
  • The getimagesize() will generate an error of level E_NOTICE, if there is any error in reading.

Below programs illustrate the getimagesize() function in PHP:

Note: The image (geeks.png) given below used in the following program.
geeks image

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Calling getimagesize() function
$image_info = getimagesize("geeks.png");
print_r($image_info);
?>

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Calling getimagesize() function
list($width, $height, $type, $attr) = getimagesize("geeks.png");
   
// Displaying dimensions of the image
echo "Width of image : " . $width . "<br>";
  
echo "Height of image : " . $height . "<br>";
  
echo "Image type :" . $type . "<br>";
  
echo "Image attribute :" .$attr;
?>

chevron_right


Output:

Width of image : 667
Height of image : 184
Image type :3
Image attribute :width="667" height="184"

Reference: http://php.net/manual/en/function.getimagesize.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.