Skip to content

Commit

Permalink
Added Structured Data support for Article Images
Browse files Browse the repository at this point in the history
For feature #902
  • Loading branch information
eSilverStrike committed Feb 19, 2019
1 parent e8bc3dd commit 4eb76c4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
30 changes: 29 additions & 1 deletion public_html/article.php
Expand Up @@ -528,7 +528,35 @@ function extractExternalLinks($text) {
$properties['keywords'] = $article->DisplayElements('meta_keywords');
$properties['commentCount'] = CMT_commentCount($article->getSid(), 'article');
$_STRUCT_DATA->add_type('article', $article->getSid(), $article->displayElements('structured_data_type'), $properties);
$_STRUCT_DATA->set_author_item('article', $article->getSid(), $article->DisplayElements('username'));
$_STRUCT_DATA->set_author_item('article', $article->getSid(), $article->DisplayElements('username'));
// Include any images attached to the article (taken in part from renderImageTags function in article class)
$result = DB_query("SELECT ai_filename,ai_img_num FROM {$_TABLES['article_images']} WHERE ai_sid = '{$article->getSid()}' ORDER BY ai_img_num");
$numRows = DB_numRows($result);

$stdImageLoc = true;
if (!strstr($_CONF['path_images'], $_CONF['path_html'])) {
$stdImageLoc = false;
}

for ($i = 1; $i <= $numRows; $i++) {
$A = DB_fetchArray($result);

$imgPath = '';

if ($stdImageLoc) {
$imgPath = substr($_CONF['path_images'], strlen($_CONF['path_html']));
$imgSrc = $_CONF['site_url'] . '/' . $imgPath . 'articles/' . $A['ai_filename'];
} else {
$imgSrc = $_CONF['site_url'] . '/getimage.php?mode=articles&amp;image=' . $A['ai_filename'];
}

$sizeAttributes = COM_getImgSizeAttributes($_CONF['path_images'] . 'articles/' . $A['ai_filename'], false);
if (is_array($sizeAttributes)) {
$_STRUCT_DATA->set_image_item('article', $article->getSid(), $imgSrc, $sizeAttributes['width'], $sizeAttributes['height']);
} else {
$_STRUCT_DATA->set_image_item('article', $article->getSid(), $imgSrc);
}
}

$display = COM_createHTMLDocument(
$display,
Expand Down
22 changes: 16 additions & 6 deletions public_html/lib-common.php
Expand Up @@ -6715,10 +6715,11 @@ function COM_convertDate2Timestamp($date, $time = '')
/**
* Get the HTML for an image with height & width
*
* @param string $file full path to the file
* @return string html that will be included in the img-tag
* @param string $file full path to the file
* @param boolean $html flag to return html source or array
* @return string if $html true then html that will be included in the img-tag. Else an array will be returned with the information
*/
function COM_getImgSizeAttributes($file)
function COM_getImgSizeAttributes($file, $html = true)
{
$sizeAttributes = '';

Expand All @@ -6743,15 +6744,24 @@ function COM_getImgSizeAttributes($file)
}

if (($width !== '?') && ($height !== '?')) {
$sizeAttributes = 'width="' . $width . '" height="' . $height . '" ';
if ($html) {
$sizeAttributes = 'width="' . $width . '" height="' . $height . '" ';
} else {
$sizeAttributes['width'] = $width;
$sizeAttributes['height'] = $height;
}
}
}
} else {
// Other file type
$dimensions = getimagesize($file);
if (!empty($dimensions[0]) && !empty($dimensions[1])) {
$sizeAttributes = 'width="' . $dimensions[0]
. '" height="' . $dimensions[1] . '" ';
if ($html) {
$sizeAttributes = 'width="' . $dimensions[0] . '" height="' . $dimensions[1] . '" ';
} else {
$sizeAttributes['width'] = $dimensions[0];
$sizeAttributes['height'] = $dimensions[1];
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion system/classes/structureddata.class.php
Expand Up @@ -216,7 +216,7 @@ public function set_author_item($type, $id, $name)
* @param string $type Plugin of the content used to create the structured data
* @param string $id Id of content
*/
public function set_image_item($type, $id, $url, $width, $height)
public function set_image_item($type, $id, $url, $width = '', $height = '')
{

$sd_name = $this->create_name($type, $id);
Expand Down

0 comments on commit 4eb76c4

Please sign in to comment.