Tutorialspoint.dev

PHP | getimagesizefromstring() Function

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:

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.

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

Below programs illustrate the getimagesize() function in PHP:



Image:

Example 1 :

<?php
  
$img
   
// 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);
?>

Output:

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
   
// 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
?>

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.getimagesizefromstring.php



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter