Home >Backend Development >PHP Tutorial >PHP source code converts images into data/base64 data flow example detailed explanation_php example

PHP source code converts images into data/base64 data flow example detailed explanation_php example

WBOY
WBOYOriginal
2016-12-05 13:28:251260browse

php source code to convert images into data/base64 data stream

Here we share a method to convert images to base64 encoding format:

<&#63;php
$img = 'test.jpg';
$base64_img = base64EncodeImage($img);
 
echo '<img src="' . $base64_img . '" />';
/* 作者:http://www.manongjc.com */
function base64EncodeImage ($image_file) {
  $base64_image = '';
  $image_info = getimagesize($image_file);
  $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
  $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
  return $base64_image;
}
&#63;>

The base64 encoded string converted by the above method can be stored in the database and can be read directly from the database when needed, reducing the number of requests when accessing images.

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

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