Firstly this is for a simple university project, so time is more important than performance of database etc.
I have a database with images stored in blob files. Now some form outputs data from this database, but i want it to also output the image, along with the text.
I realise there is a bit of code to change headers to image, but then the text wont display, so is there a way to display the image and the text on the same page?
thanks a lot,
It is possible to embed images inside HTML, but you usually do it only if you have a really good reason to. Assuming you do, you write the following code, provided that $BLOB is the binary raw data of the image:
<img src="data:image/jpeg;base64,<?php echo base64_encode($BLOB); ?>"/>
Why not use image tags like normal wherever you need the images to be, and in the src attribute, specify a PHP script along with a get argument (for instance, <img src='image.php?name=something.jpg'>). This script then authenticates the request, accesses the database, sends the appropriate headers, followed by the actual BLOB data.
Related
Well in a particular website when I upload the image and then try to get the link of the image I get this:
data:image/svg+xml;base64,thebase64encodeOfMyImageCode
I have seen this in many of the website. After decoding the base64 code I find that its the code of my image file (guessed out by uploading svg image file ) . Can any one tell me how this work and where the image is actually being stored . There is no direct URL like website.com/image.svg etc. This is confusing me.
This image is not stored in a file. All the data for the image is stored in the data URL. The server most likely is storing this data in a database, which is then outputted in the webpage inline.
For more information, see MDN
When base64 encoding is used for the image - you simply cannot know where the actual image is stored, if it is available at all. It might not be stored on a file at all, but only on a database, or might be stored outside the domain. On some occasions, such images are generated in real time and not stored on a disk at all.
I would appreciate your thoughts on this method.
I have a page that uploads a pic given by user and displays it after saving it in a database now that's very simple so, I wanted users to be able to modify these images as they wish, hence I decided to add filters to these images using php. Now before these images are uploaded to the database I want the user to see a preview of what the image looked like after adding the filter.
The solution I came up was to send the image to php file using a form and AJAX using POST, do all necessary checks if file was safe. create a new image and add filter and then convert it to base 64 and send it back to browser and display it using a DATA URL
something like:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
Is this a good idea or there is something I should worry about?
Base-64 encoding adds about 33% overhead. For this reason, it is usually preferable to serve the image in binary form.
You can do this with a PHP script easily.
header('Content-Type: image/png');
echo $yourImageData;
Then in your HTML:
<img src="yourPHPScript.php?id=12345" />
hello we have a library for that actually, we make use of GD library to be able to achieve the correct image output that we would like to have, you can check more about our image library here.. by the way, our library does not only contain image library.. it has a bunch of useful libraries that you can actually use on your projects and we opensource it
http://eden.openovate.com/documentation/core/images
I'd like to know if it's possible to assign image data as text to an image in HTML form instead of setting it's "src" property to a file path... i want to do it with PHP!...
i remember seeing something like the following code in some websites' source...
for example:
image data = R6+1u5jwhwf6GOG6X6MpFR/hrlbNA1JcqeByPKDIivcJQa2ePIft0Jqewk4/lLYSy4YU1BXARkvdN7vJxx0vUOJGiU5OiMhMhWrH6s1n3pGK0Sat0mMiUCQX4e4BDU+yD1kB87tI+Xh+WitqNN7FyLysoGlAvsGfZQ2bOo+7+7Bm6K4NMktamfNG9v
in this way... by viewing the source of my webpage... it's not possible to see the address of the images used! just it's data! i think it's more secure! MAYBE!
Thanks!
It is not more secure as the picture has to be created in the browser to be displayed by the user.
What you could do (which won't make it any more "secure"), is to have a PHP script that translates your "data" into an image, see this thread for an idea on how to do this!
I think what you're referring to is Base64 encoded images in HTML.
It's not very portable though, not nearly as much as a standard img tag.
if you have image data you can show image on html page like this <img src="data:image/png;imagedatacodehere" /> , if it has png extension, check this link
Yes, it's possible, no it's not more secure. You're talking about data uris, and since the user's browser has to have the image data in order to display the image, you're still sending the picture to the user. All they have to do is right-click/save as on the displayed and boom, done. Also, the image data is just embedded in base64 format into the page itself, meaning you can trivially cut/paste that base64 string and still steal the image, even if you have all kinds of pointless image protection (e.g. right-click disablers).
Initially i added full source code for viewer. but it has some format problem and all the code was messed with HTML format. So now i am asking step by step.
1) I uploaded an image file to my MySQL database. (it has no error, i.e. loaded successfully)
2) Fetching it back to the browser from MySQL Database. It has also no problem(in some condition)
a) It is printing OK if i am printing it before the HTML Tag.
b) I am not able to see any basic HTML design after printing image on browser.
c) If i am printing it inside html tag. It is showing some special characters and numbers, i.e. value which we see in phpmyadmin if we execute the query (select image from pix;)
So, Can any one help me to print image by fetching the image from MySql database?
Or can any one convert this database saved image from MySQL into real image to store into hard-disk back to see/edit it?
You are making a fundamental mistake here. An image resource is always stored and requested in a separate file, not in the HTML source code of the page you want to embed it in.
Such a separate file could be named, say, getimage.php.
It would fetch the image data from the database (e.g. from the record with the ID 890) and output it like e.g. so:
... code to fetch the image ....
header("Content-type: image/jpeg"); // or image/gif, image/png....
echo $imageData;
In the HTML page, you would embed it using
<img src="getimage.php?id=890">
There is the theoretical possibility to have image data directly in the HTML source but that's not what you want.
The answer is simple:
Do not store images in the database.
Store it on the disk.
It will eliminate all these problems for you.
As with the other answers, the best solution is to separate the image from the html source code.
That being said, if you absolutely require the image to be part of the page, and are not using internet explorer, it is possible to embed the image in the page.
it should be as simple (an example being a png) as:
<img src="data:<?php echo $mimeType ?>;base64,<?php echo chunk_split(base64_encode($imageData)); ?>" />
This only works in non IE browsers (I'm not sure about IE8, as I don't have it around to test at the moment).
Just make sure you get the mime type right (ex, image/png for png).
A little bit more info:
http://www.greywyvern.com/code/php/binary2base64
We are working on a website that has tons of images and one of the requirement is to have ALT text for each image (JPG, JPEG, PNG, GIF etc). So whenever the HTML shows and image the ALT text for that image is also written to HTML along with IMG SRC.
After our discussion with developers the solutions that turned up are
1) Create a new table to hold all these image names. Then each time the image is used a db lookup is done and appropriate ALT text is added.
2) Another solution is to write some EXIF info into each file and each time they are used the EXIF info of the file is read and added to HTML as required.
Any suggestions on how to proceed. There are a lot of images so we are looking for the best solution with least load on server and loading time.
Additional info - We run PHP on our server to select images etc.
Thanks
I'ld rule out EXIF as it does not support PNG and GIF.
The db lookup sounds okay (to me) and would scale okay (as long as you did it cleverly). For example you should try to reduce lookups as much as possible.
You might even already have some of this data, and it would be useful to have data about the images anyways
I would recommend storing it in the database because I am sure you have to maintain records of these images, adding another column to a table is little work. Also, if its inside the database you can perform searches on the alt text in case you want to have such a feature.
If you wan't to give the images an alt text, it should be something that works correctly if the image is not there.
I shouldn't be "image's alt text", or "image.jpg". Rather it should be something like "Stackoverflow.com has a lot of questions and answers." when showing a SO screenshot. But if your image can't have a meaningful alt text, then just make alt="", and move on, sometimes it's simply better to give no alt text than giving a bad alt text.
Because of this, you should store the alt text for every image that means something, and not put meaningless alt text (ruling out EXIF information).
If you can live with the alt tags matching the name of the file, you can use some javascript to get all the images and add an alt tag based on the name of the file.
Something like this:
//get all the img tags
var images = document.getElementsByTagName('img');
for (i=0;i<images.length;i++)
{
//get the filename from the src
filename = images[i].src.substring(images[i].src.lastIndexOf('/')+1,images[i].src.lastIndexOf('.'));
//do any formatting here
filename = filename.replace('_',' ');
//set alt/title tags
images[i].setAttribute('alt', filename);
images[i].setAttribute('title', filename);
}
I would avoid option 2 because if you wanted to update the alt text you would need to write to the image file each time, also, if you wanted to process the images eg. generate thumbnails, the meta-data might be lost.
Option 1 seems the most logical, and if you're querying for filenames from a DB, then just get the alt text at the same time.
Maybe third option:
store ALT text in filename :)
While both solutions seem fine to me the question about required load is a bit tricky: if you get the images from a database anyway I'd store the alt tag values in the database as well. If the files are served from a directory it depends on if you are using a database at all. If you don't use a database going the EXIF route sounds like a reasonable alternative, but might create more load than using an additional database as you need to open each file to retrieve the EXIF data.
In short, go the database route if you already use a database.
I would suggest the database option, because the EXIF option will become a problem if somehow an image has to be switched with another image (transferring EXIF data is not trivial), plus parsing a lot of image files on each request would be very resource consuming.
But be it with the database or the EXIF option, I would strongly suggest that you generate a php file acting as a cache, with an associative array of image_name => alt_text, because you don't want 30 sql queries on each page load. The php file would be included as a bootstrap, so every script would have acces to the associative array, via a global variable for example.
And you would have a script that generates this file, so whenever an alt text is changed, you can easily regenerate the cache file containing the associative array.
Honest question: do you really even need to create a new table? If you're using a PHP upload mechanism anyway, couldn't you simply add a new field to the database and add the alt text, then link it as a variable as needed? The PHP would be very simple from there. Just a thought!
If you already have a database (which I presume you do), then don't add a whole new table, just add a column to the existing table where your images are. Then you can just get all the information with the same query you populate your page with. I believe this is the best option.
When you say "a lot of images", how many are we talking about? Hundreds, or thousands?
If you're in the hundreds, I would just create a PHP file with an array of the imagePath/altText pair. Then include this PHP file wherever you are referencing the image. To abstract the implementation, have a method in the PHP file to return the altText given the complete image path.
$texthash = array(
"/some/path/imageName.png" => "some alt text"
, "/some/path/imageName2.png" => "some other alt text"
);
function get_alt_text($imgpath) {
return $texthash($imgpath);
}
This strategy is fast and will not slow down your pages as long as the number of images is still relatively small. The only tricky part is making sure you keep the array sorted by image path as new images are added.
Once the number of images gets large enough that this method is slowing down performance, move the information to the database, and change the method in the PHP file to query the database. Since you've abstracted the implementation, you won't need to change any of the referencing PHP files.
Also make sure you are HTML-escaping the alt text before using it in the HTML.