Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract and use height and width of SVG images #3

Open
bowman2001 opened this issue Nov 11, 2023 · 1 comment
Open

Extract and use height and width of SVG images #3

bowman2001 opened this issue Nov 11, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@bowman2001
Copy link
Owner

  1. Extract the values. There are two general ways to parse the SVG content as far as I can see:

    1. Use string functions with matching capabilities (findRE or maybe others) to isolate the width and height attributes or the viewBox.
    2. Use transform.Unmarshal to get the full data structure of the SVG.
  2. We need to collect and maybe calculate the values (viewBox) for the width and height.

  3. Add them to the image tag.

@bowman2001 bowman2001 added the enhancement New feature or request label Nov 11, 2023
@bowman2001 bowman2001 self-assigned this Nov 11, 2023
@richtong
Copy link

If you need help on this, I wrote stuff that does this. Happy to help, but do not know if this is stale, here is the fragment in https://richtong/hugo-universal-theme


  {{ $svg := readFile $image_local | transform.Unmarshal }}
  <meta property="og:image:width" content="{{ index $svg "-width" }}">
  <meta property="og:image:height" content="{{ index $svg "-height" }}">

This adds it to the Facebook OpenGraph header, but the main ideas are to use readFile and then unmarshal to get the SVG's XML into a Go Map and the use the hugo index. function to find it. Other trick is that parameters and a dash - in them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants