How to store Image along with data in MySQL database?

조회 수: 8 (최근 30일)
Dipojjal Chakrabarti
Dipojjal Chakrabarti 2015년 4월 23일
답변: Piyush Kumar 2024년 10월 25일 7:43
I am creating a face recognition system which will recognize faces and return information after recognizing that user like: his name, email address, user ID, password, etc.
I was planning to use MySQL because it will be impemented on the web later.
How do I do so?

답변 (2개)

Stalin Samuel
Stalin Samuel 2015년 4월 23일

Piyush Kumar
Piyush Kumar 2024년 10월 25일 7:43
Hi,
To store an image along with other data in a MySQL database, follow these steps:
1. Create the Table
CREATE TABLE images (
id INT AUTO_INCREMENT PRIMARY KEY,
image_name VARCHAR(255),
image_data LONGBLOB
);
2. Insert the image: Use the LOAD_FILE function to insert an image into the database. Ensure that the secure_file_priv setting in MySQL allows access to the directory where the image is stored:
INSERT INTO images (image_name, image_data)
VALUES ('100.png', LOAD_FILE('C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\100.png'));
You will see "BLOB" entry in the database in "image_data" column.
3. Extract the image: To retrieve and save the image from the database, you can use the following command. Make sure the output path is permitted by the secure_file_priv setting:
mysql -u root -p -e "SELECT image_data FROM test.images WHERE image_name = '201.png' INTO DUMPFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/extracted.img';"
4. Convert the Extracted file to PNG using powerShell: Convert the binary data back into a PNG file using PowerShell:
$inputFile = "C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\extracted.img"
$outputFile = "C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\extracted.png"
[System.IO.File]::WriteAllBytes($outputFile, [System.IO.File]::ReadAllBytes($inputFile))
5. Important Note: Ensure that the file paths used for storing and retrieving images are within the directories allowed by the secure_file_priv setting in MySQL. This setting restricts file operations to specified directories for security reasons.
This process ensures that the image data is correctly stored and retrieved from the database, and the PowerShell script helps convert the binary data back into a usable image file.

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by