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

How to convert images into binary output in php

王林
王林Original
2020-07-10 11:57:193476browse

How PHP converts images into binary output: This can be achieved through the fopen() function in combination with the fread() function. The fopen() function can open a file or URL. The fread() function is used to read an open file and returns the read string.

How to convert images into binary output in php

Related function introduction:

(Recommended tutorial: php tutorial)

1. fopen function

fopen() function opens a file or URL.

If fopen() fails, it will return FALSE with an error message. You can hide error output by adding an '@' in front of the function name.

Syntax

fopen(filename,mode,include_path,context)

Parameter introduction:

  • ##filename Required. Specifies the file or URL to open.​

  • #mode Required. Specifies the type of access you are requesting to this file/stream.

2. fread function

fread() function reads the open file.

The function will stop running when it reaches the specified length or reaches the end of file (EOF) (whichever comes first). This function returns the string read, or FALSE on failure.

Syntax

string fread ( resource $handle , int $length )

Parameters

  • handle The file system pointer is a resource typically created by fopen().​

  • #length Required. Specifies the maximum number of bytes to read.

Code implementation:

header( "Content-type: image/jpeg");
$PSize = filesize('1.jpg');
$picturedata = fread(fopen('1.jpg', "r"), $PSize);
echo $picturedata;

The above is the detailed content of How to convert images into binary output 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