Get (file) weight of resources

Hi,
How do I get the weight/size of an image resource in kb ?
I don’t see a method for that. Resizing pictures when they’re less than 100 ko makes no sense, I realized it could noticeably damage quality.
Also, I noticed
there we can’t change default imaging parameters in frontmatters, no

  hint = 'drawing'
  quality = 30
  resampleFilter = 'lanczos'

for a specific page. at least do brought no change in file sizes for me.
Oh sure I can add parameters, but something automatic would be hella neat.

Yours

1 Like
1 Like

Ah… I see, " Details of the FileInfo structure are available in the Go documentation."
The word “size” or “weight” not appearing, I didn’t register.
Adding some keywords in the page would help.

You should use Ressources

There is a .Name property with the file path.

That seems awfully long-winded, and I don’t use page bundle at all.
But I was confused, I thought I had to know the path and weight of processed resources. That would be even better, but is not necessary here,
{{if ge 150 000 (os.Stat .Destination).Size}}
will do fine.
However ideally, I would tell it to stop resizing when the processed image gets smaller than 100kb. An idea how to ?

my 2cents

You must get the source fileinfo a File variable. Use readDir or readFile functions.
The use os.stat for File.Path to get the size.

HTH

https://discourse.gohugo.io/search?q=file%20size

1 Like

I didn’t think of looking for “file size” in the forum.
But the solutions are bad… for something simple, and a need quite intuitive to come up with in term of optimization (getting the size of an object explicitly declared in the same document) since at the end of the day, we don’t really care about the dimensions of an image, only its size in kb/mb. I had it “working”, but it produced files bigger than the original, which makes no sense… Not easy to manipulate.
I’ll forget it for now, until a real solution is put in hugo’s design.

As a photographer I disagree, I find you are better defining pixel dimensions and quality most of the time and let the file size fall into place.
If you compress to a file size you are likely to over compress a detailed image.
If you are making an image bigger than it started then you have likely picked a quality setting higher than the original image.

1 Like

Ok then, I’ll follow your suggestion. Also simpler to I’m implement. I have other means in mind for optimization, namely, getting to know how many children there is, in a css class, to deduce the exact dimensions of an image relative to the page.
About the quality issue, i don’t know what to say !
All my pictures come from the web and have been converted to webp already, losslessly.
When I indicated q75, i thought that meant 75% quality of anything processed, and that is the behavior I want. But now I’m wondering if tempering with quality is even worth the hassle, with webp seemingly so efficient already :thinking:.

I managed to get all image file properties including size this way:

{{/* Get Featured Image Properties */}}
{{/* Retrieve the image resource */}}
{{ $featuredImage := .Page.Resources.GetMatch "*feature*" }}
{{/* Check if the image exists */}}
{{ if $featuredImage }}
    {{/* Image properties */}}
    <p>Width: {{ $featuredImage.Width }} pixels</p>
    <p>Height: {{ $featuredImage.Height }} pixels</p>
    <p>File Type: {{ $featuredImage.MediaType.Type }}</p>
{{/* Image size */}}
{{ $contentLengthBytes := len $featuredImage.Content }}
    <p>File Size Bytes: {{ $contentLengthBytes }} bytes</p>
{{/* Convert bytes to megabytes */}}
{{ $contentLengthMB := div $contentLengthBytes 1048576.0 }}
    <p>File Size MB: {{ printf "%.2f MB" $contentLengthMB }}</p>
{{ else }}
    <p>No featured image found.</p>
{{ end }}

When posting code, configuration, or data on this forum, please wrap the text within backticks or use the </> button in the menu.

```
my code
```
1 Like

apologies, corrected

1 Like

No problem.