So i think this question has been addressed before but none of the answers seem to help me.
I have uploaded a .jpeg file to my database by extracting the content from the file and uploaded that to a BLOB field in my database. When it comes to retrieving the data I search the database using an id set by the query string ?id=146 (I've inserted a value that relates to a specific entry in my database just to check the functionality). When I then echo the image['image'] the binary data displays fine (well the browser renders the data how it interprets it) so its finding the entry I want, and it displays the data fine. When I add header("Content-type: image/jpeg"); and reload the page the browser tells me the image is missing. ![screen shot of missing image display][1]
The entire functionality will work as i will reference the php file in the src of an tag on the page I want the image to display. But I can't even get the image to load onto the php page when I type in its URL(inc the correct query string).
Here is a my code for the php page to find the image:
$id = intval(addslashes($_REQUEST['id']));
header("Content-type: image/jpeg");
$result = $dbh->prepare("SELECT * FROM `cefk_profile` WHERE `id`=$id");
$result->execute();
$image = $result->fetch(PDO::FETCH_ASSOC);
echo $image['img'];
And here is a shot of the page that will display the picture:
echo '<img src="php/get_img.php?id=146">';
Related
GOAL: I'm trying to display an Image into my Laravel 7 view, retrived directly from database.
In my MySQL WORKBENCH:
PROCESS:
As you can see in my controller JobsController, I will select and retrieve the image from database, identified by jobs $job_name.
As you can see here, I tried to dd($job_name) and we can see that BLOB data in $job_image variable. I will pass this Collection object to view and display it directly.
Most SO questions relate to displaying the image using <img src="myFileName"/>.
Problem is I'm not storing it as a file but instead just retrieve and display it directly. I just want to dump all the variables into my view file like other job details.
Solutions tried in my view file.
<img>{{ base64_encode($job_details->job_image) }}</img>. Failed. Output is "/9j/4AAQSkZJRgABAQ".
<img>{{ base64_decode($job_details->job_image) }}</img>. I don't have much understanding about what encoding and decoding happens when data is stored and retrieved from and to the database. Here I decoded the variable. No output.
<img src="{{ $job_details->job_image}}"/>. This is dumb, src attribute expects an PATH.
So there, none of these would turn the variable content into an image.
Please tell me what I am missing here.
Try this
$image = 'your_image_blob_name';
$imageData = base64_encode(file_get_contents($image));
$src = 'data: '.mime_content_type($image).';base64,'.$imageData;
echo '<img src="'.$src.'">';
You're almost there. You have to use the base64 string in the src attribute. Make sure you also include the data type and other relevant information at the beginning. See: How to display Base64 images in HTML?
This question already has answers here:
Convert HTML to image in php
(2 answers)
Closed 3 years ago.
I want to make a some step of saving data into DB as BLOB data type. I need convert data to BLOB in the DB. I have the data that are considered as HTML output in the some file. The selected programming language is PHP with Zend Framework 1 and database is MySQL.
I tried to save HTML page as valid BLOB image in DB, but it is still not working. I can't find any solutions about it and I don't know how to do. When I got a content from URL and saved it into DB as BLOB by using function file_get_contents($url), that is worked to me fine.
Here is a code that saves the content of the HTML page in the file described below into DB as BLOB.
$file = "C:\test.html";
$fp = fopen($file, 'r');
$content = fread($fp, filesize($file));
fclose($fp);
$dbModel = new MyTable($this->db);
$dbRowSet = $dbModel->find(1);
$dbRow = $dbRowSet->current();
$dbRow->map = $content;
$dbRow->save();
Here is a preview of saved HTML page in DB, but there I can't show any BLOB image.
There is shown BLOB image. I had the URL page and tried to save it into DB.
I expected that the result of saving data (HTML page) into DB should be that DB contains valid BLOB images.
Thank you for your help.
You can download wkhtmltoimage from this link. There is a version for all operating systems so that shouldn't be a problem. Then you can use it like so:
$path="wkhtmltoimg/wkhtmltoimage.exe"; //path to your executable
$url="http://google.com";
$output_path="test.png";
shell_exec("$path $url $output_path");
One thing you want to note is that if PHP is in safe mode, shell_exec will not work and you won't be able to do your conversion.
Credit: #Tom
Convert HTML to image in php
After this you just need file_get_contents() and save blob in the database.
I've create a table where I've saved images through "BLOB". I need to show those images along with other items. But I don't know how to show those images together in the same page.
Here's my php code that displays other things in form of a table. Similarily, I wanted to display images accordingly. Any help?
<?php
$display_query = mysql_query("SELECT * FROM eportal");
echo "<table id='pageTable'><thead><tr><th>Item code</th><th>Description</th><th>Cost</th></tr></thead>";
echo "<tbody>";
while($row = mysql_fetch_array($display_query)){
print "<tr><td>".$row['itemid']."</td><td>".$row['description']."</td><td>";
print "₹".$row['cost']."</td></tr>";
}
echo "</tbody>";
echo "</table>";
mysql_close($connection);
?>
Saving images to the DB is not a good idea but if You think You need to it this way, then You can retrieve the data from DB table, encode it to base64 (http://php.net/base64_encode) and then in HTML print it in this way:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
Using PHP You would write:
echo '<img src="data:'.$image_mime_type.';base64,'.base64_encode($image_data_from_db).'" alt="My image alt" />';
As other people mentioned, storing the images in the database is usually a bad idea.
Images are not transmitted in the same HTTP response with another page data.
To show images from the database, you would need to implement a script which, given the item id, would read the field and send the image's binary data; and provide the path to the script in your form's <img src="">:
while($row = mysql_fetch_array($display_query)){
print "<tr><td>".$row['itemid']."</td><td>".$row['description']."</td><td>";
print "₹".$row['cost']."</td><td>";
print "<img src=\"image.php?id=".$row['id']."\"></td></tr>";
}
image.php is another script which outputs the value of image_blob given the eportal.id. You would also need to provide correct size and mime type in the headers.
You better just store the images in a file (accessible by the web server) and store the path fo the file in the database.
Read the Blob data and write it into the file with header type image.. and try to print it, It should display the image file.
And yes saving image or any file in DB is really a bad habit as you are increasing DB size and it slowdown the performance also.. I suggest you to just try to convert you Blob into Image but don't apply in your work. Just save the image at desired location and keep its location path into DB to fetch and save next time.
The debate of storing blobs versus storing a path to the image file on disk has been debated over and over again. Microsoft provides a research paper comparing the pros and cons of each here. With that said, to display a blob as an image you need to make a call to a separate page and output header information that tells the browser what type of image is stored.
For example:
connectToDatabase();
$sql = "SELECT image_blob FROM eportal;";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: image/jpeg");
echo $row['image_blob'];
$db->close();
In case you still want to save your images in database, you will need second script which will get those images from database and pass them to browser with correct headers.
header("Content-type: image/jpeg");
#
# Replace this with database read:
# readfile('myimage.jpg');
Also, you will need to store what kind of image u use. There will be different header for JPEG, GIF or PNG file.
i want to read .ico-Images from a Website, store the Information to the Database and show the Images later on a global Website.
I have managed to save the images in a string into the Database. The step to show the Images on a Website is my problem.
For reading the Contents:
$data=file_get_contents("http://www.google.com/favicon.ico");
$data = base64_encode($data);
What is the right way to show that image in a single div on a website?
Fesp
You basically need to tell your script to output the content as an ico type.
<?php
//Getting your image
$data=file_get_contents("http://www.google.com/favicon.ico");
$data = base64_encode($data);
//If your storing in the db then you do the query ect togo get the data string
//Then echo out like this
header('Content-Type: image/vnd.microsoft.icon');
echo base64_decode($data);
?>
Remember you cant output anything before setting the header so perhaps you will need a seperate script to grab the file and ouput so
<img src="get_ico.php?id=1"/>
Then in get_ico.php
<?php
//Connect db ect
//Query db for $_GET['id']
//Then echo out like this
header('Content-Type: image/vnd.microsoft.icon');
echo base64_decode($row['image_data']);
?>
I'm trying to make a database that uploads the pictures and shows them, sorta like a gallery. It uploads them but the problem is where the pictures should be it gives me this strange symbol ( sorry can't post it because I'm new :| ) and I can't tell if this means it just refuses to show them, or something went wrong. Help?
<?php
mysql_connect("localhost") or die(mysql_error());
mysql_select_db("images") or die(mysql_error());
$id=addslashes($_REQUEST['id']);
$image=mysql_query("SELECT * FROM dadsda WHERE id=$id");
$image=mysql_fetch_assoc($image);
$image=$image['image'];
header("Content-type: image/jpeg");
print($image);
?>
At no point in that code is the image actually output.
If image is a BLOB field in the database, you'd need to do print $image; after the header() call. If it's a filename/path, you'd need to use readfile() to output the contents of that file.
Also, this code is vulnerable to SQL injection. If I go to script.php?id=1%3B+DROP+TABLE+dadsda%3B it'll delete your database table because I just made your code execute the SQL query SELECT * FROM dadsda WHERE id=1; DROP TABLE dadsa;.
sometimes if the image box is empty my jpeg is not defined in your mime types on server side.. just right click on the empty image box and give me the properties...
regards..