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

How to convert images into binary in php?

青灯夜游
青灯夜游Original
2020-08-17 16:01:514495browse

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.

How to convert images into binary in php?

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)

    How to convert images into binary in php?

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!

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