Home > Backend Development > PHP Tutorial > Advantages and disadvantages of converting images to base64 format with PHP

Advantages and disadvantages of converting images to base64 format with PHP

不言
Release: 2023-03-25 12:20:01
Original
1957 people have browsed it

This article mainly introduces the advantages and disadvantages of converting images into base64 format with PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it.

1. Advantages and Disadvantages of Converting Pictures to Base64 Format

1. Advantages

(1) Pictures in base64 format are in text format and occupy small memory. The size ratio after conversion is about 1/ 3. Reduce resource server consumption;

(2) When using base64 format images in web pages, there is no need to request the server to call image resources, reducing the number of server visits.

2. Disadvantages

(1) There is a lot of text content in base64 format, which is stored in the database and increases the pressure on the database server;

(2) Web page loads images Although there is no need to access the server, because there is too much content in base64 format, the speed of loading web pages will be reduced, which may affect the user experience.

(3) Base64 cannot be cached. To cache, you can only cache files containing base64, such as js or css. This is much worse than directly caching images, and generally HTML changes frequently, so it is equivalent to not getting Caching benefits.

2. PHP converts images to base64 format function

	/**
	* 图片转base64
	* @param ImageFile String 图片路径
	* @return 转为base64的图片
	*/
    function Base64EncodeImage($ImageFile) {
        if(file_exists($ImageFile) || is_file($ImageFile)){
            $base64_image = '';
            $image_info = getimagesize($ImageFile);
            $image_data = fread(fopen($ImageFile, 'r'), filesize($ImageFile));
            $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
            return $base64_image;
        }
        else{
            return false;
        }
    }
Copy after login

3. Practical application of base64 images

<img src="data:image/jpeg;base64,/9j/4AAQS……"/>
Copy after login

Because of base64 Disadvantages of use, so generally when the picture is less than 10kb, we will choose to use base64 pictures, such as some emoticon pictures. Converting pictures that are too large to base64 is not worth the gain. Of course, extreme cases are considered.

Related recommendations:

Detailed explanation of examples of php storing pictures in mysql

How to use PHP to convert pictures into base64 encoding

The above is the detailed content of Advantages and disadvantages of converting images to base64 format with PHP. For more information, please follow other related articles on the PHP Chinese website!

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