Home >php教程 >php手册 >PHP source code converts images into data/base64 data stream examples detailed explanation

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

高洛峰
高洛峰Original
2016-12-27 09:45:251619browse

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;
}
?>

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!

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

Related articles

See more