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

Elevating DevSecOps and SRE Efficiency with a Software Delivery Governance Platform

Introduction Enterprise software engineering has reached a tipping point where systemic complexity threatens structural delivery stability. Modern engineering organizations routinely support highly fragmented ecosystems populated by hundreds…

Read More

Best Hospitals in India for International Patients and Affordable Surgery Costs

Introduction Global healthcare costs are rising rapidly, forcing many families to look for alternative solutions when facing serious medical diagnoses. In countries like the United States, the…

Read More

A Beginner Guide to Data Analytics Automation using Enterprise DataOps Workflows

Organizations rely heavily on fast, accurate, and reliable business intelligence to make critical commercial decisions. Whether it is predicting customer churn or managing real-time inventory levels, business…

Read More

Integrating AI Tools in DataOps Pipelines: A Comprehensive Guide

Introduction Modern organizations deal with a massive influx of data from applications, IoT devices, and cloud services. Managing these data volumes requires speed, accuracy, and agility. Traditional…

Read More

Modern Cloud DataOps Platforms for Reliable Data Pipelines

Introduction Modern organizations depend heavily on data. Every department, from finance and sales to healthcare, manufacturing, marketing, and customer support, needs reliable data to make better decisions….

Read More

Advanced DataOps Monitoring Tools for Enterprises: A Comprehensive Implementation Guide

Introduction Enterprise data environments are becoming more complex as organizations depend on cloud platforms, data lakes, data warehouses, real-time pipelines, analytics tools, and automated workflows. When one…

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