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'];
}
}
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
user3108024
  • 25
  • 2
  • 9
  • 2
    with the information provided this is impossible to answer - sorry. – Horen Dec 16 '13 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. – Marc B Dec 16 '13 at 16:30
  • I could not add the result image because I need to have at least 10 reputation. – user3108024 Dec 16 '13 at 16:58

2 Answers2

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;
amarjit singh
  • 463
  • 5
  • 14
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.

Garry Welding
  • 3,599
  • 1
  • 29
  • 46
  • 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. – user3108024 Dec 16 '13 at 16:54
  • 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. – Garry Welding Dec 16 '13 at 21:43