How to convert php image to base64: 1. Get an image address; 2. Convert the image to base64 string format through the "chunk_split(base64_encode($gambar));" method.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to convert php image to base64?
PHP image to base64
The following code uses PHP to convert the image to base64 string format:
Example
<?php //$file:图片地址 //Filetype: JPEG,PNG,GIF $file = "encode.jpg"; if($fp = fopen($file,"rb", 0)) { $gambar = fread($fp,filesize($file)); fclose($fp); $base64 = chunk_split(base64_encode($gambar)); // 输出 $encode = '<img src="data:image/jpg/png/gif;base64,' . $base64 .'" >'; echo $encode; } ?>
Recommended learning: " PHP video tutorial》
The above is the detailed content of How to convert php image to base64. For more information, please follow other related articles on the PHP Chinese website!