开发者

displaying an image stored in a mysql blob

开发者 https://www.devze.com 2023-02-21 09:18 出处:网络
when i run the code below it displays an image that is stored in a mysql Db as a blob variable. The pro开发者_JAVA百科blem is if I echo out anything else, even something as simple as echo \'--------\'

when i run the code below it displays an image that is stored in a mysql Db as a blob variable. The pro开发者_JAVA百科blem is if I echo out anything else, even something as simple as echo '--------'; before I call the image, the image will not display. only random characters display. if i echo out anything after the image the image displays but nothing does after it. Can someone tell me why this is and how i can go about displaying other items and the image together, thanks

<?php

include("inc/library.php");

connectToDatabase();

$sql = "SELECT * FROM theBlogs WHERE ID = 1;";

$result = mysql_query($sql) or die(mysql_error());  
$row = mysql_fetch_array($result);

header("Content-type: image/jpeg");
echo $row['imageContent'];
$db->close();

?>


You can convert the image data into base64 and stick it in an <img> tag. Currently, you are trying to write text outside of the image data, and the internet browser thinks your text is part of the image and thus throws an error.

Try something like this:

echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['imageContent'] ) . '" />';
echo 'Hello world.';

Note, this isn't the best solution because it cannot be cached and is fairly slow, especially on mobile phones. Check out the caniuse for data URIs.


Well...the answer to why it will do what you described is because of your use of the header() function. In PHP, you cannot print anything prior to a header call as this directs the web server to prepare a content header. Usually those completely supersede all content.

Secondly, I would like to mention that storing images in a database is usually a bad idea for two reasons.

  1. It has a significant impact on performance and rendering.
  2. You have to write code render the blob data rather than just displaying the image itself.

The preferred method for database driven image presentation would be that you would have the images stored in a directory and their filenames stored in the database. Now, when you wish to display the images, you merely need to poll the DB for what filenames you want to display and then simply include the filename into an HTML attribute.

The execution is much faster also.

Also, I would like to point out that if you wish to have a script actually do your rendering, you would want that script to define your header and then echo or print the image blob after you define the header.

Please note that when you create your html tag...that in the src attribute, you would then make it something more like this;

<img src="image.php?id=<some_number>">

Now, your image.php file will spit out the image data into the tag.


Just a idea, maybe you can seperate the logic to display the image from the content. What I mean is that you could create a file like pic.php that accepts the id/filename (Not sure if you use filenames or id's to refer to the image) and then displays the image. Then you could simply refrence images in your HTML by for example doing something like this:

<p>my content</p>
<img src="pic.php?picid=1" />
<p>my Other Content</p>
0

精彩评论

暂无评论...
验证码 换一张
取 消