How to convert images into binary data in php

藏色散人
Release: 2023-03-02 12:54:01
Original
4287 people have browsed it

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));//二进制数据
Copy after login

Method two:

file_get_contents($_FILES['file']['tmp_name']);
Copy after login

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;
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template