Search code examples
phpmime-typesgetimagesize

PHP using getimagesize() on a file (not a file name)


I am making an attachments system for my website. In a form-based upload method, you can get the image size using:

$SizeResult = getimagesize($_FILES['file']['tmp_name']);

But using a Drag&Drop upload system via HTML5's File API, the files are uploaded not by form post, but by black magic & wizardry.

$File = file_get_contents('php://input');

In this case $File is not a name, but the actual binary contents of the file.

How can I get the size of said $File, when getimagesize() only seems to accept file names? Internally, getimagesize wants to open the file from disk itself. It doesn't want to accept $File.

Anyone know how I can get around this issue? I'm hoping to avoid saving the file to disk.


Solution

  • This is what I meant, please try it:

    $File = file_get_contents('php://input');
    $image = ImageCreateFromString($File);
    echo ImageSX($image); // width
    echo ImageSY($image); // height