Skip to content

Commit

Permalink
Add Open Graph image width, height and MIME type
Browse files Browse the repository at this point in the history
  • Loading branch information
AlfredoRamos committed Jun 29, 2019
1 parent faa0959 commit 2b74c79
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 23 deletions.
21 changes: 21 additions & 0 deletions adm/style/acp_seo_metadata_settings.html
Expand Up @@ -64,6 +64,27 @@ <h1>{{ lang('ACP_SEO_METADATA') }}</h1>
<input type="text" id="seo_metadata_default_image" name="seo_metadata_default_image"{% if SEO_METADATA_DEFAULT_IMAGE %} value="{{ SEO_METADATA_DEFAULT_IMAGE }}"{% endif %} class="full" />
</dd>
</dl>
<dl>
<dt>
<label for="seo_metadata_default_image_width">{{ lang('ACP_SEO_METADATA_DEFAULT_IMAGE_DIMENSIONS') }}{{ lang('COLON') }}</label>
<br /><span>{{ lang('ACP_SEO_METADATA_DEFAULT_IMAGE_DIMENSIONS_EXPLAIN') }}</span>
</dt>
<dd>
<input type="number" id="seo_metadata_default_image_width" name="seo_metadata_default_image_width" min="0" max="1000" value="{% if SEO_METADATA_DEFAULT_IMAGE_WIDTH %}{{ SEO_METADATA_DEFAULT_IMAGE_WIDTH }}{% else %}0{% endif %}" />
x
<input type="number" id="seo_metadata_default_image_height" name="seo_metadata_default_image_height" min="0" max="1000" value="{% if SEO_METADATA_DEFAULT_IMAGE_HEIGHT %}{{ SEO_METADATA_DEFAULT_IMAGE_HEIGHT }}{% else %}0{% endif %}" />
{{ lang('PIXEL') }}
</dd>
</dl>
<dl>
<dt>
<label for="seo_metadata_default_image_type">{{ lang('ACP_SEO_METADATA_DEFAULT_IMAGE_TYPE') }}{{ lang('COLON') }}</label>
<br /><span>{{ lang('ACP_SEO_METADATA_DEFAULT_IMAGE_TYPE_EXPLAIN') }}</span>
</dt>
<dd>
<input type="text" id="seo_metadata_default_image_type" name="seo_metadata_default_image_type"{% if SEO_METADATA_DEFAULT_IMAGE_TYPE %} value="{{ SEO_METADATA_DEFAULT_IMAGE_TYPE }}"{% endif %} class="narrow" />
</dd>
</dl>
<dl>
<dt>
<label for="seo_metadata_local_images">{{ lang('ACP_SEO_METADATA_LOCAL_IMAGES') }}{{ lang('COLON') }}</label>
Expand Down
2 changes: 2 additions & 0 deletions config/services.yml
Expand Up @@ -16,6 +16,8 @@ services:
- '@language'
- '@user'
- '@log'
- '@upload_imagesize'
- '@alfredoramos.seometadata.helper'

alfredoramos.seometadata.helper:
class: alfredoramos\seometadata\includes\helper
Expand Down
74 changes: 66 additions & 8 deletions controller/acp.php
Expand Up @@ -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
{
Expand All @@ -36,26 +38,36 @@ 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;
$this->request = $request;
$this->language = $language;
$this->user = $user;
$this->log = $log;
$this->imagesize = $imagesize;
$this->helper = $helper;
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
11 changes: 7 additions & 4 deletions event/listener.php
Expand Up @@ -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'];
Expand All @@ -156,21 +156,24 @@ 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'],
],
'json_ld' => [
'headline' => $data['title'],
'description' => $data['description'],
'image' => $data['image']
'image' => $data['image']['url']
]
]
);
Expand Down
30 changes: 19 additions & 11 deletions includes/helper.php
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
{
Expand All @@ -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']);
Expand Down Expand Up @@ -581,27 +584,27 @@ 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;
}

$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'])
];
}

Expand Down Expand Up @@ -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];
}

/**
Expand Down
6 changes: 6 additions & 0 deletions language/en/acp/info_acp_settings.php
Expand Up @@ -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 <samp>og:image</samp>. It will only be used if an image cannot be found within the current page. It must be relative to <samp>%s</samp>',

'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 <samp>0</samp> 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 (<samp>%s</samp>).',

Expand Down
48 changes: 48 additions & 0 deletions migrations/v12x/m00_configuration.php
@@ -0,0 +1,48 @@
<?php

/**
* SEO Metadata extension for phpBB.
* @author Alfredo Ramos <alfredo.ramos@yandex.com>
* @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', '']
]
];
}
}

0 comments on commit 2b74c79

Please sign in to comment.