How to convert images to binary in php

藏色散人
Release: 2023-03-01 21:36:01
Original
3883 people have browsed it

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

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!

Related labels:
php
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