import (
"errors"
"fmt"
+ "image"
"image/color"
+ "image/draw"
+ "image/jpeg"
"io"
"os"
"strconv"
"strings"
+ "sync"
- "github.com/mitchellh/mapstructure"
-
+ "github.com/disintegration/imaging"
"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/helpers"
+ "github.com/mitchellh/mapstructure"
- // Importing image codecs for image.DecodeConfig
- "image"
- "image/draw"
+ // Blind import for image.Decode
_ "image/gif"
- "image/jpeg"
_ "image/png"
- "github.com/disintegration/imaging"
- // Import webp codec
- "sync"
-
+ // Blind import for image.Decode
_ "golang.org/x/image/webp"
)
strings.ToLower("Cosine"): imaging.Cosine,
}
+// Image represents an image resource.
type Image struct {
config image.Config
configInit sync.Once
*genericResource
}
+// Width returns i's width.
func (i *Image) Width() int {
i.initConfig()
return i.config.Width
}
+// Height returns i's height.
func (i *Image) Height() int {
i.initConfig()
return i.config.Height
}
-// Implement the Cloner interface.
+// WithNewBase implements the Cloner interface.
func (i *Image) WithNewBase(base string) Resource {
return &Image{
imaging: i.imaging,
}
if rgba != nil {
return jpeg.Encode(w, rgba, &jpeg.Options{Quality: quality})
- } else {
- return jpeg.Encode(w, img, &jpeg.Options{Quality: quality})
}
+ return jpeg.Encode(w, img, &jpeg.Options{Quality: quality})
default:
return imaging.Encode(w, img, i.format)
}
_ permalinker = (*genericResource)(nil)
)
-const DefaultResourceType = "unknown"
-
var noData = make(map[string]interface{})
// Source is an internal template and not meant for use in the templates. It
Params() map[string]interface{}
}
+// ResourcesLanguageMerger describes an interface for merging resources from a
+// different language.
type ResourcesLanguageMerger interface {
MergeByLanguage(other Resources) Resources
// Needed for integration with the tpl package.
Content() (interface{}, error)
}
-// OpenReadSeekeCloser allows setting some other way (than reading from a filesystem)
+// OpenReadSeekCloser allows setting some other way (than reading from a filesystem)
// to open or create a ReadSeekCloser.
type OpenReadSeekCloser func() (hugio.ReadSeekCloser, error)
// I.e. both pages and images etc.
type Resources []Resource
+// ByType returns resources of a given resource type (ie. "image").
func (r Resources) ByType(tp string) Resources {
var filtered Resources
}
// MergeByLanguage adds missing translations in r1 from r2.
-func (r1 Resources) MergeByLanguage(r2 Resources) Resources {
- result := append(Resources(nil), r1...)
+func (r Resources) MergeByLanguage(r2 Resources) Resources {
+ result := append(Resources(nil), r...)
m := make(map[string]bool)
- for _, r := range r1 {
- if translated, ok := r.(translatedResource); ok {
+ for _, rr := range r {
+ if translated, ok := rr.(translatedResource); ok {
m[translated.TranslationKey()] = true
}
}
- for _, r := range r2 {
- if translated, ok := r.(translatedResource); ok {
+ for _, rr := range r2 {
+ if translated, ok := rr.(translatedResource); ok {
if _, found := m[translated.TranslationKey()]; !found {
- result = append(result, r)
+ result = append(result, rr)
}
}
}
// MergeByLanguageInterface is the generic version of MergeByLanguage. It
// is here just so it can be called from the tpl package.
-func (r1 Resources) MergeByLanguageInterface(in interface{}) (interface{}, error) {
+func (r Resources) MergeByLanguageInterface(in interface{}) (interface{}, error) {
r2, ok := in.(Resources)
if !ok {
return nil, fmt.Errorf("%T cannot be merged by language", in)
}
- return r1.MergeByLanguage(r2), nil
+ return r.MergeByLanguage(r2), nil
}
type Spec struct {