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

Certified AIOps Manager: Strategic Framework for Intelligent IT Operations

Introduction The Certified AIOps Manager program is a specialized training designed to help professionals lead the next wave of IT operations. This guide is for engineers and…

Read More

Advanced AIOps Architect Certification Roadmap for DevOps Engineers

Introduction The Certified AIOps Architect is a comprehensive professional program designed for engineers and architects who want to master the intersection of Artificial Intelligence and IT Operations….

Read More

Advanced Certified AIOps Professional Guide for Mastering AI Driven Operations Skills

Introduction Artificial Intelligence for IT Operations is the future of managing complex systems and large scale digital environments. The Certified AIOps Professional program is designed for those…

Read More

Certified AIOps Engineer Training to Boost Automation Monitoring and Career Growth

The Certified AIOps Engineer is a specialized professional program designed to integrate artificial intelligence into modern IT operations. As systems scale and generate massive amounts of telemetry…

Read More

Advanced Guide to AIOps Foundation Certification for Scalable IT Infrastructure

In an era where infrastructure and applications generate massive amounts of telemetry data, manual intervention is no longer a sustainable strategy for maintaining system uptime. The AIOps…

Read More

Advanced Certified Site Reliability Manager Learning Path for DevOps Teams

Introduction The Certified Site Reliability Manager program is an essential credential for those aiming to lead high-performance engineering teams in the modern era of cloud computing. As organizations transition…

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