emptygif
Language: English

All languages and frameworks

FrankenPHP (worker mode) PHP

A PHP snippet for FrankenPHP (worker mode) that answers requests for /pixel.gif with the empty GIF, decoded once at startup.

FrankenPHP (worker mode) PHP

File
index.php
Run
frankenphp php-server --listen :8080 --worker index.php
<?php

// Worker script: booted once, then handles requests in a loop.
ignore_user_abort(true);

$gif = base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');

$handler = static function () use ($gif): void {
    if (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) !== '/pixel.gif') {
        http_response_code(404);
        return;
    }
    header('Content-Type: image/gif');
    header('Content-Length: ' . strlen($gif));
    header('Cache-Control: no-store');
    echo $gif;
};

while (frankenphp_handle_request($handler)) {
    gc_collect_cycles();
}

We ran this snippet and compared the bytes it returned.

Check it

curl -sI http://localhost:8080/pixel.gif

Official site: FrankenPHP (worker mode)

More in PHP