Home  >  Article  >  Backend Development  >  How to convert images into binary data in php

How to convert images into binary data in php

藏色散人
藏色散人Original
2020-07-13 09:28:554220browse

How to convert php pictures into binary: first get the picture that needs to be converted; then use the fopen function to read the picture information; then use the "fread($fp, filesize($img));" method to convert the picture into binary data.

How to convert images into binary data in php

#What was recorded this time is very simple, which is to convert the picture into binary data and save it in the database, and to take out the data and output the picture for display.

Convert image to binary

Method one:

$img = '111111.jpg';
$fp= fopen($img, 'rb');
$content = fread($fp, filesize($img));//二进制数据

Method two:

file_get_contents($_FILES['file']['tmp_name']);

Convert binary to image

Taking method 1 as an example, you only need to add a header to output the image to the browser.

header( "Content-type: image/jpeg");
$fp      = fopen($img, 'rb');
$content = fread($fp, filesize($img)); //二进制数据
echo $content;

For the second method, you need to use base64 to convert it.

For more related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to convert images into binary data in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn