Home  >  Article  >  Backend Development  >  How to convert images to binary in php

How to convert images to binary in php

藏色散人
藏色散人Original
2020-07-04 09:12:243798browse

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.

How to convert images to binary in php

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!

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