diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-07-11 15:01:02 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-07-11 15:01:02 +0300 |
commit | c2f382aba86aaebb9806ff1b43c1af69992e9a10 (patch) | |
tree | aee205721fd675926c5e7163eec0dddc448f7813 /model | |
parent | 24982a48f570b89e537850dda4a4d1ac33ea919f (diff) |
support dark mode for images with alpha channel
Diffstat (limited to 'model')
-rw-r--r-- | model/Page.php | 5 | ||||
-rw-r--r-- | model/Post.php | 18 | ||||
-rw-r--r-- | model/Upload.php | 59 |
3 files changed, 45 insertions, 37 deletions
diff --git a/model/Page.php b/model/Page.php index 6711a2c..f516836 100644 --- a/model/Page.php +++ b/model/Page.php @@ -24,10 +24,9 @@ class Page extends Model { return $this->updateTs && $this->updateTs != $this->ts; } - public function getHtml(bool $retina): string { + public function getHtml(bool $is_retina, string $user_theme): string { $html = $this->html; - if ($retina) - $html = markup::htmlRetinaFix($html); + $html = markup::htmlImagesFix($html, $is_retina, $user_theme); return $html; } diff --git a/model/Post.php b/model/Post.php index 10a396b..b0360ac 100644 --- a/model/Post.php +++ b/model/Post.php @@ -22,8 +22,8 @@ class Post extends Model { $data['update_ts'] = $cur_ts; if ($data['md'] != $this->md) { - $data['html'] = \markup::markdownToHtml($data['md']); - $data['text'] = \markup::htmlToText($data['html']); + $data['html'] = markup::markdownToHtml($data['md']); + $data['text'] = markup::htmlToText($data['html']); } parent::edit($data); @@ -31,15 +31,15 @@ class Post extends Model { } public function updateHtml() { - $html = \markup::markdownToHtml($this->md); + $html = markup::markdownToHtml($this->md); $this->html = $html; getDb()->query("UPDATE posts SET html=? WHERE id=?", $html, $this->id); } public function updateText() { - $html = \markup::markdownToHtml($this->md); - $text = \markup::htmlToText($html); + $html = markup::markdownToHtml($this->md); + $text = markup::htmlToText($html); $this->text = $text; getDb()->query("UPDATE posts SET text=? WHERE id=?", $text, $this->id); @@ -81,10 +81,9 @@ class Post extends Model { return date('j F Y', $this->updateTs); } - public function getHtml(bool $retina): string { + public function getHtml(bool $is_retina, string $theme): string { $html = $this->html; - if ($retina) - $html = markup::htmlRetinaFix($html); + $html = markup::htmlImagesFix($html, $is_retina, $theme); return $html; } @@ -173,9 +172,8 @@ class Post extends Model { foreach ($images[$u->randomId] as $s) { list($w, $h) = $s; list($w, $h) = $u->getImagePreviewSize($w, $h); - if ($u->createImagePreview($w, $h, $update)) { + if ($u->createImagePreview($w, $h, $update, $u->imageMayHaveAlphaChannel())) $images_affected++; - } } } diff --git a/model/Upload.php b/model/Upload.php index 441a14d..06b348b 100644 --- a/model/Upload.php +++ b/model/Upload.php @@ -38,7 +38,8 @@ class Upload extends Model { $h *= 2; } - return 'https://'.$config['uploads_host'].'/'.$this->randomId.'/p'.$w.'x'.$h.'.jpg'; + $prefix = $this->imageMayHaveAlphaChannel() ? 'a' : 'p'; + return 'https://'.$config['uploads_host'].'/'.$this->randomId.'/'.$prefix.$w.'x'.$h.'.jpg'; } // TODO remove? @@ -73,6 +74,12 @@ class Upload extends Model { return in_array(extension($this->name), self::$ImageExtensions); } + // assume all png images have alpha channel + // i know this is wrong, but anyway + public function imageMayHaveAlphaChannel(): bool { + return strtolower(extension($this->name)) == 'png'; + } + public function isVideo(): bool { return in_array(extension($this->name), self::$VideoExtensions); } @@ -94,36 +101,40 @@ class Upload extends Model { return [$w, $h]; } - /** - * @param ?int $w - * @param ?int $h - * @param bool $update Whether to proceed if preview already exists - * @return bool - */ - public function createImagePreview(?int $w = null, ?int $h = null, bool $update = false): bool { + public function createImagePreview(?int $w = null, + ?int $h = null, + bool $force_update = false, + bool $may_have_alpha = false): bool { global $config; $orig = $config['uploads_dir'].'/'.$this->randomId.'/'.$this->name; $updated = false; - for ($mult = 1; $mult <= 2; $mult++) { - $dw = $w * $mult; - $dh = $h * $mult; - $dst = $config['uploads_dir'].'/'.$this->randomId.'/p'.$dw.'x'.$dh.'.jpg'; + foreach (themes::getThemes() as $theme) { + if (!$may_have_alpha && $theme == 'dark') + continue; - if (file_exists($dst)) { - if (!$update) - continue; - unlink($dst); - } + for ($mult = 1; $mult <= 2; $mult++) { + $dw = $w * $mult; + $dh = $h * $mult; + + $prefix = $may_have_alpha ? 'a' : 'p'; + $dst = $config['uploads_dir'].'/'.$this->randomId.'/'.$prefix.$dw.'x'.$dh.($theme == 'dark' ? '_dark' : '').'.jpg'; - $img = imageopen($orig); - imageresize($img, $dw, $dh, [255, 255, 255]); - imagejpeg($img, $dst, $mult == 1 ? 93 : 67); - imagedestroy($img); + if (file_exists($dst)) { + if (!$force_update) + continue; + unlink($dst); + } - setperm($dst); - $updated = true; + $img = imageopen($orig); + imageresize($img, $dw, $dh, themes::getThemeAlphaColorAsRGB($theme)); + imagejpeg($img, $dst, $mult == 1 ? 93 : 67); + imagedestroy($img); + + setperm($dst); + $updated = true; + } } return $updated; @@ -138,7 +149,7 @@ class Upload extends Model { $files = scandir($dir); $deleted = 0; foreach ($files as $f) { - if (preg_match('/^p(\d+)x(\d+)\.jpg$/', $f)) { + if (preg_match('/^[ap](\d+)x(\d+)(?:_dark)?\.jpg$/', $f)) { if (is_file($dir.'/'.$f)) unlink($dir.'/'.$f); else |