Thumbnail generation

I guess hugo does not support automatic thumbnail generation at the time of rendering?

As I’m a photographer that would be a great feature for me.

Is there a way I can configure a shortcode/markdown to trigger a thumbnail generation script when rendering the site.

Not currently, no. But it is one of the more frequently discussed features on GitHub, so it will happen … eventually. If you know some Go, pull requests are always welcome!

Unfortunately I don’t know any Go otherwise I probably would have written something already.

I hope it gets implemented soon.

This sounds interesting, and would definitely be grouped into some other “pre-compilation” features (like compiling SASS).

Here’s how I would do it in PowerShell:

function New-Thumbnail {
    param (
        [string]$Path,
        [string]$ThumbnailPath,
        [int]$Width = 72,
        [int]$Height = 72
    )
    
    $full = $null
    $thumb = $null
    
    try {
        $full = [System.Drawing.Image]::FromFile($Path); 
        $thumb = $full.GetThumbnailImage($Width, $Height, $null, [intptr]::Zero); 
        $thumb.Save($ThumbnailPath); 
    } finally {
        if($full -ne $null) {
            $full.Dispose();
        }
        
        if($thumb -ne $null){
            $thumb.Dispose();
        }
    }
}

function hugo-build {
    # pre-compile things here
    
    # gen thumbnails
    gci .\static\images\full\*.jpg | %{
    
        $orig = $_
        $nameNoExt = [IO.Path]::GetFileNameWithoutExtension($orig.Name)
        $thumbnailPath = "{0}/{1}-thumb.jpg" -f $_.Parent.FullName, $nameNoExt
        
        New-Thumbnail -Path $_.FullName -ThumbnailPath $thumbnailPath        
    }
    
    # compile site with hugo
    hugo
}

If you find that the quality isn’t the best, here is an article (C# code) to improve the quality (would need to be converted to PowerShell).

2 Likes