Skip to content

Commit

Permalink
improve open graph image handling
Browse files Browse the repository at this point in the history
  • Loading branch information
atas committed Oct 14, 2023
1 parent fc6af45 commit 6c76644
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"linkedin_url": "https://www.linkedin.com/in/atasasmaz",
"twitter_url": "https://twitter.com/AtaSasmaz",
"github_url": "https://github.com/atas",
"location": "City, Country"
"location": "City, Country",
"default_opengraph_image": "assets/images/site-icon-big.jpg"
}
19 changes: 16 additions & 3 deletions layout/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
$title = !isset($page_meta->title) || strlen($page_meta->title) == 0 ? $config->full_title : "$page_meta->title$config->appended_title";
$desc = !isset($page_meta->desc) || strlen($page_meta->desc) == 0 ? $config->site_desc : $page_meta->desc;

if (!preg_match('/^(https?:\/\/|\/)/', $page_meta->og_image)) {
list($og_image_width, $og_image_height) = getimagesize($page_meta->og_image);
$og_image = getCurrentHostnameWithProtocol() . "/" . $page_meta->og_image;
}
else if (preg_match('/^https?:\/\//', $page_meta->og_image)) {
$og_image = $page_meta->og_image;
}

?>
<!-- Built with Ata's SSG https://www.github.com/atas/ssg -->
<!DOCTYPE html>
Expand All @@ -25,9 +33,14 @@
<meta property="og:type" content="<?= $page_meta->type ?>"/>
<meta property="og:title" content="<?= $title ?>"/>
<meta property="og:description" content="<?= $desc ?>"/>
<meta property="og:image" content="<?= getCurrentHostnameWithProtocol() ?>/assets/images/site-icon-big.jpg"/>
<meta property="og:image:width" content="685"/>
<meta property="og:image:height" content="685"/>
<?php
if (isset($og_image))
echo "<meta property=\"og:image\" content=\"$og_image\"/>\n";
if (isset($og_image_width))
echo "<meta property=\"og:image:width\" content=\"$og_image_width\"/>\n";
if (isset($og_image_height))
echo "<meta property=\"og:image:height\" content=\"$og_image_height\"/>\n";
?>
<title><?= $title ?></title>
<?php
if (isBuildRunning()) {
Expand Down
1 change: 1 addition & 0 deletions system/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class PageMeta
public string $type = "website";
public string $desc = "";
public string $selectedTab = "";
public string $og_image;
}

class ConvertedMarkdown {
Expand Down
1 change: 1 addition & 0 deletions system/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

$page_meta = new PageMeta();
$page_meta->desc = $config->site_desc;
$page_meta->og_image = $config->default_opengraph_image;

/**
* Get a markdown file by its path, by converting that to html
Expand Down

0 comments on commit 6c76644

Please sign in to comment.