PHP - Store image to database or file better? - php

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.

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)

Store an image in MySQL database and display it in ImageView in Android?

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 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.

How to store thumbnail image into the database?

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.

Categories