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

How to convert images into binary output in php

王林
Release: 2023-03-02 09:52:01
Original
3517 people have browsed it

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)
Copy after login

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 )
Copy after login

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;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template