0

I'm working on a project that involves retrieving images my code have no errors, but the images are displayed as image icon. Only one image is displayed.

$query = "SELECT * from cbir"; $result =mysql_query($query) or die('Error, query failed'); while($row = mysql_fetch_array($result)){ if (abs($red_count - $row['red_count'])> 50 && abs($blue_count - $row['blue_count'])> 50 && abs($green_count - $row['green_count'])> 50 ){ header("content-type: image/jpeg"); echo $row['image']; } } 
3
  • 2
    with the information provided this is impossible to answer - sorry. Commented Dec 16, 2013 at 16:28
  • 1
    http responses normally contain only a SINGLE item. some html, or an image, or a pdf, whatever. You're fetching/outputting in a loop. Your header() call will FAIL after the first output image, and the rest of the data will simply be seen as random garbage after the first image's data is displayed. Commented Dec 16, 2013 at 16:30
  • I could not add the result image because I need to have at least 10 reputation. Commented Dec 16, 2013 at 16:58

2 Answers 2

0

Try this

 $image = base64_decode($thumb); /** check if the image is db */ if($image!=null) { $db_img = imagecreatefromstring($image); Header("Content-type: image/jpeg"); imagejpeg($db_img); } exit; 
Sign up to request clarification or add additional context in comments.

4 Comments

you're assuming OP's stored the image base64-encoded. With a blob, b64 encoding is a hideous waste of space, as it blows everything up by about 33%
i assume the image is stored in blob
yes, and blobs are for BINARY storage. there's no need to b-64 encode for a blob.
It works for me.Thanks i will keep in mind in future
0

You need to create a PHP script that will get an image based on it's unique ID, lets call it imageId for the sake of this answer, and lets pretend like this is in a PHP file called image.php. This would look something like:

$query = "SELECT * from cbir WHERE imageId = ".mysql_real_escape_string($_GET['imageId']); $result = mysql_query($query) or die('Error, query failed'); while($row = mysql_fetch_array($result)){ header("content-type: image/jpeg"); echo $row['image']; } 

You'd then need to do the following in your display script.

$query = "SELECT * from cbir"; $result = mysql_query($query) or die('Error, query failed'); while($row = mysql_fetch_array($result)){ if (abs($red_count - $row['red_count'])> 50 && abs($blue_count - $row['blue_count'])> 50 && abs($green_count - $row['green_count'])> 50 ){ echo '<img src="image.php?imageId='.$row['imageId'].'" />'; } } 

Can I also just point out that you shouldn't really be using the mysql function any more, you should be using mysqli at the very least, but preferably PDO.

2 Comments

I want to retrieve similar images. The user will upload an image and the count of r, g and b will be extracted to compare it with stored r, g and b for each image in database. the images with the smallest diference will be displayed. I cant compare using the id.
Sorry, that was my bad. Edited now to use your original query for getting an image id. Copy paste error on my part. The select * query will return all images to do your rgb comparison but just make sure you have a unique image id per image that image.php can use to load the single image as part of the src attribute on the image tag.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.