getimagesizefromstring
Description
The getimagesizefromstring of Image for PHP get the size of an image from a string.
Syntax
getimagesizefromstring( string $string, array &$image_info = null ): array|false
Parameters
string
The image data, as a string.
image_info
This optional parameter allows you to extract some extended information from the image file. Currently, this will return the different JPG APP markers as an associative array. Some programs use these APP markers to embed text information in images. A very common one is to embed IPTC information in the APP13 marker. You can use the iptcparse() function to parse the binary APP13 marker into something readable.
Note: The image_info only supports JFIF files.
Return
Returns an array with up to 7 elements.
Index 0 contains the width of the image.
Index 1 contains the height of the image.
Note: Some formats may contain no image or may contain multiple images. In these cases, getimagesize() might not be able to properly determine the image size. getimagesize() will return zero for width and height in these cases.
Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.
Index 3 is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag.
bits is the number of bits for each color. Not all image types will include the bits element.
channels will be 3 for RGB pictures and 4 for CMYK pictures. Not all image types will include the channels element.
mime is the correspondant MIME type of the image. This information can be used to deliver images with the correct HTTP Content-type header.
On failure, false is returned.
Examples
1 · string
<? $filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/jpg/Happy.jpg'; $string = file_get_contents($filename); $return = getimagesizefromstring($string); print_r($return);
Array ( [0] => 100 [1] => 100 [2] => 2 [3] => width="100" height="100" [bits] => 8 [channels] => 3 [mime] => image/jpeg )
2 · image_info
<? $filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/jpg/Happy.jpg'; $string = file_get_contents($filename); $return = getimagesizefromstring($string, $image_info); print_r($return); print_r($image_info);
Array ( [0] => 100 [1] => 100 [2] => 2 [3] => width="100" height="100" [bits] => 8 [channels] => 3 [mime] => image/jpeg ) Array ( [APP0] => JFIF G G [APP1] => http://ns.adobe.com/xap/1.0/ <?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.1-c034 46.272976, Sat Jan 27 2007 22:37:37 "> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="" xmlns:xap="http://ns.adobe.com/xap/1.0/"> <xap:CreatorTool>Adobe Fireworks CS3</xap:CreatorTool> <xap:CreateDate>2008-10-17T23:32:20Z</xap:CreateDate> <xap:ModifyDate>2023-08-23T16:42:33Z</xap:ModifyDate> </rdf:Description> <rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/"> <dc:format>image/jpeg</dc:format> </rdf:Description> </rdf:RDF> </x:xmpmeta> <?xpacket end="w"?> )