What should I use to save an image in database - php

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.

Related

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)

In MySQL is there a way or command to say point to a file where the images are stored?

I am building a database for a company. They want a MySQL database and they want to store images for over 11,000 products. I just wanted to know if there is a way or command to say point to a file where the images are stored.
Just save at as a VARCHAR, and that value represents a path to that file. Then just do:
$q=mysql_query("select path from images where id=123");
if($r=mysql_fetch_array($q)){
echo '<img src="'.$r['path'].'">';
}
Regarding the other option, you can store images as BLOBs in mysql, but then you have to go through mysql to get it. And you'll have write a script that will send an image to the browser. I think it's much better to keep them on the filesystem, and just use mysql to point to them. Then when an tag is sent to the browser, your webserver will handle the serving of that image correctly for you. Also, if you have shell/ftp access to your server, it will make managing/viewing your images a lot easier and simple.
You can store the files in table columns with the blob datatype.
Either that, or store the server file path in a string(varchar) field, and retrieve files using server code, after reading from the database.

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

PHP & MYSQL - what should I do with my image data?

I'm creating a blog with a featured image on each post. I have a dilemma, I'm unsure what to do with my image data...
Should I insert image data into my MYSQL database using BLOB?
Or should I just create an uploader which makes a directory into the users images folder and upload the photo that way...then just reference it directly in the image field when adding a Blog Post?
Is there a standardised way?
Kind regards,
adam
Upload the files to your server and save the location of the file in your database. Less strain on your DB and your HTTP daemon is better at serving images than MySQL.
The general approach is not to store files in DB, unless you understand why do you need it to be stored there. So, since you are not sure, it's much simplier storing them in upload folder.
But, just in case you decide you need storing files (no matter images or some other) in DB, you have to declare BLOB field and then save it using some BLOB-supporting DB mechanism. 'PHP's MySQLi extension: Storing and retrieving blobs' is a good example of how it can be made
You should store images in folder. Click on below link from where you can get idea how to crop different-different size images and store images name in to database table:
How can I upload images in a normal insert form (MySql)? after upload the image should have three versions of different sizes and different names
convert the image data to base64. This can be done within PHP:
<?
$image=file_get_contents("image.png");
$image=base64_encode($image);
?>
Storing images in a DB is a good idea for secure images.
Always store images, music files etc in system files on disk, and then store the urls to them in the database. That will make it
1) faster
2) easier to configure security settings
3) better in any ways I can imagine
Disadvantage
If file system is corrupted you will have hard time recovering.
You can also use third party Image hosting sites too, you can use Amazon S3 or Mosso Cloud Files.
Problem with file system is it is difficult to scale.
Facebook uses cassandra to store images.
Since it is blog you can store images in filesystem.
Both are valid approaches.
They have different advantages/disadvantages.
Storing it in the database means you need to add extra code to change the image to a representation which will fit inside a INSERT/UPDATE statement (base64 is one approach, and requires equivalent decode, but you could just use mysql_real_escape_string()). Although you can't query the image directly (other than finding exact matches) it may reduce the number of seek and I/O operations required to retrieve the data compared with looking up the path in the database then retrieving the file.
It's also a lot simpler to set up replication of a database compared with setting up replication of the database AND the filesystem if you run on multiple nodes. And there's the issue og keeping filesystem and database backups synchronized.
OTOH, using a filesystem makes your data tables much smaller, and therefore faster to retrieve records from.
which makes a directory into the users images folder
You certainly don't want to allow users to upload content directly into your webserver's document tree - regardless of which route you take, the data should be stored in a location not directly accessible by the webserver but accessible by your code.

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