php method to convert pictures into binary: first get the picture that needs to be converted; then use filesize() to get the size of the picture file, use fopen() to open the picture file; finally use fread() to read the picture file , convert the image into binary data.
Recommended: "PHP Video Tutorial"
php converts images into binary
header( "Content-type: image/jpeg"); $img = '1.jpg'; $PSize = filesize($img ); $fp= fopen($img, 'rb'); $picturedata = fread($fp, $PSize);//二进制数据 echo $picturedata;
With just a few 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.
Description:
filesize() function returns the size of the specified file. If successful, the function returns the file size in bytes. On failure, returns FALSE.
fopen() function opens a file or URL. If the opening fails, this function returns FALSE.
fread() function reads a file (safe for binary files).
Syntax: fread(file,length)
The above is the detailed content of How to convert images into binary in php?. For more information, please follow other related articles on the PHP Chinese website!