Home >Backend Development >PHP Problem >How to convert php images into binary streams
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.
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='$strData'>
function base64EncodeImage($strTmpName) { $base64Image = ''; $imageInfo = getimagesize($strTmpName); $imageData = fread(fopen($strTmpName , 'r'), filesize($strTmpName)); $base64Image = 'data:' . $imageInfo['mime'] . ';base64,' . 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!