Giter Site home page Giter Site logo

Comments (6)

panr avatar panr commented on August 24, 2024

Hej @vinamelody, just added support for this. In your .md file just use imgproc shortcode, like this:

{{< imgproc [name_of_image] [options] />}} -> {{< imgproc hello "Fit 500x200" />}}

or with caption:

{{< imgproc hello "Fit 500x200" >}}
Hello friend
{{< />}}

For more info, please look at image processing docs (the same docs you mentioned above)

from hugo-theme-hello-friend.

vinamelody avatar vinamelody commented on August 24, 2024

Thank you :)

from hugo-theme-hello-friend.

vinamelody avatar vinamelody commented on August 24, 2024

Hi, when I tried using it for example

  • {{< imgproc xinhua-fried-beehoon-dish.jpg "Fit 500" >}}, the error: unrecognized character in shortcode action: U+002E '.'. Note: Parameters with non-alphanumeric args must be quoted
  • {{< imgproc "xinhua-fried-beehoon-dish.jpg" "Fit 500" >}}, the error: Invalid image processing command: Must be one of Fit, Fill or Resize.

By the way, I read an article https://laurakalbag.com/processing-responsive-images-with-hugo/ and tried adding a custom shortcode from the article and it works!

{{/* get file that matches the filename as specified as src="" in shortcode */}}
{{ $src := .Page.Resources.GetMatch (printf "*%s*" (.Get "src")) }}

{{/* set image sizes, these are hardcoded for now, x dictates that images are resized to this width */}}


{{ $tinyw := default "500x q80" }}
{{ $smallw := default "800x q80" }}
{{ $mediumw := default "1200x q80" }}
{{ $largew := default "1500x q80" }}

{{/* resize the src image to the given sizes */}}

{{ .Scratch.Set "tiny" ($src.Resize $tinyw) }}
{{ .Scratch.Set "small" ($src.Resize $smallw) }}
{{ .Scratch.Set "medium" ($src.Resize $mediumw) }}
{{ .Scratch.Set "large" ($src.Resize $largew) }}

{{/* add the processed images to the scratch */}}

{{ $tiny := .Scratch.Get "tiny" }}
{{ $small := .Scratch.Get "small" }}
{{ $medium := .Scratch.Get "medium" }}
{{ $large := .Scratch.Get "large" }}

{{/* only use images smaller than or equal to the src (original) image size, as Hugo will upscale small images */}}
{{/* set the sizes attribute to (min-width: 35em) 1200px, 100vw unless overridden in shortcode */}}

<img 
  {{ with .Get "sizes" }}sizes='{{.}}'{{ else }}sizes="(min-width: 35em) 1200px, 100vw"{{ end }}
  srcset='
  {{ if ge $src.Width "500" }}
    {{ with $tiny.RelPermalink }}{{.}} 500w{{ end }}
  {{ end }}
  {{ if ge $src.Width "800" }}
    {{ with $small.RelPermalink }}, {{.}} 800w{{ end }}
  {{ end }}
  {{ if ge $src.Width "1200" }}
    {{ with $medium.RelPermalink }}, {{.}} 1200w{{ end }}
  {{ end }}
  {{ if ge $src.Width "1500" }}
    {{ with $large.RelPermalink }}, {{.}} 1500w {{ end }}
  {{ end }}'
  {{ if .Get $medium }}
    src="{{ $medium.RelPermalink | safeURL }}" 
  {{ else }}
    src="{{ $src.RelPermalink | safeURL }}" 
  {{ end }}
  {{ with .Get "alt" }}alt="{{ . | plainify }}"{{ end }}
  {{ with .Get "style" }} style="{{ . | safeCSS }}"{{ end }}>

from hugo-theme-hello-friend.

vinamelody avatar vinamelody commented on August 24, 2024

The downside of that, Hugo seems to produce multiple image files upon compilation

from hugo-theme-hello-friend.

panr avatar panr commented on August 24, 2024

@vinamelody please try to use filename without an extension. Error says that the issue is with . 🤔

from hugo-theme-hello-friend.

vinamelody avatar vinamelody commented on August 24, 2024

hmm tried that, and tried wrapping it with double-quote too, it doesn't work.

from hugo-theme-hello-friend.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.