Note: This post now archived and as such no longer works

An external image showing your user-agent and the total "hit count"

  • Skull giver@popplesburger.hilciferous.nl
    link
    fedilink
    arrow-up
    15
    ·
    edit-2
    11 months ago

    Probably has bugs. Probably no security bugs. Feedback is welcome (but I don’t care enough about this to try my hardest).

    require_once('/var/www/html/geoip2.phar');
    use GeoIp2\Database\Reader;
    
    $ip = $_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR'];
    
    $cityReader = new Reader('/var/local/php/GeoLite2-City.mmdb');
    $record = $cityReader->city($ip);
    
    header('Content-Type: image/png');
    
    $image = @imagecreatefrompng('lemmybase.png');
    
    $black = imagecolorallocate($image, 0, 0, 0);
    
    // "Some City, SS, Country Name"
    $text = $record->city->name . ', ' . $record->mostSpecificSubdivision->isoCode . ', ' . $record->country->name;
    
    /* $font_path = '/tmp/ComicSand.ttf'; */
    $font_path = '/usr/share/fonts/ubuntu/Ubuntu-M.ttf';
    
    // Render text
    imagettftext($image, 30, 0, 28, 224, $black, $font_path, chunk_split($text, 22));
    
    // Dump image to web server
    imagepng($image);
    
    // Free resources
    imagedestroy($image);
    

    Edit: damn, Lemmy really hates < ? php. Just imagine that’s the first line in the file.

      • Skull giver@popplesburger.hilciferous.nl
        link
        fedilink
        arrow-up
        6
        ·
        11 months ago

        PHP is underappreciated, especially recent PHP. Null coalescing operators! Actually typed variables that produce an error if you pass the wrong type! It’s superior to Python despite it’s mid-2000s-spaghetti-college-kid-developer reputation.

        Hell, I may get downvoted for this, but I honestly believe PHP’s Doctrine is superior to Java/Kotlin’s Hibernate. Symfony and Spring are almost equally good in terms of functionality, though PHP is quite a lot slower, sadly.

      • SokathHisEyesOpen@lemmy.ml
        link
        fedilink
        arrow-up
        1
        ·
        11 months ago

        PHP is the OG bad-ass for getting shit done. No setup, no compile, no deployment pipelines. Hell, you can create and write the files right there on the server with nothing more than an SSH terminal if you want.