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