How to convert images into binary strings in php

藏色散人
Release: 2023-03-05 07:46:01
Original
2971 people have browsed it

php method to convert images into binary strings: first obtain the temporary file name through the "$_FILES['file']['tmp_name'];" method; then convert the image file into binary through the base64EncodeImage function Stream; finally output the conversion result.

How to convert images into binary strings in php

Recommended: "PHP Video Tutorial"

php converts images into binary streams

//Get the temporary file name

$strTmpName = $_FILES['file']['tmp_name'];
Copy after login

//Convert to binary stream

$strData = base64EncodeImage(strTmpName );
Copy after login

//Output

<img src=&#39;$strData&#39;>
Copy after login
function base64EncodeImage($strTmpName)
{
    $base64Image = &#39;&#39;;
    $imageInfo   = getimagesize($strTmpName);
    $imageData   = fread(fopen($strTmpName , &#39;r&#39;), filesize($strTmpName));
    $base64Image = &#39;data:&#39; . $imageInfo[&#39;mime&#39;] . &#39;;base64,&#39; . chunk_split(base64_encode($imageData));
    return $base64Image;
}
Copy after login

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