Search code examples
phpmysqlblobgetimagesize

Getting image dimensions using getimagesize() from a BLOB in PHP


I'm trying to use getimagesize() to get the width and height of my image which I have fetched from a database as a BLOB. I've tried to convert it to base64 to get the dimensions but it doesn't seem to work.

if (!empty($row['img'])) {$img = $row['img'];}
    list($width, $height) = getimagesize(base64_encode($img));
    return $width . " " . $height;

Does anyone know how to fix this or know an alternative method that would return the images dimensions?

SOLUTION:

if (!empty($row['img'])) {$img = $row['img'];}
$width = imagesx(imagecreatefromstring($img));
$height = imagesy(imagecreatefromstring($img));
return $width . " x " . $height;

Solution

  • Use imagecreatefromstring and imagesx.