Im creating a blog and the little bit of code below is where the blog geets printed out.
I've got a blob saved in my mysql database and im trying to turn it back into an image.
the imageName, imageType, imageSize, imageContect all receive values when i run my code. The problem is that the imageContent variable displays a load of random characters rather then an image. it seems that the reason for this is the headers but i've no idea what to do. can anyone help me to recode the image. thanks
while($row = mysql_fetch_array($result))
{
echo "name ".$row['imageName'].'<BR>';
echo "type ".$row['imageType'].'<BR>';
echo "size ".$row['imageSize'].'<BR>';
echo '<B>'.$row['blogTitle'].'</B><br />';
echo '<A HREF = http://www.alcaeos.com/blog/displayblogProcess.php?mode=edit&blogID='.$row['blogID'].'>Edit</A> ';
echo '<A HREF = http://www.alcaeos.com/blog/displayblogProcess.php?mode=delete&blogID='.$row['blogID'].'>Delete</A><BR />';
echo $row['blog'].'<br />';
header("Content-length:".$row['imageSize']);
header("Content-type:.".$row['imageType']);
header("Content-Disposition: attachment; filename=".$row['imageName']);
echo $row['imageContent'].'---------<br /><br /><br />';
}
Here is a fairly simple example of displaying an image stored as a blob.
<?php
require_once ('./includes/db.inc.php'); // Connect to the db.
//let the browser know its an image
header("Content-type: image/jpeg");
// Make the query.
$query = "SELECT thumbnail FROM items where item_id=" . $_GET['item_id'];
$result = #mysql_query ($query);
if ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo $row['thumbnail'];
}
mysql_close(); // Close the database connection.
?>
Skip the Content-length and Content-disposition, just use Content-type and set it to a valid MIME type.
Assuming they're JPEGs, you'd do this:
header( 'Content-type: image/jpeg' );
Check out this link too
Related
I tried to display image stored in oracle database
I get it as decode data
I tried to this code but not work
first way
$img= studimage::select('studimage')->where('studnum',$id)->first();
header("Content-type: image/jpeg");
echo ($img->studimage) ;
sec-way
echo '<img src="data:image/jpg;base64,'. base64_encode($img->studimage). '" />';
the two ways does not work :(
I'm using this same functionality in one of my project, please check my code below
you can either make a page that will render the image
<img src="image.php?id=123" />
That image.php page would have this:
$sql = "SELECT image FROM images WHERE image_id = " . (int) $_GET['id'];
$stid = oci_parse($conn, $sql);
oci_execute($stid);
$row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);
if (!$row) {
header('Status: 404 Not Found');
} else {
$img = $row['IMAGE']->load();
header("Content-type: image/jpeg");
print $img;
}
Or, you could base64 encode it into the src (note, not all browsers handle this well):
<img src="data:image/jpeg;base64,<?php echo base64_encode($img); ?>" />
I was trying to figure out how to upload an image into mysql then display it. I got the uploading part set. The problem I am have is displaying it. When I go to display it does one of the following:
Displays no image but only has a little image icon 10x10px image on top left
Displays the whole string of the image and then properly renders the image.
When I remove/comment out the echo $contentIMG statement, nothing displays.
<?php
include 'connections/conn.php';
error_reporting(E_ALL);
// some basic sanity checks
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
$contentIMG = $row_Recordset1['content'];
echo $row_Recordset1['content'];
//echo $contentIMG;
// set the header for the image
//header("Content-type: image/jpeg");
header("Content-type: image/jpeg\n\n");
echo "where is the image #1<br>";
//echo '<img src="data:image/jpeg;base64,'.$contentIMG.'"/>';
echo "where is the image #2<br>";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $contentIMG ).'"/>';
if (isset($contentIMG)){
//echo '<img src="data:image/jpg;base64,'.base64_encode($contentIMG).'" />';
}
//echo mysql_result($result, 0);
// close the db link
mysql_close($result);
}
else {
echo 'Please use a real id number';
}
echo "break <br>";
$img=base64_encode($row_Recordset1['content']);
?>
I looks like this
enter image description here
I may be wrong but I think that when you set your header Content-type to image/jpeg, you should just return the image data (assuming it is stored as a blob in your database)
<?php
header('Content-type: image/jpeg');
echo $contentIMG;
?>
Your code should look like this:
<?php
include 'connections/conn.php';
error_reporting(E_ALL);
// some basic sanity checks
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
$contentIMG = $row_Recordset1['content'];
header('Content-type: image/jpeg');
echo $row_Recordset1['content'];
// close the db link
mysql_close($result);
}
else {
echo 'Please use a real id number';
}
?>
i have meet all pages and did implement, but did not get result what i am looking for. Here is the database picture at this link : "http://www.uploadmb.com/dw.php?id=1389533907", that i want to get and display. I have upload code. Now i am trying to display. That is the code which from this forum: "p" is the name of myfolder.
<?php
$con=mysql_connect("localhost","root",'');
mysql_select_db("project",$con) or die("error db");
$sql="select * from upload";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query))
{
$image=$row ['name'];
echo '<img src=\"p/'.$image.'" width="360" height="150">';
}
?>
To display an image stored as a blob you need to read it from the database then either include it in the users browser as a data uri or print the binary data along with the appropriate headers.
DataURL:
echo '<img src="data:image/png;base64,' . base64_encode( $row['content'] ) . '" />';
Or where $size is from length(content) in the SQL query:
header("Content-type: image/png");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=myimage.png");
print $row['content'];
Try like this...
echo ("<img src='path/$image width='360' height='150' />");
Try this:
echo "<img src=\"/p/$image\" width=\"360\" height=\"150\">";
mysql_* is deprecated, use msqli_*
$cn = mysqli_connect("localhost","root","", "project") or die('Connection error');
$result = mysqli_query($cn, "SELECT * FROM upload") or die( mysqli_error($cn) );
while( $row = mysqli_fetch_array($result) )
{
printf('<img src="p/%s" width="360" height="150">', urlencode($row['name']));
}
mysqli_free_result($result);
mysqli_close($cn);
I'm new to php. I have a sample mysql db in that I have a table named testdb with columns id(INT) and image(BLOB). I have uploaded an image into testdb. Uploaded successfully. The following is the php code. The variable $conn contains the connection details. I have a html page which redirects to this php page on submitting.
<?php
$name = $_FILES["sample"]["name"];
echo $name . "<br/>";
$tmp_name = $_FILES["sample"]["tmp_name"];
echo $tmp_name . "<br/>";
$size = $_FILES["sample"]["size"];
echo $size . "<br/>";
$contents = file_get_contents($tmp_name);
$htmlen = htmlentities($contents);
$cont = mysql_real_escape_string($contents);
$query = "INSERT INTO testdb(image)
VALUES ('$cont')";
$dbquery = mysql_query($query, $conn);
if($dbquery){
echo "successfully inserted";
}
else{
echo "could not inserted" . mysql_error();
}
?>
I am trying to get the image with the following code. But it is showing string characters rather than the image. As far as I know this should work fine.
<?php
$query = "SELECT image, id
FROM testdb ";
$dbquery=mysql_query($query , $conn);
if(! $dbquery){
echo "Could not selected the data from database. " . mysql_error();
}
while( $row = mysql_fetch_array($dbquery) ){
$decodeimg = html_entity_decode($row["image"]);
echo "<img src= $decodeimg/><br/> hellow orld <br/>";
}
?>
Could anyone help me with this. Thanks in advance.
Instead of storing the actual image in your database (which is redundant because it is probably stored on your server too); why don't you just store the PATH to the image as a string, query the string from your db and then append it to the 'src' attribute with php.
I am also got the same error when show the BLOB image from DB. I just use the decoding method for this problem....
$photo=$myrow['image'];
echo '<img src="data:image/jpeg;base64,' . base64_encode( $photo ) . '" width="150" height="150" />
thank's for help. I have problem displaying images retrieving from my database.
I cant see the image when loading image.php in img src or directly from the page. When i display the variable without header('Content-type: image/jpeg'); i can see all the code inside, as i put this line all goes off.
I have a table called TABLE with id, title, img stored as longblob directly uploaded inside phpmyadmin.
Can anyone help me?
index.php
<?php
session_start();
include "admin/include/connection2.php";
$data = new MysqlClass();
$data->connect();
$query_img ="SELECT * FROM table ORDER BY data ASC LIMIT 4";
$post_sql = $data->query($query_img);
if(mysql_num_rows($post_sql) > 0){
while($post_obj = $data->estrai($post_sql)){
$id = $post_obj->id;
$titolo = stripslashes($post_obj->title);
$data_articolo = $post_obj->data;
$immagine = $post_obj->img;
// visualizzazione dei dati
echo "<h2>".$titolo."</h2>";
echo "Autore <b>". $autore . "</b>";
echo "<br />";
echo '<'.'img src="image.php?id='.$post_sql['id'].'">';
echo $id;
echo "<hr>";
}
}else{
echo "no post aviable.";
}
// here is the image.php code
<?php
include "admin/include/connection2.php";
$data = new MysqlClass();
// connect
$data->connetti();
$id = $_GET['id'];
echo $id;
$query = mysql_query("SELECT * FROM articoli_news WHERE id='".$id."'"; //even tried to send id='1' but not working
echo $query;
$row = mysql_fetch_array($query);
echo $row['id']; //correct displaying
$content = base64_decode($query['img']);
header('Content-type: image/jpeg');
echo $content;
?>
Delete all "echo" commands except "echo $content;" because there are also appear in the output, and damage your image.
And use ob_start(); in the begining of the script, and check out your script file not contain any of whitespace characters before or after the php begint and close tags .