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
class ComponentFlag extends Component
{
const FLAGS_DIRECTORY = 'flags/';
private $width;
private $height;
private $mc;
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;
}
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
);
}
}
}