From 947c68669d5b9b8c17e3f3a4babc62306e9579b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Thu, 13 Jun 2024 00:09:52 +0800 Subject: [PATCH] Add og:image:width and og:image:height. --- app/controllers/announcements_controller.rb | 29 +++++++++++++++++++-- app/models/bulletin.rb | 13 ++++++--- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index eb5c241..b5329b3 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -753,7 +753,21 @@ class AnnouncementsController < ApplicationController request = OrbitHelper.request meta_desc = announcement.subtitle.nil? || announcement.subtitle == "" ? announcement.text.to_s[0..200] : announcement.subtitle - OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => announcement.title},{"property" => "og:site_name", "content" => Site.first.title},{"property" => "og:url", "content" => request.original_url.split("?").first},{"property" => "og:description", "content" => meta_desc},{"property" => "og:image", "content" => "#{request.base_url}#{announcement.image.url}"},{"property" => "og:type", "content" => "Article"}]) + metas = [{"property" => "og:title", "content" => announcement.title},{"property" => "og:site_name", "content" => Site.first.title},{"property" => "og:url", "content" => request.original_url.split("?").first},{"property" => "og:description", "content" => meta_desc},{"property" => "og:type", "content" => "Article"}] + if announcement.image.present? + metas << {"property" => "og:image", "content" => URI.join(request.base_url, URI.encode(announcement.image.url)).to_s} + img_src_path = announcement.image.path + if File.exist?(img_src_path) + image = MiniMagick::Image.open(img_src_path) + if image[:width] + metas << {"property" => "og:image:width", "content" => image[:width].to_s} + end + if image[:height] + metas << {"property" => "og:image:height", "content" => image[:height].to_s} + end + end + end + OrbitHelper.render_meta_tags(metas) subtitle_ann = nil img_src = nil @@ -876,7 +890,18 @@ class AnnouncementsController < ApplicationController meta_desc = "" end - OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => announcement["title_translations"][locale]},{"property" => "og:site_name", "content" => Site.first.title},{"property" => "og:url", "content" => request.original_url.split("?").first},{"property" => "og:description", "content" => meta_desc},{"property" => "og:image", "content" => announcement["image"]["original"]},{"property" => "og:type", "content" => "Article"}]) + metas = [{"property" => "og:title", "content" => announcement["title_translations"][locale]},{"property" => "og:site_name", "content" => Site.first.title},{"property" => "og:url", "content" => request.original_url.split("?").first},{"property" => "og:description", "content" => meta_desc},{"property" => "og:type", "content" => "Article"}] + if announcement["image"]["original"].present? + image = announcement["image"] + metas << {"property" => "og:image", "content" => image["original"]} + if image["width"] + metas << {"property" => "og:image:width", "content" => image["width"].to_s} + end + if image["height"] + metas << {"property" => "og:image:height", "content" => image["height"].to_s} + end + end + OrbitHelper.render_meta_tags(metas) datetime = announcement["postdate"] ? DateTime.parse(announcement["postdate"]) : nil diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index 9004e0f..19b0599 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -406,9 +406,16 @@ class Bulletin a["image_description_translations"] = self.image_description_translations a["image"] = {} a["display_img"] = self.display_img - a["image"]["original"] = ("#{base_url}" + self.image.url rescue "") - a["image"]["thumb"] = ("#{base_url}" + self.image.thumb.url rescue "") - a["image"]["mobile"] = ("#{base_url}" + self.image.mobile.url rescue "") + if self.image.present? + a["image"]["original"] = ("#{base_url}" + self.image.url rescue "") + a["image"]["thumb"] = ("#{base_url}" + self.image.thumb.url rescue "") + a["image"]["mobile"] = ("#{base_url}" + self.image.mobile.url rescue "") + image = MiniMagick::Image.open(self.image.path) rescue nil + if image + a["image"]["width"] = image[:width] + a["image"]["height"] = image[:height] + end + end a["tags"] = [] a["category"] = {} a["author"] = author