Home > Article > Backend Development > How to convert image to binary string in php?
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.
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!