PHP will turn base64 format picture function

  What good is base64? In discussing this issue today with the junior partner, if the whole picture into the station with Php base64 okay?

 

  1. improve performance: each picture on the page, all need to consume a http request to download from the download pictures always have to make a request to the server, if the pictures do not download request to the server, base64 can with HTML Download downloaded to the local reducing https request.

2. Encryption: allows users to not seeing a picture content, you can only see the code.
3. ease of reference: When multiple files at the same time use some pictures, you can put the picture into base64 file format, the style on the global, such as common.css, when used in the future can be directly added to the class name, two multi-layer does not need to find a file path, will increase efficiency

  

  

/**
	* 图片转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;
        }
    }

  Picture practical application base64

  

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

  

    But base64 has its drawbacks:

    Text 1) base64 form large, increases the pressure stored in the database, the database server;

   (2) Although the page load pictures without accessing the server, but because of too much content base64 format, the page load speed will be reduced, it may affect the user experience. 

  (3) base64 can not cache to cache file cache contains only base64, such as js or css, these cached images than direct a lot worse, and generally HTML changes more frequently, so the cache is equivalent to the lack of effectiveness.

  Summary: Because of the disadvantages of using base64, so the general picture is less than 10kb, we will choose to use base64 images, such as some emoticons, images converted to base64 much more harm than good. Of course, extreme cases of extreme consideration.

Guess you like

Origin www.cnblogs.com/68xi/p/11612500.html