Hello, I have a form where I upload images to my MySQL DB, and they are stored in BLOB format.

I can get the images to display separately through a link to a different page.

Although what I would like to do is have preview of each image on the first page - not sure if this is possible though due to header information.

Any ideas please, or ask if you dont understand.

<table class="sample">
		<tr>
			<th>Image</th>
			<th>Filename</th>
			<th>Type</th>
			<th>Description</th>
			<th>Download/Delete</th>
		</tr>
		<?php
		if ($filelist) 
		{
		while ($f = mysql_fetch_array($filelist)) 
		{
		?>
		<tr valign="top">
			<td>
				<?php echo $f['filedata']; ?>
			</td>
			<td>
				<a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=view&id=<?php echo $f['id']; ?>">
				<?php echo $f['filename']; ?></a>
			</td>
			<td><?php echo $f['mimetype']; ?></td>
			<td><?php echo $f['description']; ?></td>
			<td>
				<a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=dnld&id=<?php echo $f['id']; ?>">Download</a> |
				<a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=del&id=<?php echo $f['id']; ?>" onclick="return confirm('Delete this file?');">Delete</a>
			</td>
		</tr>

	<?php
	}
	} 
	else 
	{
	?>
	<tr><td colspan="3">No Files!</td></tr>
	<?php
	}
	?>
</table>

    Each image will have to be served up separately, since the browser will call each via the src attribute value of its relevant img tag. So you will need an "image server" script, probably using $_GET to have the desired image name/id passed to it. For example:

    The HTML:

    <img src='image.php?id=123' alt=''>
    

    The image.php file:

    <?php
    header('Content-Type: image/jpeg'); // or whatever type of image it is
    if(isset($_GET['id']))
    {
       $id = (int)$_GET['id'];
       // now query the DB based on $id to get image data, then...
       if(<some condition that indicates image was found>)
       {
          echo $variable_with_image_data;
          exit;  // so no more data is accidentally output
       }
    }
    // if we got here, something went wrong, so...
    header("HTTP/1.0 404 Not Found");
    
      <img src='image.php?id=<?php echo $f['id']; ?>' alt='Error - no image in DB' width="50" height="50">

      Above code in the while loop.

      Resizes the images so that they are distorted but that dont matter - least its a preview.

      Thanks for the help.

        17 days later

        Johny7126,

        I'm someone who's stuggling to get my image from a blob field in mysql to display on a webpage for a long while. All that I am getting are hieroglyphs. At this point, I am able to upload an image to a database, and even download to save on a desktop. But this is not what I want. To make everything work, what type of source code for the table, upload page, image.php file, and the preview.php file, so that I can get my image to show?

        Thank you so much,
        Luvjazz

          Hi Johny,

          Just some thoughts about the solution you found:

          The width and height properties in your <img>-tag are just properties to display the image. However, the full image data are sent over the network. Imagin you have a preview page of 50 pictures, making 2 MB each, you need to send 100 MB, just to display some thumbnails !

          Maybe some solutions to work around this problem:

          • you might want to store a thumbnail preview of you full image also in the database; if your server has the appropriate image-manipulation-tools installed, you could do it on-the-fly while uploading the image. This way, you do the job only once.

          • Preferrably, you could size the thumbnail with appropriate height/width ratio

          • Eventually, it might be possible to generate the thumbnail on-the-fly while generating preview content, instead of leaving this to the browser. keeping width/height ratio wouldn't be much trouble in the same step.

          I'd prefer the first solution: it won't eat up much more storage than storing the full picture, and resizing has to be done only once. If you resize image at each retrieval, this might be an issue depending on the performance of your server, as well as downloading full-size pictures might be an issue with available bandwith or amount of transferrable data (if you need to pay for each MB, you might run into trouble ...)

          It is up to you to think about all this, because you know approximately the number of pictures to treat, the number of page calls, you storage or bandwidth capacity, etc.

          HTH

          Greetings,
          Jean-Jacques

          (I already did some reflections about doing this resizing stuff, but so far I did not yet write one single line of code, so I can't give you a plug-and-play solution for it ...)

          See the manual of PHP functions about "Exif functions" and "Image functions"

            Write a Reply...