I have some images in a MySQL Database, each stored in BLOB fields. How can I evaluate the width and height of the images in pixels?
Asked
Active
Viewed 5,041 times
3
-
Use [imagecreatefromstring()](http://uk3.php.net/manual/en/function.imagecreatefromstring.php) to turn the blob data into an image, then use [getimagesize()](http://uk3.php.net/manual/en/function.getimagesize.php) to retrieve its dimensions. – r3mainer Feb 10 '14 at 13:41
-
may be while storing the image in DB , you can get the size and store it in DB also .That will reduce the trouble . Simple – Dimag Kharab Feb 10 '14 at 13:41
-
@CodingAnt unfortunately this is not an option, as there already are hundreds of images in the database. – tmuecksch Feb 10 '14 at 13:43
-
@squeamishossifrage thanks for the advice. I'm gonna try this. – tmuecksch Feb 10 '14 at 13:43
-
1i am curious why you have opted to store images in a database. Why not store the file locally ? It's not only faster, but more easier to manage ... – KarelG Feb 10 '14 at 13:44
-
@KarelG You're right. I share your opinion. I'm working for a client on a very very old code and I've been asked to only make minor changes to the code. – tmuecksch Feb 10 '14 at 13:50
1 Answers
3
Let's say you have retrieved the image from the blob field in the database via a mysqli_query and assigned it variable $image. You can use this code:
$im = imagecreatefromstring($image);
$width = imagesx($im);
$height = imagesy($im);

SyntaxLAMP
- 975
- 8
- 11
-
Hm. Unfortunately this doesn't work for me right now as I get this error, I can't interpret: ----------------------- imagecreatefromstring(): gd-jpeg: JPEG library reports unrecoverable error: in line 38 of file ImgDoc.inc.php | Only content in this line is: $img = imagecreatefromstring($imageArray[$key]["Image"]); – tmuecksch Feb 10 '14 at 13:57
-
You may need to set the memory limit higher. Here is another page that may help fix this error:http://stackoverflow.com/questions/10611883/warning-imagejpeg-functionimagejpeg-gd-jpeg-jpeg-library-reports-unrecov – SyntaxLAMP Feb 10 '14 at 14:38