So I have a webcam widget to take snapshots from guests. My guest table have an image field and I want to store this pic on it. I tried storing raw binary data, base64 encoded data and it didn't works. I don't know if database is receiving correct data or if I don't dealing with returned data well. I'm using PDO in my app and I've read on the web that it can have an issue with data larger than 64 kb, anyone knows if it's true?
This is how I got the image data from the webcam:
$imagem = file_get_contents('php://input');
Like I said above I tried to store image's pure data $imagem and then this way:
$data = #unpack("H*hex", $imagem);
$imagem_bd='0x'.$data['hex'];
And for both of that I've tried encoding with base64 too, but none worked.
Just for information: This app I'm working is a remake of another similar app that have this feature too, but on this old app I's used TSQL and it works fine.
Thanks in adv.
EDIT
For now I have done the insert well, but I can't retrieve and render the image at the browser.
you can encode images in blob instead of base64. But i recommend you to store image as file and store the link to the image in database. As storing images in database may consume large space and also returning it can eat your server and also it may take more time to load compared to file system.
If you use PreparedStatement, all you need to do is use bindValue, with your binary image:
$sql = "INSERT INTO your_table (image) VALUES (?)";
$stmt->bindValue(1, $imagem);
$stmt->execute();
Anyway, as said here, it's not a good idea to store images in the database.
About the 64KB limit, it depends of the data types of your columns in the table (in MySQL, the BLOB type has a limit of 64KB, but there is another types of data, like MEDIUMBLOB and LONGBLOB).
Related
I am new to Android development. I am just trying to store an image in MySQL database and want to display that image in an ImageView.
What should I do?
I think I have to store the URL of image in MySQL database but I don't know how to do it either.
I am using PHP too.
One method is to store the base64 version of image using a php script with
base64_encode() wich returns the base64 encryption of the image but i don't recommend it because this is increasing the size of the image.
You should upload the image to a server and only store in the database the path to the image and then load it inside the Android application.
Here is a nice Java tutorial that explains how to upload an image through HTTP http://www.jguru.com/faq/view.jsp?EID=62798 it should work for your application.
Try to avoid if you can, storing images in database (as it would increase the size)..
If you cannot avoid, try using BLOB in database.. as mentioned here.
NOTE: Also do some research before asking questions.
What should I use to save an image in the database. In what type I have to use to store and retrieve the image in PHP and MySQL.
Rule of thumb is you shouldn't be storing images in the database. Blob is available but is pretty crappy.
What you should be doing is storing the binary file on the filesystem which is many times faster than a database and in the database just store a path or link or file name and have the application load the image from the path instead.
This allows you to easily implement stuff like cdn's or san storage etc for static files. It even allows you to use something like lighttpd to display the static content images rather than apache.
https://blogs.oracle.com/manveen/entry/blob_vs_file_system_storage gives a little more information but there's plenty of stats and data on the web about the disadvantages of blobs
use BLOB Datatype which means Binary Large Object
BLOB
BLOB's may be slower than file storage, but it is much easier to copy, backup, and restore a single database file than it is to manage/maintain images in a file structure.
I also recommend storing the images in their own dedicated database (store the properties/tag information in a separate database).
Simple & optimized option is to upload the image on the server and store the string of "url of the image" in database.
Incase, you want to store actual image file only, then use Blob datatype.
Its a very bad idea to store images in database. Database is determined to save a little pieces of information - not images or files. You have HDD on that purpose.
Instead, you should save image on disk and save its filename in database. Like that:
$name = uniqid();
file_put_contents($name.".png",$image_data);
mysql_query("INSERT INTO `images` (image) VALUES ('$name.png')") or die(mysql_error());
If you really insist on saving images in DB use BLOB.
Just a quick question I am wanting to allow users to link there facebook account to my new product, and I am wondering how do I take there facebook photo URL and save it as an image in BLOB formate for MYSQL
I am using this example to connect to Facebook
https://github.com/facebook/connect-js/blob/master/examples/jquery/login.html
and to send the image to my server I use the $.ajax formate to submit it to the core.php (I have not coded this yet, as I need to know the best way.
Well my first recommandation is to NOT store image data in your database unless you absolutely need that in your database backups. You will clutter your database massively. Instead, just get the url to the picture using the server-side $facebook class, and just "file_get_contents" it to your php memory.
Then dump it on disk and save a reference of that dumped image in your database. It will do much more good to your database this way.
Else, if you really want to save it to a BLOB, use the same method to fetch the image to memory and then use the hex() function to transform it to a textual representation that can be fit into an INSERT/UPDATE query...
Just save a reference to the location where Facebook stores the image. That way you can abuse their servers (which are very nice/fast) and just serve image tags that refer to their servers.
Also when you save images to a database you lose the native compression that you get when you save an image to the filesystem, so you bloat your data storage size for not much reason (backups are easier I suppose).
I'm trying to figure out a way to store files in a database. I know it's recommended to store files on the file system rather than the database, but the job I'm working on would highly prefer using the database to store these images (files).
There are also some constraints. I'm not an admin user, and I have to make stored procedures to execute all the commands. This hasn't been of much difficulty so far, but I cannot for the life of me establish a way to store a file (image) in the database.
When I try to use the BULK command, I get an error saying "You do not have permission to use the bulk load statement." The bulk utility seemed like the easy way to upload files to the database, but without permissions I have to figure a work-a-round.
I decided to use an HTML form with a file upload input type and handle it with PHP. The PHP calls the stored procedure and passes in the contents of the file. The problem is that now it's saying that the max length of a parameter can only be 128 characters.
Now I'm completely stuck. I don't have permissions to use the bulk command and it appears that the max length of a parameter that I can pass to the SP is 128 characters.
I expected to run into problems because binary characters and ascii characters don't mix well together, but I'm at a dead end...
Thanks
In general, we don't pass binary data in SQL. We upload the file to the server, then load the image from the server into the database.
Load the image into the database from a file:
UPDATE images
SET image = LOAD_FILE('images/myimage.jpg')
WHERE image_id = 1234
Get the image back out to a file:
SELECT image
INTO DUMPFILE 'images/myimage.jpg'
FROM images
WHERE image_id = 1234
Here is an example I've found in David Hayden's blog.
It's a c# example, but the steps should be similar in PHP:
Convert your uploaded file to a byte array
Execute dynamic TSQL on the server
I created an .png image from a video using ffmpeg tool and i want to how to insert that image into the db which has a blob field and also i want to know how can i recollect that image from the db to display as a image again?
use file_get_contents($fileDir); and then insert into a text column type in your database ensuring that you base64_encode your file_get_contents string before inserting.
It's called BLOB inserting.
In general it is not recommende to store images in the db. It is usually better to store them in the fs and only keep the meta data in the db.
It's generally a better idea to only store the filename or path to the image in the database rather than storing it as a blob. You'll notice a big performance hit when you have a large volume of images.
Aside from that, it's a lot easier and more secure to use php's built-in file and image functions on actual files, rather than having to process the blob beforehand.