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
tpl: Adds imageConfig function which calls image.DecodeConfig and returns the height, width and color mode of the image. #2677
Conversation
It is a good idea, but we need to make some changes. Here is how I see it: {{ $src := .Get "src" }}
{{ $config := imageConfig (printf "/static/%s" $src) }}
<amp-img src="{{$src}}"
height="{{$config.Height}}"
width="{{$config.Width}}"
layout="responsive">
</amp-img>
There are some challenges with the cache on reloads etc. that we will have to revisit later --we will probably reuse this in some way if we decide to do this on some scale. |
I'm not sure if caching is really necessary. At least for my use cases, images will typically only be embedded once. I guess if the image was being embedded in a shared template/header it'll be used a lot. As for the readFile thing, yeah, it probably would be better to just take a direct path. Just took another look at it, and it's probably way better to use As an example, for PNG all the width/height/color type information is contained in the first 33 bytes of the image. https://en.wikipedia.org/wiki/Portable_Network_Graphics#Technical_details If we're only reading a small section of the file, I'm not sure if there's really going to be any large benefit to caching those results. I can make the change and benchmark it if you'd like. What are your thoughts on renaming this to be |
I was just pointing out what is needed to get this merged into Hugo's master. You are more than welcome to keep this in your personal fork, but then please close this PR. This would be a good addition to Hugo, but not in its current state. |
Just to add this this: Even if reading the first 33 bytes of a file is enough, if I use that one picture on 5000 pages, it would be pretty wasteful to open that same file 5000 times to read of that same 33 bytes. |
I fully understand that you have the final say. It's no problem to add caching. It seemed to add a layer of complexity that will rarely be needed, but does have valid reasons. I just wanted to discuss it a bit before making the change. |
…urns the height, width and color mode of the image. This allows for more advanced image shortcodes and templates such as those required by AMP. layouts/shortcodes/amp-img.html ``` {{ $src := .Get "src" }} {{ $config := imageConfig (printf "/static/%s" $src) }} <amp-img src="{{$src}}" height="{{$config.Height}}" width="{{$config.Width}}" layout="responsive"> </amp-img> ```
I've made the changes requested. There's now an |
Merged, and thanks, this is very useful -- now we can create some proper AMP templates! Solid code, but please note I changed your commit message ... we are strangely pedantic about that. It is in the contribution guidelines. |
Awesome! Any interest in having something like the amp-img shortcode built in? I briefly glanced over that section earlier. Guess I didn't look closely enough. |
amp-img shortcode would be good. If you could create a tracking issue for it (don't need any text other than the headline) so the implementation could close it, it makes it a little easier to write release notes etc. Also see this discussion https://discuss.gohugo.io/t/custom-output-content-types/4584/1 |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This allows for more advanced image shortcodes and templates such as those required by AMP.
layouts/shortcodes/amp-img.html