Here are two practical ways to implement those optimizations for your HTML and PHP projects.
1. Optimized .htaccess Configuration
Adding these rules to your .htaccess file will handle Brotli/Gzip compression and Browser Caching automatically. This significantly improves your LCP (Loading speed) by reducing file sizes and server requests.
# ———————————————————————-
# 1. Enable Gzip Compression (Brotli is usually handled by the host)
# ———————————————————————-
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript application/json
</IfModule>

# ———————————————————————-
# 2. Set Browser Caching (Expires Headers)
# ———————————————————————-
<IfModule mod_expires.c>
  ExpiresActive On
  # Images (1 Year)
  ExpiresByType image/jpg “access plus 1 year”
  ExpiresByType image/jpeg “access plus 1 year”
  ExpiresByType image/gif “access plus 1 year”
  ExpiresByType image/png “access plus 1 year”
  ExpiresByType image/webp “access plus 1 year”
  # CSS and JS (1 Month)
  ExpiresByType text/css “access plus 1 month”
  ExpiresByType application/javascript “access plus 1 month”
</IfModule>

# ———————————————————————-
# 3. Prevent Render-Blocking (Basic setup)
# ———————————————————————-
Header set Connection keep-alive

2. PHP Script: Automatic WebP Conversion
Since images are usually the largest part of an informational page, converting them to .webp is a huge win. This PHP function takes a .jpg or .png and creates a compressed .webp version if it doesn’t already exist.
<?php
function convertToWebp($source, $quality = 80) {
    // Determine the directory and filename
    $dir = dirname($source);
    $name = pathinfo($source, PATHINFO_FILENAME);
    $destination = $dir . DIRECTORY_SEPARATOR . $name . ‘.webp’;

    // If webp version already exists, don’t re-convert
    if (file_exists($destination)) {
        return $destination;
    }

    // Get image info
    $info = getimagesize($source);
    $mime = $info[‘mime’];

    // Create image resource based on type
    if ($mime == ‘image/jpeg’) {
        $image = imagecreatefromjpeg($source);
    } elseif ($mime == ‘image/png’) {
        $image = imagecreatefrompng($source);
        // Maintain transparency
        imagepalettetotruecolor($image);
        imagealphablending($image, true);
        imagesavealpha($image, true);
    } else {
        return $source; // Return original if not supported
    }

    // Save the webp file
    imagewebp($image, $destination, $quality);
    imagedestroy($image);

    return $destination;
}

// Example usage:
// $webpImage = convertToWebp(‘assets/images/hero-banner.jpg’);
// echo ‘<img src=”‘ . $webpImage . ‘” alt=”Optimized Image” loading=”lazy”>’;
?>

Why this helps your SEO:
* Lower TTFB: Compressed files travel faster from the server.
* Reduced Bandwidth: Smaller images mean faster mobile load times (Google’s primary index).
* Better User Experience: Caching ensures that returning visitors see your page instantly, reducing bounce rates.
Would you like me to show you how to set up an automated image sitemap in PHP to ensure Google indexes all these new WebP images correctly?


Discover more from NathanLegakis.com

Subscribe to get the latest posts sent to your email.

Leave a Reply

Discover more from NathanLegakis.com

Subscribe now to keep reading and get access to the full archive.

Continue reading