Meta data images for template

Hello !
Could anyone can help me with my code ? I want to specify different og:image for pages from template article than other pages.

This is my code :

  <!-- Social -->
  <meta property="og:title" content="<?= $site->title()->html() ?> | <?= $page->title()->html() ?>">
  <meta property="og:site_name" content="<?= $site->title()->html() ?>" />
  <meta property="og:description" content="<?= page('metas')->descr()->html() ?>" />
  <meta property="og:url" content="<?= $page->url() ?>" />
  <?php /* <meta property="og:locale" content="<?= $language->locale() ?>" />*/?>
  <?php if($page->template()!="article"): ?>
  <meta property="og:image"            content="assets/images/imf-share-<?= kirby()->language() ? kirby()->language()->code() : 'fr' ?>.png" />
  <meta property="og:image:type"       content="image/png">
  <meta property="og:image:width"      content="1200">
  <meta property="og:image:height"     content="630">
  <?php endif ?>
  <?php if($page->template()=="article"): ?>
    <?php $cover = $page->cover()->thumb(['width' => 1200, 'height' => 630, 'crop' => true])->url() ?>
    <meta property="og:image"            content="<?= $cover ?>" />
    <meta property="og:image:type"       content="image/jpg">
    <meta property="og:image:width"      content="1200">
    <meta property="og:image:height"     content="630">
  <?php endif ?>

When I try to post the link of an article on social media, the image is displayed in the preview but it does not work when I post it. Any idea ?
Thank you !

I don’t understand this sentence. What exactly does not work? Can you please try and rephrase?

Hello Texnine

I mean the image is displayed when I paste the link of an article in a social media (in this case Linkedin, but obviously I guess it’s the same case everywhere) only in the editor (before published the post), but once published the image is no longer displayed there is only a gray square. And og:title, og:site_name, og:description and og:url is working.

Did you try different platforms to rule a problem with LinkedIn?
Try here please: https://www.opengraph.xyz

Apart from that, your code is a bit repetitive. My suggestion:

<?php 
$ogImage = ($page->template->name() === 'article' && ($image = $page->cover())) ? 
    $image->thumb(['width' => 1200, 'height' => 630, 'crop' => true]) :
    asset('assets/images/imf-share-' . kirby()->language()->code() . '.png');
if ($ogImage): ?>

  <meta property="og:image"            content="<?= $ogImage->url() ?>" />
  <meta property="og:image:type"       content="<?= $ogImage->mime() ?>">
  <meta property="og:image:width"      content="<?= $ogImage->width() ?>">
  <meta property="og:image:height"     content="<?= $ogImage->height() ?>">
<?php endif ?>  

But one question: Is cover just a field or is that a model method that returns an image?