Skip to content

Commit

Permalink
fix(parsing/medium): weserv img still too big
Browse files Browse the repository at this point in the history
Signed-off-by: Rongrong <[email protected]>
  • Loading branch information
Rongronggg9 committed Dec 9, 2023
1 parent 505be54 commit ca8c577
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Bug fixes

- **"Remote" `/test` unavailable**: Fix a bug preventing the bot manager from using the `/test` command "remotely".
- **Resized images still too big**: Fix a bug causing images resized by `wsrv.nl` to be sometimes too big (exceed the 5MiB limitation of Telegram DC) to send.

## Significant performance improvement, native blockquote and syntax highlighting (v2.4.0)

Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Bug 修复

- **“远程” `/test` 不可用**:修复阻止 bot 管理员“远程”使用 `/test` 命令的错误。
- **调节尺寸后的图像仍然太大**: 修复了一个错误,这个错误导致经过 `wsrv.nl` 调节尺寸后的图像有时候仍然太大(超过 Telegram DC 的 5MiB 限制)以至于无法发送。

## 显著提高性能、原生块状引用和语法高亮(v2.4.0)

Expand Down
18 changes: 14 additions & 4 deletions src/parsing/medium.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ def __init__(self, urls: Union[str, list[str]]):
self.urls = new_urls
self.type_fallback_urls = new_urls.copy()
urls_not_weserv = [url for url in self.urls if not url.startswith(env.IMAGES_WESERV_NL)]
self.urls.extend(construct_weserv_url_convert_to_2560_png(urls_not_weserv[i])
self.urls.extend(construct_weserv_url_convert_to_2560(urls_not_weserv[i])
for i in range(min(len(urls_not_weserv), 3))) # use for final fallback
self.chosen_url = self.urls[0]

Expand Down Expand Up @@ -981,6 +981,7 @@ def construct_weserv_url(url: str,
height: Optional[int] = None,
fit: Optional[str] = None,
output_format: Optional[str] = None,
quality: Optional[int] = None,
without_enlargement: Optional[bool] = None,
default_image: Optional[str] = None) -> str:
return (
Expand All @@ -990,18 +991,27 @@ def construct_weserv_url(url: str,
+ (f'&h={height}' if height else '')
+ (f'&fit={fit}' if fit else '')
+ (f'&output={output_format}' if output_format else '')
+ (f'&q={quality}' if quality else '')
+ (f'&we=1' if without_enlargement else '')
+ (f'&default={weserv_param_encode(default_image)}' if default_image else '')
)


def construct_weserv_url_convert_to_2560_png(url: str) -> str:
def construct_weserv_url_convert_to_2560(url: str) -> str:
return construct_weserv_url(
url,
width=2560,
height=2560,
fit='inside',
output_format='png',
# fit='inside', # is default
output_format='jpg',
# In the worst case, 89% ensures the size won't exceed 5MB
# E.g.:
# 2560x2560 white noise truecolor --89% JPEG--> 4.98MiB
# 2560x2560 white noise truecolor --90% JPEG--> 10.26MiB
# See also:
# https://robson.plus/white-noise-image-generator/
# https://fotoforensics.com/tutorial-estq.php
quality=89,
without_enlargement=True
)

Expand Down
4 changes: 2 additions & 2 deletions src/parsing/post_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .splitter import get_plain_text_length
from .html_parser import parse
from .html_node import *
from .medium import Media, Image, Video, Audio, File, Animation, construct_weserv_url_convert_to_2560_png
from .medium import Media, Image, Video, Audio, File, Animation, construct_weserv_url_convert_to_2560

AUTO: Final = 0
DISABLE: Final = -1
Expand Down Expand Up @@ -538,7 +538,7 @@ async def parse_html(self):
medium = File(enclosure.url)
elif any(keyword in enclosure.type for keyword in ('webp', 'svg')):
medium = Image(enclosure.url)
medium.url = construct_weserv_url_convert_to_2560_png(enclosure.url)
medium.url = construct_weserv_url_convert_to_2560(enclosure.url)
elif enclosure.type.startswith('image/gif'):
medium = Animation(enclosure.url)
elif enclosure.type.startswith('audio'):
Expand Down

0 comments on commit ca8c577

Please sign in to comment.