Search code examples
phpbase64file-get-contents

problem with base64_encode which slower my site php


I have use image src encryption with base64_encode but that code slower my site but when I put this code it makes my site slower. so do anybody has any solution to make my site faster with this type of encryption. I have put my code below.

<?php
   while ($user = mysqli_fetch_array($queryResult, MYSQLI_ASSOC)){
      if ($user["main_picture"]){
       $imageData = base64_encode(file_get_contents($user["main_picture"]));
       $result .= '<td><div class="user_image_container"><img src="data:image/jpeg;base64,'.$imageData.'"></img></div></td>';
      }
      else{
          $result .= '<td></td>';
      }
?>

can anybody help me in this.


Solution

  • When you load your page with this code the PHP interpreter has to finish fetching the image over the network before it can interpret the rest of the page, which adds time.

    If you were to load the page without this code and use a direct link to the image, the page itself would load a lot faster and then the browser would load the image.

    Potential workaround: use a database to store single-use tokens that map to to images. When a user loads the page, generate a token (which will be quicker than pulling the image) and have the image src point to an image-serving endpoint that you set up that checks the token, marks it as used, fetches the file and then sends the image. You might run into problems with caching if you want it to be truly single use, but it does at least hide the source of the image.