PHP 5.4.35 Released

getimagesizefromstring

(PHP 5 >= 5.4.0)

getimagesizefromstringGet the size of an image from a string

Descrizione

array getimagesizefromstring ( string $imagedata [, array &$imageinfo ] )

Identical to getimagesize() except that getimagesizefromstring() accepts a string instead of a file name as the first parameter.

See the getimagesize() documentation for details on how this function works.

Elenco dei parametri

imagedata

The image data, as a string.

imageinfo

See getimagesize().

Valori restituiti

See getimagesize().

Esempi

Example #1 getimagesizefromstring() example

<?php
$img 
'/path/to/test.png';

// Open as a file
$size_info1 getimagesize($img);

// Or open as a string
$data       file_get_contents($img);
$size_info2 getimagesizefromstring($data);
?>

Vedere anche:

add a note add a note

User Contributed Notes 3 notes

up
11
imageman
10 months ago
getimagesizefromstring function for < 5.4

<?php
  
if (!function_exists('getimagesizefromstring')) {
      function
getimagesizefromstring($string_data)
      {
        
$uri = 'data://application/octet-stream;base64,'  . base64_encode($string_data);
         return
getimagesize($uri);
      }
}
?>
up
3
sarah at anigel dot net
9 months ago
Just a quick comment on the solution by imageman for versions < 5.4 you will need to enable allow_url_fopen in order to use the data wrapper.
up
-1
josh at karmabunny dot com dot au
1 year ago
If you need to get the type (but not the size) of an image contained within a string, you can make use of the signatures contained within the headers of various file formats.

<?php
function get_img_type($data) {
   
$magics = array(
       
'ffd8ff' => 'jpg',
       
'89504e470d0a1a0a' => 'png',
    );
       
    foreach (
$magics as $str => $ext) {
        if (
strtolower(bin2hex(substr($data, 0, strlen($str)/2))) == $str) return $ext;
    }
       
    return
NULL;
}
?>

If required, additional magic signatures can be added to the array, there is a page on Wikipedia with a good list:
http://en.wikipedia.org/wiki/List_of_file_signatures
To Top