How To Create Url Compression Test Tool Through Laravel

Step 1:- Create a blade page

<h4>URL Compression Test</h4>
                  <div class="form-group">
                     <br>
                     <br>
                     <br>
                     <p>This tool checks your server to see if it is sending out compressed data. It checks for compression via
                        mod_gzip, mod_deflate, or any server-side language that does content compression. Enter the address of a
                        specific page or file to check</p>
                     <br>
                     
                     <form action="{{route('submitUrlToCheck')}}" method="post">
                        @csrf
                        <div class="input-group mb-3">
                           <input type="text" name="url" class="form-control" placeholder="Enter a url to test" aria-label="Recipient's username" aria-describedby="basic-addon2">
                           <div class="input-group-append">
                              <button class="btn btn-primary" style="padding-right: 50px;" type="submit">Check</button>
                           </div>
                        </div>
                     </form>
                     
                     <br>
                     <br>
                     @if($encoding == 'firstTime')

                     @elseif($encoding == 'else')
                     <div>
                        <h5>{{$url}} <span class="text-danger">is NOT Compressed</span> </h5>
                     </div>
                     @else
                     <div>
                        <h5>{{$url}} <span class="text-success"> is Compressed with {{$encoding}}</span></h5>
                     </div>
                     @endif
                     <br>
                     
                     @if($uncompressedSizeFormatted > 0)
                     <h5>Page size: {{$uncompressedSizeFormatted}}</h5>
                     @endif
                     <br>
                     
                     @if($compressedSizeFormatted > 0)
                     <h5>Compressed Page Size: {{$compressedSizeFormatted}}</h5>
                     @endif
                     <br>
                     
                     @if($percentageSavings > 0)
                     <h5>Potential Savings: {{$percentageSavings}}</h5>
                     @endif

Step 2:- Create a route on web.php

Route::get('checkUrlCompression',[App\Http\Controllers\CheckUrlCompressionController::class, 'index'])->name('checkUrlCompression');
Route::post('submitUrlToCheck',[App\Http\Controllers\CheckUrlCompressionController::class, 'post'])->name('submitUrlToCheck');

Step 3:- Create a controller

 public function index()
    {
        $encoding = 'firstTime';
        $url = '';
        $uncompressedSizeFormatted = " ";
        $compressedSizeFormatted = " ";
        $percentageSavings = " ";
        return view('CheckUrlCompression.index', compact('encoding', 'url', 'uncompressedSizeFormatted','percentageSavings','compressedSizeFormatted'));
    }
    
    public function post(Request $request)
{
    $ch = curl_init($request->url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_ENCODING, ""); // Request both compressed and uncompressed content
    $buffer = curl_exec($ch);
    $curl_info = curl_getinfo($ch);
    curl_close($ch);
    $header_size = $curl_info["header_size"];
    $headers = substr($buffer, 0, $header_size);
    $body = substr($buffer, $header_size);

    function getEncoding(&$headers)
    {
        $arr = explode("\r\n", trim($headers));
        array_shift($arr);
        foreach ($arr as $header) {
            list($k, $v) = explode(':', $header);
            if ('content-encoding' == strtolower($k)) {
                return trim($v);
            }
        }
        return false;
    }

    $encoding = getEncoding($headers);
    $url = $request->url;
    
    $uncompressedSize = strlen($body);

    if ($encoding) {
        // If compressed, calculate compressed page size
        $compressedSize = $uncompressedSize; // Initialize it with the uncompressed size
        $potentialSavings = 0;
    } else {
        // Not compressed
        $compressedSize = strlen(gzencode($body, 9, FORCE_GZIP));
        $potentialSavings = $uncompressedSize - $compressedSize;
    }

    $uncompressedSizeFormatted = $this->formatSize($uncompressedSize);
    $compressedSizeFormatted = $this->formatSize($compressedSize);
    $potentialSavingsFormatted = $this->formatSize($potentialSavings);

    if ($encoding) {
        return view('CheckUrlCompression.index', compact('encoding', 'url', 'uncompressedSizeFormatted', 'compressedSizeFormatted', 'potentialSavingsFormatted'));
    } else {
        $encoding = 'else';
        $percentageSavings = number_format(($potentialSavings / $uncompressedSize) * 100, 2) . '%';
        return view('CheckUrlCompression.index', compact('encoding', 'url', 'uncompressedSizeFormatted', 'compressedSizeFormatted', 'potentialSavingsFormatted', 'percentageSavings'));
    }
}

function getRemoteFilesize($url, $formatSize = true)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $headers = substr($response, 0, $headerSize);
    $body = substr($response, $headerSize);

    // Calculate the uncompressed page size
    $uncompressedSize = strlen($body);

    // Check if the response is compressed
    $encoding = getEncoding($headers);
    if ($encoding) {
        // If compressed, calculate compressed page size
        $compressedSize = strlen(gzencode($body, 9, FORCE_GZIP));
        // Calculate potential savings
        $potentialSavings = $uncompressedSize - $compressedSize;
    } else {
        // Not compressed
        $compressedSize = $uncompressedSize;
        $potentialSavings = 0;
    }

    if ($formatSize) {
        // Format sizes
        $uncompressedSize = $this->formatSize($uncompressedSize);
        $compressedSize = $this->formatSize($compressedSize);
        $potentialSavings = $this->formatSize($potentialSavings);
    }

    return compact('uncompressedSize', 'compressedSize', 'potentialSavings');
}

function formatSize($size)
{
    if ($size < 1024) {
        return $size . ' B';
    } elseif ($size < 1048576) {
        return round($size / 1024, 2) . ' KB';
    } elseif ($size < 1073741824) {
        return round($size / 1048576, 2) . ' MB';
    } elseif ($size < 1099511627776) {
        return round($size / 1073741824, 2) . ' GB';
    }
}

Modify it as per your requirements.

Hopefully, It will help you …. Happy Coding !!!!

Related Posts

How DataOps and MLOps Work Together for Scalable AI Pipelines

Introduction In the current landscape of artificial intelligence, building a model is only the beginning. The real challenge for enterprise teams lies in the transition from a…

Read More

Evaluating Modern DataOps Tools Across Business Analytics Infrastructure

Introduction Managing data pipelines used to be a straightforward task for single analytics teams. Today, data ecosystems are complex, fast-moving, and frequently fragmented across multiple cloud environments….

Read More

Essential Guide To Choosing And Mastering Modern Enterprise DataOps Platforms

Introduction DataOps platforms represent the modern standard for orchestrating the entire data lifecycle, from initial ingestion to final analytics delivery. By applying agile engineering and automated DevOps…

Read More

Exploring Financial Operations Workflows in Modern Cloud Environments

Introduction The Certified FinOps Professional is the definitive benchmark for experts looking to master the intersection of finance, engineering, and business. As organizations transition from traditional data…

Read More

Strategic Certified FinOps Engineer integrates governance with cloud operations

Introduction The shift to cloud computing has fundamentally altered how businesses manage infrastructure, but it has also introduced significant financial complexities that many engineering teams struggle to…

Read More

Certified FinOps Manager Knowledge for Cloud Financial Governance

Introduction The shift toward cloud-native infrastructure has brought undeniable speed, but it has also introduced significant financial complexity. The Certified FinOps Manager is a professional designation designed…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x