Skip to content

Commit 2d4fd9b

Browse files
committed
refine social images
1 parent 3e9aeaf commit 2d4fd9b

3 files changed

Lines changed: 29 additions & 17 deletions

File tree

codepoints.net/lib/Controller/OGImage.php

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use ImagickDraw;
77
use ImagickPixel;
88
use Codepoints\Controller;
9+
use Codepoints\Unicode\Codepoint;
10+
use Codepoints\Unicode\CodepointInfo\SENSITIVITY_LEVEL;
911

1012
final class OGImage extends Controller {
1113

@@ -14,29 +16,29 @@ final class OGImage extends Controller {
1416
*/
1517
#[\Override]
1618
public function __invoke($match, Array $env) : string {
19+
$cp = Codepoint::getCached($env['db']->getOne('SELECT cp, gc, name
20+
FROM codepoints
21+
WHERE cp = ?', hexdec($match[1])), $env['db']);
22+
1723
/* send an expiry of 1 year. This mirrors the Apache config for the
1824
* cached files. */
1925
$cache_duration = 365*24*60*60;
20-
$root = hexdec($match[1]);
21-
$info = $env['db']->getOne('SELECT gc, name
22-
FROM codepoints
23-
WHERE cp = ?', $root);
24-
$canonical = str_pad(strtoupper(dechex($root)), 4, '0', STR_PAD_LEFT);
2526
header('Content-Type: image/png');
2627
header('Cache-Control: public, max-age='.$cache_duration);
2728
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $cache_duration));
29+
2830
$dbimage = $env['db']->getOne('SELECT width, height, image
2931
FROM codepoint_image
30-
WHERE cp = ?', get_printable_codepoint($root, $info['gc']));
32+
WHERE cp = ?', get_printable_codepoint($cp->id, $cp->gc));
3133
if (! $dbimage) {
3234
http_response_code(404);
3335
return '';
3436
}
35-
$svg = sprintf(
36-
'<svg xmlns="http://www.w3.org/2000/svg" width="%s" height="%s" viewBox="0 0 1000 1000">'.
37-
'<style>:root svg{opacity:0;height:100%%;width:auto}:root svg:target{opacity:1}</style>'.
38-
'%s'.
39-
'</svg>', $dbimage['width'], $dbimage['height'], $dbimage['image']);
37+
$svg = preg_replace(
38+
'/viewBox="[^"]+"/',
39+
sprintf('viewBox="-50 0 %s %s"', $dbimage['width'] + 100, $dbimage['height'] + 100),
40+
$dbimage['image']);
41+
$ratio = ($dbimage['width'] + 100) / ($dbimage['height'] + 100);
4042

4143
$img = new Imagick();
4244
$img->setSize(1200, 630);
@@ -46,26 +48,36 @@ public function __invoke($match, Array $env) : string {
4648
$svgimg->setBackgroundColor('transparent');
4749
$svgimg->readImageBlob($svg);
4850
$svgimg->setImageFormat('png24');
49-
$svgimg->resizeImage(250, 250, imagick::FILTER_LANCZOS, 1);
50-
$img->compositeImage($svgimg, Imagick::COMPOSITE_DEFAULT, 100 + (250 - $svgimg->getImageWidth()) / 2, 100);
51+
if ($cp->sensitivity->value !== SENSITIVITY_LEVEL::NORMAL->value) {
52+
$blur = match($cp->sensitivity->value) {
53+
SENSITIVITY_LEVEL::RAISED->value => 50,
54+
SENSITIVITY_LEVEL::HIGH->value => 80,
55+
SENSITIVITY_LEVEL::MAX->value => 100,
56+
default => 0,
57+
};
58+
$svgimg->blurImage($blur, $blur);
59+
}
60+
$svgimg->resizeImage(ceil(min(400, 400 * $ratio)), ceil(min(400, 400 / $ratio)), imagick::FILTER_LANCZOS, 1);
61+
$img->compositeImage($svgimg, Imagick::COMPOSITE_DEFAULT, 50 + (int)((400 - $svgimg->getImageWidth()) / 2), 50);
5162

52-
$name = case_cp_name($info['name']);
63+
$name = case_cp_name($cp->name);
5364
$draw = new ImagickDraw();
5465
$draw->setFillColor('#1c1917');
5566
$draw->setFont(dirname(dirname(__DIR__)).static_url('src/fonts/Literata.woff2'));
5667

5768
$draw->setFontSize(100);
58-
$img->annotateImage($draw, 400, 350, 0, sprintf('U+%04X', $root));
69+
$img->annotateImage($draw, 500, 300, 0, sprintf('U+%04X', $cp->id));
5970

6071
$draw->setFontSize(strlen($name) < 34? 60 : 40);
61-
$img->annotateImage($draw, 75, 500, 0, $name);
72+
$img->annotateImage($draw, 50, 530, 0, $name);
6273

6374
$draw->setFillColor('rgb(95, 35, 40)');
6475
$draw->setFontSize(30);
65-
$img->annotateImage($draw, 75, 570, 0, sprintf('Read more on codepoints.net/U+%04X', $root));
76+
$img->annotateImage($draw, 50, 590, 0, sprintf('Read more on codepoints.net/U+%04X', $cp->id));
6677

6778
/* let the web server take over by storing already created images in
6879
* their place in the docroot */
80+
$canonical = str_pad(strtoupper(dechex($cp->id)), 4, '0', STR_PAD_LEFT);
6981
$img->writeImage(dirname(dirname(__DIR__)).'/image/og-'.$canonical.'.png');
7082
return $img->getImageBlob();
7183
}

src/images/og-image.xcf

8.64 KB
Binary file not shown.

src/public/images/og-image.png

-35.7 KB
Loading

0 commit comments

Comments
 (0)