How to use Image Processing from Hugo's example

I’m creating styleguide.md file and trying to use Hugo’s image short code from this link but it’s not clear for me from the example on the same page how to use it?

When I add this code in styleguide.md file:

{{< imgproc sunset Resize “300x” />}}

I get an error. Should I define image title and src e.g. “sunset” somewhere?

P.S. The shortcode I’m using is:

{{ $original := .Page.Resources.GetMatch (printf "*%s*" (.Get 0)) }}
{{ $command := .Get 1 }}
{{ $options := .Get 2 }}
{{ if eq $command "Fit"}}
{{ .Scratch.Set "image" ($original.Fit $options) }}
{{ else if eq $command "Resize"}}
{{ .Scratch.Set "image" ($original.Resize $options) }}
{{ else if eq $command "Fill"}}
{{ .Scratch.Set "image" ($original.Fill $options) }}
{{ else }}
{{ errorf "Invalid image processing command: Must be one of Fit, Fill or Resize."}}
{{ end }}
{{ $image := .Scratch.Get "image" }}
<figure style="padding: 0.25rem; margin: 2rem 0; background-color: #cccc">
	<img style="max-width: 100%; width: auto; height: auto;" src="{{ $image.RelPermalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}">
	<figcaption>
	<small>
	{{ with .Inner }}
	{{ . }}
	{{ else }}
	.{{ $command }} "{{ $options }}"
	{{ end }}
	</small>
	</figcaption>
</figure>  

And what error do you get?

Hi @chrillek

If I add image source like this:

{{< img "https://images.unsplash.com/photo-1629666451128-f936a2dca0f7" Resize "300x" />}}

I get this error:

executing “shortcodes/img.html” at <$original.Resize>: nil pointer evaluating resource.Resource.Resize

That’s because $original is not an image resource. Check the file name and everything else. The problem here is not with Hugo.
You could add something like
{{warnf "%#v" (.Get 0)}}
at the beginning of your shortcode to see what Get 0 really returns.

I still get same error.

Of course. I have no idea what you’re passing in as parameters. And I didn’t even pretend that the error would go away simply by printing out a warning message. I just tried to help you with figuring out what might go wrong.

But I’ll stop here. Please go and read the documentation. Try to understand what Resources are (there’s a whole chapter on those) and how you pass parameters to a shortcode (hint: You can’t pass something like Resize without including it in quotes, because the bare word is a command).

Also, if you ask for help, try at least to be consistent in your problem description. In your first post, you used {{<imgproc sunset Resize “300x”>}} (which can’t work because you do not quote your parameters). In your second post, your shortcode has another name and you’re quoting the image (which does not help because Hugo needs a resource not a URL).

Thank you @chrillek,

I’m sorry because of the inconsistency. In the meanwhile I was trying to resolve the issue and changed the imgproc into img, but foresaw to change it after the pasting and it was my bad I haven’t checked it once again before replying.

I’m new to Hugo and although I was going through it’s documentation (and watched YT videos about it) I’m having difficulties understanding it, especially it’s examples and explanations.

@Mizuchi

structure
content/
├── test/
│   └── test-1/
│       ├── index.md
│       └── sunset.jpg
└── _index.md

content/test/test-1/index.md
{{< img sunset Resize 100x >}}

layouts/shortcodes/img.html

Copied from here

{{ $original := .Page.Resources.GetMatch (printf "*%s*" (.Get 0)) }}
{{ $command := .Get 1 }}
{{ $options := .Get 2 }}
{{ if eq $command "Fit"}}
{{ .Scratch.Set "image" ($original.Fit $options) }}
{{ else if eq $command "Resize"}}
{{ .Scratch.Set "image" ($original.Resize $options) }}
{{ else if eq $command "Fill"}}
{{ .Scratch.Set "image" ($original.Fill $options) }}
{{ else }}
{{ errorf "Invalid image processing command: Must be one of Fit, Fill or Resize."}}
{{ end }}
{{ $image := .Scratch.Get "image" }}
<figure style="padding: 0.25rem; margin: 2rem 0; background-color: #cccc">
	<img style="max-width: 100%; width: auto; height: auto;" src="{{ $image.RelPermalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}">
	<figcaption>
	<small>
	{{ with .Inner }}
	{{ . }}
	{{ else }}
	.{{ $command }} "{{ $options }}"
	{{ end }}
	</small>
	</figcaption>
</figure>

@chrillek There is no need to encapsulate the parameters in quotation marks.

Just an fyi @Mizuchi but unless your image caption is a legal disclaimer, or a copyright notice, then it is probably not appropriate to use <small>.

1 Like

Thank you @jmooring, I structured folders/files according to your example and the image is rendering now! :slight_smile:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.