fix image scaling for oembed photos

This commit is contained in:
Morgan McMillian 2020-12-16 21:29:23 -08:00
parent 20ba306748
commit 0347fa45eb

View file

@ -235,8 +235,8 @@ class PostItem(Gtk.ListBoxRow):
# header container
self.h_box = Gtk.Box(orientation='horizontal')
self.avatar = Handy.Avatar(size=48, text=post.user.username, show_initials=True)
self.avatar.set_image_load_func(self.get_image, post.user.content.avatar_image.link)
#self.avatar = Gtk.Image.new_from_pixbuf(self.get_image(48, post.user.content.avatar_image.link))
self.avatar.set_image_load_func(self.get_avatar, post.user.content.avatar_image.link)
#self.avatar = Gtk.Image.new_from_pixbuf(self.get_avatar(48, post.user.content.avatar_image.link))
self.h_box.pack_start(self.avatar, False, False, 18)
self.h_box.pack_start(self.name_box, False, False, 0)
@ -256,13 +256,13 @@ class PostItem(Gtk.ListBoxRow):
if raw.type == "io.pnut.core.oembed":
oembed = raw.value
if oembed.type == "photo":
photo = Gtk.Image.new_from_pixbuf(self.get_image(256, oembed.thumbnail_url))
photo = Gtk.Image.new_from_pixbuf(self.get_oembed_thumb(oembed))
self.c_box.pack_end(photo, False, False, 18)
self.box.pack_start(self.h_box, True, True, 10)
self.box.pack_start(self.c_box, True, True, 10)
def get_image(self, size, url):
def get_avatar(self, size, url):
# TODO: needs a cache
# TODO: can I make it not round?
r = requests.get(url)
@ -272,3 +272,16 @@ class PostItem(Gtk.ListBoxRow):
pixbuf = loader.get_pixbuf()
return pixbuf.scale_simple(size,size,GdkPixbuf.InterpType.BILINEAR)
def get_oembed_thumb(self, oembed):
url = oembed.thumbnail_url
old_width = oembed.thumbnail_width
old_height = oembed.thumbnail_height
ratio = old_width / old_height
new_height = 256 / ratio
r = requests.get(url)
loader = GdkPixbuf.PixbufLoader()
loader.write(r.content)
loader.close()
pixbuf = loader.get_pixbuf()
return pixbuf.scale_simple(256,new_height,GdkPixbuf.InterpType.BILINEAR)