get a image from page Content

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
andrewvideouk
Forum Members
Forum Members
Posts: 156
Joined: Thu Aug 20, 2020 10:49 pm

get a image from page Content

Post 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" />
Attachments
10956906_396737803821010_168799778_n.png
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: get a image from page Content

Post 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.
Last edited by velden on Wed Sep 09, 2020 8:36 pm, edited 1 time in total.
Reason: Spelling error
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Contact:

Re: get a image from page Content

Post 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
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
andrewvideouk
Forum Members
Forum Members
Posts: 156
Joined: Thu Aug 20, 2020 10:49 pm

Re: get a image from page Content

Post by andrewvideouk »

Thank you I well have a read. Yes good point size of the random image could break metadata.


Thank you
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1800
Joined: Wed Feb 25, 2009 4:25 am

Re: get a image from page Content

Post 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
Not getting the answer you need? CMSMS support options
Post Reply

Return to “CMSMS Core”