flash.moe/public/signature/signature.php

137 lines
4.8 KiB
PHP
Raw Normal View History

2020-08-20 00:02:37 +00:00
<?php
define('SIG_WIDTH', 600);
2022-02-04 04:25:57 +00:00
define('SIG_HEIGHT', 100);
2022-09-25 21:58:06 +00:00
define('SIG_STATIC', __DIR__ . '/signature.png');
2020-08-20 00:02:37 +00:00
define('SIG_AGE', 60);
2022-09-25 21:58:06 +00:00
define('FW_FONT_FAM', __DIR__ . '/Electrolize-Regular.ttf');
2020-08-20 00:02:37 +00:00
define('FW_FONT_SIZE', 30);
2021-07-29 21:58:32 +00:00
define('FW_TEXT', 'flashwave');
2022-09-25 21:58:06 +00:00
define('FA_FONT_FAM', __DIR__ . '/fa-solid-900.ttf');
define('NP_FONT_FAM', __DIR__ . '/NotoSansJP-Regular.otf');
2020-08-20 00:02:37 +00:00
define('NP_FONT_SIZE', 16);
2022-09-25 21:58:06 +00:00
define('NP_FILE', __DIR__ . '/np.json');
2020-08-20 00:02:37 +00:00
define('NP_URL', 'https://now.flash.moe/get.php?u=flashwave_');
2022-09-25 21:58:06 +00:00
define('NP_COVER', __DIR__ . '/cover.png');
define('NP_COVER_URL', __DIR__ . '/cover_url.txt');
2022-02-04 04:25:57 +00:00
define('NP_COVER_URL_DEFAULT', 'https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png');
2022-09-25 21:58:06 +00:00
define('NP_COVER_DEFAULT', __DIR__ . '/no-cover.png');
2022-02-04 04:25:57 +00:00
$currentDate = (int)ltrim(date('md'), '0');
if($currentDate > 1205 && $currentDate < 1227) {
define('BG_IMAGE', 'bg5.png');
} else {
define('BG_IMAGE', 'bg4.png');
}
2020-08-20 00:02:37 +00:00
2021-07-29 21:58:32 +00:00
if(!isset($_GET['_force']) && is_file(SIG_STATIC) && time() - filemtime(SIG_STATIC) < SIG_AGE) {
2020-08-20 00:02:37 +00:00
$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);
2022-02-04 04:25:57 +00:00
if(empty($np[0]->images->large) || $np[0]->images->large === NP_COVER_URL_DEFAULT) {
2020-08-20 00:02:37 +00:00
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');
2021-07-29 21:58:32 +00:00
$background = new Imagick(BG_IMAGE);
2020-08-20 00:02:37 +00:00
$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();
2022-02-04 04:25:57 +00:00
$hasCover = $nowPlaying && is_file(NP_COVER);
if($hasCover) {
$npCover = new Imagick(/*is_file(NP_COVER) ?*/ NP_COVER /*: NP_COVER_DEFAULT*/);
$npCover->resizeImage(80, 80, Imagick::FILTER_CATROM, 0.9, true);
$imagick->compositeImage($npCover, Imagick::COMPOSITE_COPY, SIG_WIDTH - 90, 10);
2020-08-20 00:02:37 +00:00
$npCover->destroy();
}
$imagickDraw = new ImagickDraw;
2022-02-04 04:25:57 +00:00
$imagickDraw->setFillColor(BG_IMAGE === 'bg4.png' ? $ip_000 : $ip_fff);
2020-08-20 00:02:37 +00:00
$imagickDraw->setFont(FW_FONT_FAM);
$imagickDraw->setFontSize(FW_FONT_SIZE);
$imagickLogo = new Imagick();
$imagickLogo->newImage($imagick->getImageWidth(), $imagick->getImageHeight(), new ImagickPixel('none'), 'png');
2022-02-04 04:25:57 +00:00
$imagickLogo->annotateImage($imagickDraw, $hasCover ? 367 : 457, 60, 0, FW_TEXT);
2020-08-20 00:02:37 +00:00
$imagickShadow = clone $imagickLogo;
2022-02-04 04:25:57 +00:00
$imagickShadow->shadowImage(80, 5, 0, 0);
2020-08-20 00:02:37 +00:00
$imagickShadow->compositeImage($imagickLogo, Imagick::COMPOSITE_OVER, 4, 2);
$imagickLogo->destroy();
$imagick->compositeImage($imagickShadow, Imagick::COMPOSITE_OVER, -4, -2);
$imagickShadow->destroy();
2022-02-04 04:25:57 +00:00
$imagickDraw->setFillColor($ip_fff);
2020-08-20 00:02:37 +00:00
$imagickDraw->setFont(NP_FONT_FAM);
$imagickDraw->setFontSize(NP_FONT_SIZE);
if($nowPlaying) {
2022-02-04 04:25:57 +00:00
$imagick->annotateImage($imagickDraw, 40, 91, 0, sprintf('%s - %s', $np[0]->artist->name ?? '', $np[0]->name ?? ''));
2020-08-20 00:02:37 +00:00
$imagickDraw->setFont(FA_FONT_FAM);
2022-02-04 04:25:57 +00:00
$imagick->annotateImage($imagickDraw, 10, 91, 0, "\u{f001}");
2020-08-20 00:02:37 +00:00
} else {
2022-02-04 04:25:57 +00:00
$imagick->annotateImage($imagickDraw, 10, 91, 0, "If it ain't broke, I'll break it");
2020-08-20 00:02:37 +00:00
}
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();