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
    link
    fedilink
    17
    edit-2
    1 year 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.

    • @[email protected]
      link
      fedilink
      101 year ago

      Damn, PHP is such a sleeper of a language, I always forget how useful it can be.Thanks for sharing!

      • SokathHisEyesOpen
        link
        fedilink
        31 year 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.

      • Skull giver
        link
        fedilink
        81 year 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.

        • @[email protected]
          link
          fedilink
          English
          1
          edit-2
          1 year ago

          Genuinely curious, how is it superior to Python in your opinion?

          Edit: Apart from the things you listed 😅

          • SokathHisEyesOpen
            link
            fedilink
            41 year ago

            It can run natively on an Apache server without any frameworks required to render user website markup and serve pages. That’s a pretty awesome advantage.