How to store thumbnail image into the database? - php

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.

Related

Storing image's as blob in mysql database with flutter

Is it good to store blob images in mysql database and fetch them in the flutter app using php or should I store them in a separate folder with their links in database? For a database with above 100K images what would be a better solution. Storing image paths would be difficult while backup i think. Can someone suggest what to do. It will be very helpful. Thank you in advance!
The best practice is to store the images in a folder and just save their path's in the database. So basically convert the images to base64 and send it to your webserver in a regular POST method. then in your PHP code decode the base64 to a file and save the image wherever you want, just remember to keep track of the image new path so can fetch it later. fetching, by the way, works the same. good luck :)

Column Types for 64 Encoded Image via Laravel Migration

$table->text('company_logo')->nullable();
I'm trying to create a migration to store Base 64 Encoded image in my My SQL database. I'm not if what I have is good enough.
Is what I have good enough ? How do I know it ?
Storing any binary data (including images) in DB is a bad practice. You should save uploaded images as files and store links to them in DB.
its a bad idea to store images in database. it would be better if you save only path to you image in db. when you fetch the post data, you can just decode your base64 image, move this image to your images directory and save only the path to this image in database(for example /img/mypic.jpg)

PHP - Store image to database or file better?

PHP - Store image to database or file better?
The image requires to change to base64 and send to the client everytime.
So, is it good to convert the image to base64 storing to database, so as to reduce the throughput to convert it everytime when loading the image
I would avoid this, your db will fill up quick if you have lots of images. I would store to file.

What should I use to save an image in database

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.

save facebook photo to mysql 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).

Categories