PHP | getimagesizefromstring() Function
Last Updated :
11 Jul, 2025
Improve
The getimagesizefromstring() function is an inbuilt function in PHP which is used to get the size of an image from a string. This function accepts the image data as a string parameter and determines the image size and returns the dimensions with the file type and height/width of the image.
Syntax:
Example 1 :
php
Output:
php
Output:
array getimagesizefromstring( $imagedata, &$imageinfo )Parameters: This function accepts two parameters as mentioned above and described below:
- $filename: It is a mandatory parameter which accepts image data as a string.
- $imageinfo: It is an optional parameter which allows to extract some extended information from the image file such as the different JPG APP markers as associative array.
Example 1 :
<?php
$img =
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png';
// Open image as a string
$data = file_get_contents($img);
// getimagesizefromstring function accepts image data as string
$info = getimagesizefromstring($data);
// Display the image content
var_dump($info);
?>
array(6) {
[0]=> int(667)
[1]=> int(184)
[2]=> int(3)
[3]=> string(24) "width="667" height="184""
["bits"]=> int(8)
["mime"]=> string(9) "image/png"
}
Example 2:
<?php
$img =
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png';
// Open image as a string
$data = file_get_contents($img);
// getimagesizefromstring function accepts image data as string
list($width, $height, $type, $attr) = getimagesizefromstring($data);
// 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;
?>
Width of image: 667 Height of image: 184 Image type: 3 Image attribute: width="667" height="184"Reference: https://www.php.net/manual/en/function.getimagesizefromstring.php
Article Tags :