Home >Backend Development >PHP Problem >How to convert php image to base64

How to convert php image to base64

藏色散人
藏色散人Original
2021-11-09 09:51:192688browse

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.

How to convert php image to base64

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 = &#39;<img src="data:image/jpg/png/gif;base64,&#39; . $base64 .&#39;" >&#39;;
    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!

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