Home > php教程 > php手册 > body text

PHP source code converts images into data/base64 data stream examples detailed explanation

高洛峰
Release: 2016-12-27 09:45:25
Original
1587 people have browsed it

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

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

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

obtained after conversion through the above method Base64 encoded strings can be stored in the database and can be read directly from the database when needed, reducing the number of requests when accessing images.

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

For more PHP source code to convert images into data/base64 data stream examples, please pay attention to the PHP Chinese website for related articles!

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template