From 2b74c79359a96769d37ea8cbf1196bb881723c4d Mon Sep 17 00:00:00 2001 From: Alfredo Ramos Date: Fri, 28 Jun 2019 19:47:20 -0500 Subject: [PATCH] Add Open Graph image width, height and MIME type --- adm/style/acp_seo_metadata_settings.html | 21 +++++++ config/services.yml | 2 + controller/acp.php | 74 +++++++++++++++++++++--- event/listener.php | 11 ++-- includes/helper.php | 30 ++++++---- language/en/acp/info_acp_settings.php | 6 ++ migrations/v12x/m00_configuration.php | 48 +++++++++++++++ 7 files changed, 169 insertions(+), 23 deletions(-) create mode 100644 migrations/v12x/m00_configuration.php diff --git a/adm/style/acp_seo_metadata_settings.html b/adm/style/acp_seo_metadata_settings.html index 280329e..6b097f0 100644 --- a/adm/style/acp_seo_metadata_settings.html +++ b/adm/style/acp_seo_metadata_settings.html @@ -64,6 +64,27 @@

{{ lang('ACP_SEO_METADATA') }}

+
+
+ +
{{ lang('ACP_SEO_METADATA_DEFAULT_IMAGE_DIMENSIONS_EXPLAIN') }} +
+
+ + x + + {{ lang('PIXEL') }} +
+
+
+
+ +
{{ lang('ACP_SEO_METADATA_DEFAULT_IMAGE_TYPE_EXPLAIN') }} +
+
+ +
+
diff --git a/config/services.yml b/config/services.yml index acc2fb2..f04c929 100644 --- a/config/services.yml +++ b/config/services.yml @@ -16,6 +16,8 @@ services: - '@language' - '@user' - '@log' + - '@upload_imagesize' + - '@alfredoramos.seometadata.helper' alfredoramos.seometadata.helper: class: alfredoramos\seometadata\includes\helper diff --git a/controller/acp.php b/controller/acp.php index b8ca396..05aeb18 100644 --- a/controller/acp.php +++ b/controller/acp.php @@ -15,6 +15,8 @@ use phpbb\language\language; use phpbb\user; use phpbb\log\log; +use FastImageSize\FastImageSize; +use alfredoramos\seometadata\includes\helper; class acp { @@ -36,19 +38,27 @@ class acp /** @var \phpbb\log\log */ protected $log; + /** @var \FastImageSize\FastImageSize */ + protected $imagesize; + + /** @var \alfredoramos\seometadata\includes\helper */ + protected $helper; + /** * Controller constructor. * - * @param \phpbb\config\config $config - * @param \phpbb\template\template $template - * @param \phpbb\request\request $request - * @param \phpbb\language\language $language - * @param \phpbb\user $user - * @param \phpbb\log\log $log + * @param \phpbb\config\config $config + * @param \phpbb\template\template $template + * @param \phpbb\request\request $request + * @param \phpbb\language\language $language + * @param \phpbb\user $user + * @param \phpbb\log\log $log + * @param \FastImageSize\FastImageSize $imagesize + * @param \alfredoramos\seometadata\includes\helper $helper * * @return void */ - public function __construct(config $config, template $template, request $request, language $language, user $user, log $log) + public function __construct(config $config, template $template, request $request, language $language, user $user, log $log, FastImageSize $imagesize, helper $helper) { $this->config = $config; $this->template = $template; @@ -56,6 +66,8 @@ public function __construct(config $config, template $template, request $request $this->language = $language; $this->user = $user; $this->log = $log; + $this->imagesize = $imagesize; + $this->helper = $helper; } /** @@ -131,11 +143,54 @@ public function settings_mode($u_action = '') ); // Default image + $default_image = $this->request->variable('seo_metadata_default_image', ''); $this->config->set( 'seo_metadata_default_image', - $this->request->variable('seo_metadata_default_image', '') + $default_image ); + // Default image information + $default_image_info = [ + 'width' => $this->request->variable('seo_metadata_default_image_width', 0), + 'height' => $this->request->variable('seo_metadata_default_image_height', 0), + 'type' => $this->request->variable('seo_metadata_default_image_type', '') + ]; + + // Try to get image width, height and type + if (empty($default_image_info['width']) || empty($default_image_info['height']) || empty($default_image_info['type'])) + { + $default_image_info = $this->imagesize->getImageSize($this->helper->clean_image($default_image)); + + // Get MIME type as string + if (!empty($default_image_info['type'])) + { + $default_image_info['type'] = image_type_to_mime_type($default_image_info['type']); + } + } + + // Validate default image information + $valid_image_info = !empty($default_image_info) && + $default_image_info['width'] >= 200 && + $default_image_info['height'] >= 200 && + !empty($default_image_info['type']); + + // Default image information + if ($valid_image_info) + { + foreach ($default_image_info as $key => $value) + { + if (!in_array($key, ['width', 'height', 'type'], true)) + { + continue; + } + + $this->config->set( + sprintf('seo_metadata_default_image_%s', $key), + $value + ); + } + } + // Local images $local_images = $this->request->variable('seo_metadata_local_images', 1); $local_images = (in_array($local_images, [0, 1], true)) ? $local_images : 1; @@ -232,6 +287,9 @@ public function settings_mode($u_action = '') 'SEO_METADATA_META_DESCRIPTION' => ((int) $this->config['seo_metadata_meta_description'] === 1), 'SEO_METADATA_DESC_LENGTH' => (int) $this->config['seo_metadata_desc_length'], 'SEO_METADATA_DEFAULT_IMAGE' => $this->config['seo_metadata_default_image'], + 'SEO_METADATA_DEFAULT_IMAGE_WIDTH' => (int) $this->config['seo_metadata_default_image_width'], + 'SEO_METADATA_DEFAULT_IMAGE_HEIGHT' => (int) $this->config['seo_metadata_default_image_height'], + 'SEO_METADATA_DEFAULT_IMAGE_TYPE' => trim($this->config['seo_metadata_default_image_type']), 'SEO_METADATA_LOCAL_IMAGES' => ((int) $this->config['seo_metadata_local_images'] === 1), 'SEO_METADATA_ATTACHMENTS' => ((int) $this->config['seo_metadata_attachments'] === 1), 'SEO_METADATA_PREFER_ATTACHMENTS' => ((int) $this->config['seo_metadata_prefer_attachments'] === 1), diff --git a/event/listener.php b/event/listener.php index 8507e5f..2e931a9 100644 --- a/event/listener.php +++ b/event/listener.php @@ -143,7 +143,7 @@ public function viewtopic($event) // Helpers $data['title'] = $event['topic_data']['topic_title']; $data['description'] = $this->helper->clean_description($data['description']); - $data['image'] = $this->helper->clean_image($data['image']); + $data['image']['url'] = $this->helper->clean_image($data['image']['url']); $data['datetime'] = date('c', $event['topic_data']['topic_time']); $data['section'] = $event['topic_data']['forum_name']; $data['publisher'] = $this->config['seo_metadata_facebook_publisher']; @@ -156,13 +156,16 @@ public function viewtopic($event) 'twitter_cards' => [ 'twitter:title' => $data['title'], 'twitter:description' => $data['description'], - 'twitter:image' => $data['image'] + 'twitter:image' => $data['image']['url'] ], 'open_graph' => [ 'og:type' => 'article', 'og:title' => $data['title'], 'og:description' => $data['description'], - 'og:image' => $data['image'], + 'og:image' => $data['image']['url'], + 'og:image:type' => $data['image']['type'], + 'og:image:width' => $data['image']['width'], + 'og:image:height' => $data['image']['height'], 'article:published_time' => $data['datetime'], 'article:section' => $data['section'], 'article:publisher' => $data['publisher'], @@ -170,7 +173,7 @@ public function viewtopic($event) 'json_ld' => [ 'headline' => $data['title'], 'description' => $data['description'], - 'image' => $data['image'] + 'image' => $data['image']['url'] ] ] ); diff --git a/includes/helper.php b/includes/helper.php index eac847f..d4ec0ae 100644 --- a/includes/helper.php +++ b/includes/helper.php @@ -116,7 +116,10 @@ public function set_metadata($data = [], $key = '') 'og:type' => 'website', 'og:title' => '', 'og:description' => $default['description'], - 'og:image' => $default['image'] + 'og:image' => $default['image'], + 'og:image:type' => '', + 'og:image:width' => 0, + 'og:image:height' => 0 ], 'json_ld' => [ '@context' => 'http://schema.org', @@ -464,7 +467,7 @@ public function extract_description($post_id = 0) * @param integer $post_id * @param integer $max_images * - * @return array + * @return array url, width, height and type */ public function extract_image($description = '', $post_id = 0, $max_images = 3) { @@ -482,7 +485,7 @@ public function extract_image($description = '', $post_id = 0, $max_images = 3) // Check cached image first if (!empty($cached_image['url'])) { - return $cached_image['url']; + return $cached_image; } $image_strategy = abs((int) $this->config['seo_metadata_image_strategy']); @@ -581,17 +584,17 @@ public function extract_image($description = '', $post_id = 0, $max_images = 3) // Filter images foreach ($images as $key => $value) { - $size = $this->imagesize->getImageSize($value); + $info = $this->imagesize->getImageSize($value); // Can't get image dimensions - if (empty($size)) + if (empty($info)) { unset($images[$key]); continue; } // Images should be at least 200x200 px - if (($size['width'] < 200) || ($size['height'] < 200)) + if (($info['width'] < 200) || ($info['height'] < 200)) { unset($images[$key]); continue; @@ -599,9 +602,9 @@ public function extract_image($description = '', $post_id = 0, $max_images = 3) $images[$key] = [ 'url' => $value, - 'width' => $size['width'], - 'height' => $size['height'], - 'type' => image_type_to_mime_type($size['type']) + 'width' => $info['width'], + 'height' => $info['height'], + 'type' => image_type_to_mime_type($info['type']) ]; } @@ -631,13 +634,18 @@ public function extract_image($description = '', $post_id = 0, $max_images = 3) // Fallback image if (empty($images[0])) { - return $this->config['seo_metadata_default_image']; + return [ + 'url' => trim($this->config['seo_metadata_default_image']), + 'width' => (int) $this->config['seo_metadata_default_image_width'], + 'height' => (int) $this->config['seo_metadata_default_image_height'], + 'type' => trim($this->config['seo_metadata_default_image_type']) + ]; } // Add image to cache $this->cache->put($cache_name, $images[0]); - return $images[0]['url']; + return $images[0]; } /** diff --git a/language/en/acp/info_acp_settings.php b/language/en/acp/info_acp_settings.php index bb22272..10ebb7f 100644 --- a/language/en/acp/info_acp_settings.php +++ b/language/en/acp/info_acp_settings.php @@ -46,6 +46,12 @@ 'ACP_SEO_METADATA_DEFAULT_IMAGE' => 'Default image', 'ACP_SEO_METADATA_DEFAULT_IMAGE_EXPLAIN' => 'Default image URL for meta tags such as og:image. It will only be used if an image cannot be found within the current page. It must be relative to %s', + 'ACP_SEO_METADATA_DEFAULT_IMAGE_DIMENSIONS' => 'Default image dimensions', + 'ACP_SEO_METADATA_DEFAULT_IMAGE_DIMENSIONS_EXPLAIN' => 'Width x height of default image. Set both to 0 to try to guess the image dimensions.', + + 'ACP_SEO_METADATA_DEFAULT_IMAGE_TYPE' => 'Default image type', + 'ACP_SEO_METADATA_DEFAULT_IMAGE_TYPE_EXPLAIN' => 'The MIME type of default image. Leave it blank to try to guess the type, if you do not know this information or are unsure.', + 'ACP_SEO_METADATA_LOCAL_IMAGES' => 'Local images', 'ACP_SEO_METADATA_LOCAL_IMAGES_EXPLAIN' => 'Only extract post images from your domain (%s).', diff --git a/migrations/v12x/m00_configuration.php b/migrations/v12x/m00_configuration.php new file mode 100644 index 0000000..5a1158a --- /dev/null +++ b/migrations/v12x/m00_configuration.php @@ -0,0 +1,48 @@ + + * @copyright 2018 Alfredo Ramos + * @license GPL-2.0-only + */ + +namespace alfredoramos\seometadata\migrations\v12x; + +use phpbb\db\migration\migration; + +class m00_configuration extends migration +{ + /** + * Migration dependencies. + * + * @return array + */ + static public function depends_on() + { + return ['\alfredoramos\seometadata\migrations\v11x\m00_configuration']; + } + + /** + * Add configuration. + * + * @return array + */ + public function update_data() + { + return [ + [ + 'config.add', + ['seo_metadata_default_image_width', 0] + ], + [ + 'config.add', + ['seo_metadata_default_image_height', 0] + ], + [ + 'config.add', + ['seo_metadata_default_image_type', ''] + ] + ]; + } +}