Optimize social plugin: reuse resized logo

This commit is contained in:
Oleh Prypin 2022-10-17 15:51:11 +02:00
parent 5f8269b78f
commit c681be2f7f
No known key found for this signature in database
GPG Key ID: A93637E52C2AD3F5
2 changed files with 22 additions and 8 deletions

View File

@ -18,6 +18,7 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.
import concurrent.futures
import functools import functools
import logging import logging
import os import os
@ -64,6 +65,9 @@ class SocialPluginConfig(Config):
# Social plugin # Social plugin
class SocialPlugin(BasePlugin[SocialPluginConfig]): class SocialPlugin(BasePlugin[SocialPluginConfig]):
def __init__(self):
self._executor = concurrent.futures.ThreadPoolExecutor(4)
# Retrieve configuration # Retrieve configuration
def on_config(self, config): def on_config(self, config):
self.color = colors.get("indigo") self.color = colors.get("indigo")
@ -108,7 +112,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
self.color = { **self.color, **self.config.cards_color } self.color = { **self.color, **self.config.cards_color }
# Retrieve logo and font # Retrieve logo and font
self.logo = self._load_logo(config) self._resized_logo_promise = self._executor.submit(self._load_resized_logo, config)
self.font = self._load_font(config) self.font = self._load_font(config)
# Create social cards # Create social cards
@ -167,12 +171,10 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Render social card # Render social card
def _render_card(self, site_name, title, description): def _render_card(self, site_name, title, description):
logo = self.logo
# Render background and logo # Render background and logo
image = self._render_card_background((1200, 630), self.color["fill"]) image = self._render_card_background((1200, 630), self.color["fill"])
image.alpha_composite( image.alpha_composite(
logo.resize((144, int(144 * logo.height / logo.width))), self._resized_logo_promise.result(),
(1200 - 228, 64 - 4) (1200 - 228, 64 - 4)
) )
@ -304,6 +306,11 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
{ "name": "twitter:image", "content": url } { "name": "twitter:image", "content": url }
] ]
def _load_resized_logo(self, config, width = 144):
logo = self._load_logo(config)
height = int(width * logo.height / logo.width)
return logo.resize((width, height))
# Retrieve logo image or icon # Retrieve logo image or icon
def _load_logo(self, config): def _load_logo(self, config):
theme = config.theme theme = config.theme

View File

@ -18,6 +18,7 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.
import concurrent.futures
import functools import functools
import logging import logging
import os import os
@ -64,6 +65,9 @@ class SocialPluginConfig(Config):
# Social plugin # Social plugin
class SocialPlugin(BasePlugin[SocialPluginConfig]): class SocialPlugin(BasePlugin[SocialPluginConfig]):
def __init__(self):
self._executor = concurrent.futures.ThreadPoolExecutor(4)
# Retrieve configuration # Retrieve configuration
def on_config(self, config): def on_config(self, config):
self.color = colors.get("indigo") self.color = colors.get("indigo")
@ -108,7 +112,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
self.color = { **self.color, **self.config.cards_color } self.color = { **self.color, **self.config.cards_color }
# Retrieve logo and font # Retrieve logo and font
self.logo = self._load_logo(config) self._resized_logo_promise = self._executor.submit(self._load_resized_logo, config)
self.font = self._load_font(config) self.font = self._load_font(config)
# Create social cards # Create social cards
@ -167,12 +171,10 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Render social card # Render social card
def _render_card(self, site_name, title, description): def _render_card(self, site_name, title, description):
logo = self.logo
# Render background and logo # Render background and logo
image = self._render_card_background((1200, 630), self.color["fill"]) image = self._render_card_background((1200, 630), self.color["fill"])
image.alpha_composite( image.alpha_composite(
logo.resize((144, int(144 * logo.height / logo.width))), self._resized_logo_promise.result(),
(1200 - 228, 64 - 4) (1200 - 228, 64 - 4)
) )
@ -304,6 +306,11 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
{ "name": "twitter:image", "content": url } { "name": "twitter:image", "content": url }
] ]
def _load_resized_logo(self, config, width = 144):
logo = self._load_logo(config)
height = int(width * logo.height / logo.width)
return logo.resize((width, height))
# Retrieve logo image or icon # Retrieve logo image or icon
def _load_logo(self, config): def _load_logo(self, config):
theme = config.theme theme = config.theme