Home  >  Article  >  Backend Development  >  How to convert image to binary string in php?

How to convert image to binary string in php?

青灯夜游
青灯夜游Original
2020-10-07 10:01:582276browse

php method to convert an image into a binary string: first use filesize() to get the size of the image file and assign it to the variable $PSize; then use fopen() to open the image file and assign it to the variable $fp; finally use "fread($fp,$PSize)" reads the file and converts it into binary string data.

How to convert image to binary string in php?

php method to convert image files into binary output

header( "Content-type: image/jpeg");
$PSize = filesize('1.jpg');
$fp =fopen('1.jpg', "rb");
$picturedata = fread($fp, $PSize);//二进制数据
echo $picturedata;

It’s that simple With 5 lines of code, the image is output to the client in the form of a binary stream, which is no different from opening a picture.

It should be noted here that the header sent depends on the specific situation and may not always be image/jpeg. JPG is image/jpeg, but PNG is image/png. Different types of pictures output different headers.

Recommended: "PHP Video Tutorial"

The above is the detailed content of How to convert image to binary string 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