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

How to convert php images into binary streams

藏色散人
藏色散人Original
2021-03-03 09:38:082787browse

How to convert php images into binary streams: first create a PHP sample file; then get the temporary file name; finally use the "base64EncodeImage(strTmpName);" method to convert the image files into binary streams.

How to convert php images into binary streams

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer.

php converts images into binary streams

//获取临时文件名
$strTmpName = $_FILES['file']['tmp_name'];
//转成二进制流
$strData = base64EncodeImage(strTmpName );
//输出
<img src=&#39;$strData&#39;>
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;
}

[Recommended: PHP video tutorial]

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