Hi,
hope you can help me. I am working with a database (mysql) already created, pictures were stored in a longblob field, but this is the only field related to pictures (so I have no info about their size).
I manage to extract and display the pictures - using a php file like this: poza.php

<?php
ob_start();
ob_clean();
include'conn.php';//connects to server and select db
if (IsSet($_GET['imgid'])){ //imgid is the id of the row	
$img_id =$_GET['imgid'];
$query = "SELECT * FROM angajati_eurotire WHERE id_ae=$img_id";
$result = mysql_query($query); 
header("content-type: image/jpeg");	
while ($row = mysql_fetch_array($result)) 
{ print $row[poza]; } //poza is the column where images are stored
mysql_free_result($result); 	
}
ob_end_flush();
?>

Then I display the picture using <img src="poza.php?imgid=[ID]">.
This works ok, but I need to display this pictures as thumbnails (about 30% of their actual size). The pictures have random sizes.
Is there any way I can determine the image size?
Or maybe I'm going on a wrong track with all this?
Thanks for your time.
Ina

Dani AI

Generated

Summary of what worked and a next-step recipe.

already proved the main approach: extract the raw blob from MySQL and turn it into an image resource to read dimensions. was on the right track with getimagesize() but that function expects a file/URL, not raw binary — which is why it failed.

A concise, robust way to get size and MIME directly from the BLOB is getimagesizefromstring(). Example flow (assumes $blob is the binary from the DB):

$info = getimagesizefromstring($blob);
$width  = $info[0];
$height = $info[1];
$mime   = $info['mime']; // use this for the Content-Type header

Use the width/height to compute a proportional thumbnail size (scale by the smaller ratio of max-width/max-height). For resizing, use GD (create an image from the string and imagecopyresampled) or ImageMagick for large images. Generate the thumbnail once and cache it (filesystem or a thumbnail column) so you do not reprocess on every request.

Practical tips and cautions:

  • Send the correct Content-Type header based on the detected MIME, not a fixed image/jpeg.
  • Avoid the old mysql_* API and raw $_GET interpolation. Cast IDs ((int)$_GET['imgid']) or use prepared statements (mysqli/PDO).
  • Watch memory/time limits when manipulating large images; prefer ImageMagick for very large files or increase memory for controlled environments.
  • Add HTTP caching headers (Expires/Cache-Control or ETag) for served thumbnails to reduce load.

References: getimagesizefromstring and imagecopyresampled for resizing guidance.

Recommended Answers

All 3 Replies

I am guessing the below code might work but I am not sure as you are reading from a variable and not a file. So try the following and if it does not work, you will need to use something simular but use the gd library/binary to allow the

list($width, $height, $type, $attr) = getimagesize($row[poza]);

The above code theoratically stores the width and height in the variables $width and $height which you can call after that line has been executed/ran. If you are wondering where to place it, place it after the below couple of lines:

while ($row = mysql_fetch_array($result))
{ print $row[poza]; }

As I said it might not work and might need some sort of conversion but it's worth a try as that is in the official documentation.

Thank you for your answer, but unfortunately I already tried getimagesize(); it is not working here (I use it in other pages, where I have the pictures stored in a folder on the server and I only have their address stored in the database).
Maybe there is another way to use the blob field?
The way I did it it only publish the picture...
Thanks anyway,
Ina

I used this:

$image_data=trim($ra[poza]);

$a=imagecreatefromstring($image_data);
$wo = imagesx($a);//width
$ho = imagesy($a);//height

then i used these values to display the picture at any maximum w and h, constraining proportions
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.