Overview

Classes

  • Card
  • CardRegular
  • Component
  • ComponentAvatar
  • ComponentFlag
  • ComponentImage
  • ComponentLabel
  • ComponentXPBar
  • ErrorImage
  • OsuAPI
  • OsuSignature
  • PredefinedColours
  • Signature
  • Template
  • TemplateNormal
  • Utils
  • Overview
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 
<?php
/**
 * The flag component. Looks for a country flag in {@link FLAGS_DIRECTORY} based on the user's country code.
 *
 * `&flagshadow` can be used globally and adds a 3px 50% opacity shadow to the flag.
 * `&flagstroke` can be used globally and adds a 1px white rounded stroke with 93.3% opacity around the flag.
 *
 * @author Lemmmy
 * @see Component
 */
class ComponentFlag extends Component
{
    const FLAGS_DIRECTORY = 'flags/';

    /**
     * The width of this flag
     *
     * @var int
     */
    private $width;

    /**
     * The height of this flag
     *
     * @var int
     */
    private $height;


    /**
     * The memcache object
     *
     * @var Memcached
     */
    private $mc;

    /**
     * @param OsuSignature $signature The base signature
     * @param int $x The X position of this flag
     * @param int $y The Y position of this flag
     * @param int $width The width of this flag
     * @param int $height The height of this flag
     */
    public function __construct(OsuSignature $signature, $x = 0, $y = 0, $width = 18, $height = 12) {
        parent::__construct($signature, $x, $y);

        $this->width = $width;
        $this->height = $height;

        $this->mc = Utils::getMemcache();
    }

    public function getWidth() {
        return $this->width;
    }

    public function getHeight() {
        return $this->height;
    }

    /**
     * Gets the user's flag
     *
     * @param $user array The user
     *
     * @return Imagick|null The flag, or nothing.
     */
    private function getFlag($user) {
        $country = $user['country'];

        if (!$country) return null;

        $flag = new Imagick();
        $cachedPicture = $this->mc->get("osusigv3_flag_" . strtolower($country));

        if (!$cachedPicture) {
            $flagBlob = @file_get_contents(self::FLAGS_DIRECTORY . $country . '.png');

            if ($flagBlob === false) {
                return null;
            }

            $flag->readImageBlob($flagBlob);;
            $flag->setImageFormat('png');

            $this->mc->set("osusigv3_flag_" . strtolower($country), base64_encode($flag->getImageBlob()), 43200);

            return $flag;
        } else {
            $decodedPicture = base64_decode($cachedPicture);
            $flag->readImageBlob($decodedPicture);
            $flag->setImageFormat('png');

            return $flag;
        }
    }

    public function draw(OsuSignature $signature)
    {
        parent::draw($signature);

        $flag = $this->getFlag($signature->getUser());

        if ($flag) {
            $flag->resizeImage(
                $this->getWidth(),
                $this->getHeight(),
                Imagick::FILTER_CATROM,
                1
            );

            if (isset($_GET['flagshadow'])) {
                $shadow = $flag->clone();
                $shadow->setImageBackgroundColor(new ImagickPixel('black'));

                $shadow->shadowImage(50, 3, 0, 0);

                $signature->getCanvas()->compositeImage($shadow, \Imagick::COMPOSITE_DEFAULT, $this->x - 6, $this->y - 6);
            }

            if (isset($_GET['flagstroke'])) {
                $flagStroke = new \ImagickDraw();

                $flagStroke->setFillColor("#FFFFFFEE");
                $flagStroke->roundRectangle($this->x - 1, $this->y - 1, ($this->x - 1) + ($this->getWidth() + 1), ($this->y - 1) + ($this->getHeight() + 1), 1, 1);

                $signature->getCanvas()->drawImage($flagStroke);
            }

            $signature->getCanvas()->compositeImage(
                $flag,
                Imagick::COMPOSITE_DEFAULT,
                $this->x,
                $this->y
            );
        }
    }
}
API documentation generated by ApiGen