Home >Backend Development >PHP Problem >How to convert images to binary in php
php method to convert images into binary: first obtain the size of the specified image file through the filesize function; then use the fread and fopen functions to read the file; finally execute the corresponding file to output the image in the form of a binary stream to the client.
php converts image files into binary output
Method:
header( "Content-type: image/jpeg"); $PSize = filesize('1.jpg'); $picturedata = fread(fopen('1.jpg', "r"), $PSize); echo $picturedata;
It’s that simple With 4 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.
For more related knowledge, please visit PHP Chinese website!
The above is the detailed content of How to convert images to binary in php. For more information, please follow other related articles on the PHP Chinese website!