How to convert php image to base64

藏色散人
Release: 2023-03-13 18:50:01
Original
2647 people have browsed it

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;
}    
?>
Copy after login

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!

Related labels:
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