flash.moe/public/signature/signature.php
2021-07-29 21:58:32 +00:00

125 lines
4.3 KiB
PHP

<?php
define('SIG_WIDTH', 600);
define('SIG_HEIGHT', 80);
define('SIG_STATIC', 'signature.png');
define('SIG_AGE', 60);
define('FW_FONT_FAM', __DIR__ . '/../assets/Electrolize-Regular.ttf');
define('FW_FONT_SIZE', 30);
define('FW_TEXT', 'flashwave');
define('FA_FONT_FAM', __DIR__ . '/../css/fa/webfonts/fa-solid-900.ttf');
define('NP_FONT_FAM', __DIR__ . '/../assets/NotoSansJP-Regular.otf');
define('NP_FONT_SIZE', 16);
define('NP_FILE', 'np.json');
define('NP_URL', 'https://now.flash.moe/get.php?u=flashwave_');
define('NP_COVER', 'cover.png');
define('NP_COVER_URL', 'cover_url.txt');
define('NP_COVER_DEFAULT', '/www/now.flash.moe/public/resources/no-cover.png');
define('BG_IMAGE', 'bg3.png');
if(!isset($_GET['_force']) && is_file(SIG_STATIC) && time() - filemtime(SIG_STATIC) < SIG_AGE) {
$imagick = new Imagick(SIG_STATIC);
} else {
$ip_000 = new ImagickPixel('#000');
$ip_fff = new ImagickPixel('#fff');
$npRaw = file_get_contents(NP_URL);
$np = json_decode($npRaw);
if(is_array($np)) {
file_put_contents(NP_FILE, $npRaw);
if(empty($np[0]->images->large)) {
unlink(NP_COVER);
file_put_contents(NP_COVER_URL, '');
} elseif(!empty($np[0]->images->large) && $np[0]->images->large !== file_get_contents(NP_COVER_URL)) {
unlink(NP_COVER);
file_put_contents(NP_COVER_URL, $np[0]->images->large);
file_put_contents(NP_COVER, file_get_contents($np[0]->images->large));
}
}
if(empty($np))
$np = json_decode(file_get_contents(NP_FILE));
$nowPlaying = is_array($np) && !empty($np[0]->nowplaying);
$imagick = new Imagick;
$imagick->newImage(SIG_WIDTH, SIG_HEIGHT, $ip_000, 'png');
$background = new Imagick(BG_IMAGE);
$imagick->compositeImage($background, Imagick::COMPOSITE_COPY, 0, 0);
$background->destroy();
$footer = new Imagick('footer.png');
$imagick->compositeImage($footer, Imagick::COMPOSITE_MATHEMATICS, 0, SIG_HEIGHT - $footer->getImageHeight());
$footer->destroy();
if($nowPlaying) {
$npCover = new Imagick(is_file(NP_COVER) ? NP_COVER : NP_COVER_DEFAULT);
$npCover->resizeImage(60, 60, Imagick::FILTER_CATROM, 0.9, true);
$imagick->compositeImage($npCover, Imagick::COMPOSITE_COPY, SIG_WIDTH - 70, 10);
$npCover->destroy();
}
$imagickDraw = new ImagickDraw;
$imagickDraw->setFillColor($ip_fff);
$imagickDraw->setFont(FW_FONT_FAM);
$imagickDraw->setFontSize(FW_FONT_SIZE);
$imagickLogo = new Imagick();
$imagickLogo->newImage($imagick->getImageWidth(), $imagick->getImageHeight(), new ImagickPixel('none'), 'png');
$imagickLogo->annotateImage($imagickDraw, 15, 35, 0, FW_TEXT);
$imagickShadow = clone $imagickLogo;
$imagickShadow->shadowImage(80, 2, 0, 0);
$imagickShadow->compositeImage($imagickLogo, Imagick::COMPOSITE_OVER, 4, 2);
$imagickLogo->destroy();
$imagick->compositeImage($imagickShadow, Imagick::COMPOSITE_OVER, -4, -2);
$imagickShadow->destroy();
$imagickDraw->setFont(NP_FONT_FAM);
$imagickDraw->setFontSize(NP_FONT_SIZE);
if($nowPlaying) {
$imagick->annotateImage($imagickDraw, 40, 71, 0, sprintf('%s - %s', $np[0]->artist->name ?? '', $np[0]->name ?? ''));
$imagickDraw->setFont(FA_FONT_FAM);
$imagick->annotateImage($imagickDraw, 10, 71, 0, "\u{f001}");
} else {
$imagick->annotateImage($imagickDraw, 10, 71, 0, "If it ain't broke, I'll break it");
}
file_put_contents(SIG_STATIC, $imagick->getImageBlob());
}
switch($_GET['p'] ?? '') {
case 'album':
$imagick->cropImage(SIG_HEIGHT, SIG_HEIGHT, SIG_WIDTH - SIG_HEIGHT, 0);
break;
case 'album1':
$imagick->cropImage(SIG_HEIGHT, SIG_HEIGHT - 33, SIG_WIDTH - SIG_HEIGHT, 0);
break;
case 'album2':
$imagick->cropImage(SIG_HEIGHT, 33, SIG_WIDTH - SIG_HEIGHT, SIG_HEIGHT - 33);
break;
case 'body':
$imagick->cropImage(SIG_WIDTH - SIG_HEIGHT, SIG_HEIGHT, 0, 0);
break;
case 'body1':
$imagick->cropImage(SIG_WIDTH - SIG_HEIGHT, SIG_HEIGHT - 33, 0, 0);
break;
case 'body2':
$imagick->cropImage(SIG_WIDTH - SIG_HEIGHT, 33, 0, SIG_HEIGHT - 33);
break;
}
header('Content-Type: image/png');
header('Cache-Control: max-age=' . SIG_AGE . ', public');
echo $imagick->getImageBlob();
$imagick->destroy();