: This article describes how to use base64 to encode and decode images. For more information about PHP tutorials, see. I tried to transmit the image through json. at this time, I thought of using base64 encoding for the image. The main steps are as follows:
1. encode the image file, convert it to base64 encoding format, and a long string of characters;
2. the characters can be transmitted in json format;
3. the target party receives the json Array, extracts the encoding string, decodes the string, and displays the image.
The difficulty of this method mainly lies in the encoding and decoding of images. The following is the coding and decoding code implemented by PHP.
?>
Where,
Preg_match ()
Purpose: execute a regular expression match.
Return value: returnpattern
. Its value will be 0 (not matched) or 1 time, because preg_match () will stop searching after the first match. If an error occurs, preg_match () is returned.FALSE
.
File_put_contents ()
Purpose: write a string to a file.
Returned value: the number of bytes written to the file after the function is successful. if the function fails FALSE。
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above section describes how to use base64 to encode and decode images, including some content, and hope to help those who are interested in PHP tutorials.