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

Strategic DevOps Career Growth and High Salary Skills

Introduction The digital landscape is shifting rapidly. As companies across the globe transition to cloud-native infrastructures, the demand for professionals who can bridge the gap between development…

Read More

Top DevOps Certifications: Dominate Kubernetes, Cloud, And Automation

Introduction The cloud infrastructure world is moving faster than ever, and the demand for production-ready engineering talent is breaking records. Teams everywhere are desperately trying to bridge…

Read More

Streamlining Distributed Pipelines with DataOps Multi-Cloud Data Management

Introduction Modern business operations generate massive amounts of information every single second. To store, process, and analyze this information, organizations no longer rely on a single data…

Read More

Ultimate DataOps Automation Tools Guide: Build and Orchestrate Scalable Pipelines

Introduction Modern enterprises run on data, yet managing the underlying infrastructure remains a massive operational challenge. Historically, data workflows were handled manually. Data engineers wrote custom scripts,…

Read More

Accelerate Your Pipeline: Implementing Real-Time DataOps

Introduction Real-time DataOps is a critical evolution in how modern organizations manage the constant flow of information. By integrating automation, continuous testing, and real-time processing, businesses can…

Read More

Calculate Your Canada PR Points: The Complete Guide to Boosting Your CRS Score

Introduction Canada uses an objective, merit-based points system to select the most qualified candidates from around the world. To assess your chances, you need to use a…

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