Search code examples
phpimagehttprequestpagespeed

Use php to load images and minimize HTTP requests


Goal: minimizing the loading time of my website

I did lots of scans and speed tests.

They all indicate, there is lots of time spent uselessly, waiting for a HTTP response. The time spent actually sending data is very small compared to that.

Screenshot of a 'speed test':

enter image description here

I started loading all the scripts and styles in PHP and just echoing them:

<!-- loading a style -->
<style><?php echo file_get_contents("style.css"); ?></style>

<!-- loading a script (similar) -->
<script type="text/javascript"><?php echo file_get_contents("script.js"); ?></script>

That does work, however, the images still slow things down a little. How do I use php to read them and echo them as a <img> tag?

(The mentioned website is https://www.finnmglas.com/)


Solution

  • Everyone seeking answers to this:

    You should not echo back images with PHP into the main file:

    • It messes up the loading time (makes it way longer)
    • And it delays the first render of your website. Even if you have less requests, you will want to have a quick first render (users will notice the delay)

    Echoing images from a separate PHP file and including a <img> tag pointing at that PHP should not be a problem ^^