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

Ask a Doctor Online: A Simple Guide to Virtual Healthcare

In our fast-paced world, the traditional trip to a doctor’s office isn’t always the most practical first step when health concerns arise. Whether you are dealing with…

Read More

Best Free Programming Ebooks to Learn Software Development

Learning modern technology can feel overwhelming when tutorials are scattered across dozens of websites. Many software developers, engineering students, and IT professionals struggle to find comprehensive study…

Read More

FinOps Training: Understanding Cost Visibility and Optimization

Introduction Moving workloads to the cloud can simplify infrastructure management, but it can also introduce a new financial challenge. Teams can provision resources whenever they need them,…

Read More

The Architecture of Intelligent DataOps: From Ingestion to Automation

Introduction Modern organizations run on distributed data ecosystems. On any given day, an enterprise environment ingests, transforms, and serves petabytes of records sourced from transactional databases, external…

Read More

Transforming Data Reliability: How DataOps Platforms Drive Proactive Monitoring

Introduction Traditional monitoring focuses almost entirely on infrastructure availability and binary job execution states—whether a server is up or whether a task completed. However, modern distributed environments…

Read More

Navigating Pipeline Risks with Expert DevSecOps Consulting Services

Software delivery moves faster today than at any point in technological history. High-performing engineering organizations push code changes to production multiple times a day using automated deployment…

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