Page 1 of 1

get a image from page Content

Posted: Wed Sep 09, 2020 9:56 am
by andrewvideouk
Its posbile to get any image url from content_en so I can use it in the html metadata tags.

I know you can use the image link from Edit Content Page - tab options. I know that the users well forget to add a image

I want to able for people to share the page on facebook etc.
I can workout to get Content and strip the tags and truncate for the description, get title and url etc.

has anyone done this or is there a plugin / module?

I hope you understand me, I am bad at writing.

Thank you

Code: Select all

<meta property="og:url"                content="http://www.nytimes.com/2015/02/19/arts/international/when-great-minds-dont-think-alike.html" />
<meta property="og:type"               content="article" />
<meta property="og:title"              content="When Great Minds Don’t Think Alike" />
<meta property="og:description"        content="How much does culture influence creative thinking?" />
<meta property="og:image"              content="http://static01.nyt.com/images/2015/02/19/arts/international/19iht-btnumbers19A/19iht-btnumbers19A-facebookJumbo-v2.jpg" />

Re: get a image from page Content

Posted: Wed Sep 09, 2020 5:29 pm
by velden
I don't have the answer but I think it's a bad idea to take a random image (if any) from the content. You have know no idea whether there will be an image, what size it has, what content it's about.

You could add a {content_image} content block to the page template. If an image is set you can give it the proper dimensions (eg using CGSmartImage} and use it for the graph data. If no image is set you can use a default image.

Re: get a image from page Content

Posted: Wed Sep 09, 2020 6:00 pm
by Rolf
I can vaguely remember that one of the support modules of Calguy has such a feature, CGExtentions, CGSimpleSmarty or CGContentUtils...
Worth a try to look there.

If you want to do it with the page/blog fields you can use this tutorial:
https://cmscanbesimple.org/blog/base-cm ... d-metatags

Re: get a image from page Content

Posted: Wed Sep 09, 2020 8:25 pm
by andrewvideouk
Thank you I well have a read. Yes good point size of the random image could break metadata.


Thank you

Re: get a image from page Content

Posted: Wed Sep 09, 2020 10:19 pm
by DIGI3
CGSmartImage has the option to extract all images from the content. You could use this as a fallback if no image is provided in the page image field.

Once you have the image, you can size it with CGSmartImage so it's the correct sizing for og:image, and/or you can get the dimensions with php's getimagesize function, using something like this (this assumes a content_image block named ogimage with urlonly=1, and has to hack the path a bit to make it work - you may have to adjust based on how you're supplying the path)

Code: Select all

  {if $ogimage}
    {$path={$ogimage|replace:"{root_url}/":""}}
    {$dimensions=getimagesize($path)}
    <meta property="og:image:width" content="{$dimensions[0]}" />
    <meta property="og:image:height" content="{$dimensions[1]}" />
    <meta property="og:image" content="{$ogimage|replace:' ':'%20'}" />
    <meta property="og:url" content="{$canonical|default:$content_obj->GetURL()}" />
  {/if}
You will need to allow permissive_smarty in your config. The above was based on the info in Rolf's fantastic blog - https://cmscanbesimple.org/blog/getting ... -in-smarty