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

WBOY
Release: 2023-03-03 07:14:02
Original
1257 people have browsed it

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

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!

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