2019-08-26 13:12:41 -04:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2017-03-13 18:55:02 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-11-29 22:32:53 -05:00
|
|
|
// Package images provides template functions for manipulating images.
|
2017-03-13 18:55:02 -04:00
|
|
|
package images
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"sync"
|
|
|
|
|
2022-05-02 10:07:52 -04:00
|
|
|
"errors"
|
2019-08-26 13:12:41 -04:00
|
|
|
|
2023-07-08 10:29:47 -04:00
|
|
|
"github.com/bep/overlayfs"
|
2019-08-26 13:12:41 -04:00
|
|
|
"github.com/gohugoio/hugo/resources/images"
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
// Importing image codecs for image.DecodeConfig
|
|
|
|
_ "image/gif"
|
|
|
|
_ "image/jpeg"
|
|
|
|
_ "image/png"
|
|
|
|
|
2017-09-25 22:25:33 -04:00
|
|
|
// Import webp codec
|
2017-06-28 15:45:48 -04:00
|
|
|
_ "golang.org/x/image/webp"
|
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
2023-07-08 10:29:47 -04:00
|
|
|
"github.com/spf13/afero"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/cast"
|
2017-03-13 18:55:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a new instance of the images-namespaced template functions.
|
2023-07-08 10:29:47 -04:00
|
|
|
func New(d *deps.Deps) *Namespace {
|
|
|
|
var readFileFs afero.Fs
|
|
|
|
|
|
|
|
// The docshelper script does not have or need all the dependencies set up.
|
|
|
|
if d.PathSpec != nil {
|
|
|
|
readFileFs = overlayfs.New(overlayfs.Options{
|
|
|
|
Fss: []afero.Fs{
|
|
|
|
d.PathSpec.BaseFs.Work,
|
|
|
|
d.PathSpec.BaseFs.Content.Fs,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
return &Namespace{
|
2023-07-08 10:29:47 -04:00
|
|
|
readFileFs: readFileFs,
|
|
|
|
Filters: &images.Filters{},
|
|
|
|
cache: map[string]image.Config{},
|
|
|
|
deps: d,
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Namespace provides template functions for the "images" namespace.
|
|
|
|
type Namespace struct {
|
2019-08-26 13:12:41 -04:00
|
|
|
*images.Filters
|
2023-07-08 10:29:47 -04:00
|
|
|
readFileFs afero.Fs
|
|
|
|
cacheMu sync.RWMutex
|
|
|
|
cache map[string]image.Config
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
deps *deps.Deps
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config returns the image.Config for the specified path relative to the
|
|
|
|
// working directory.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Config(path any) (image.Config, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
filename, err := cast.ToStringE(path)
|
|
|
|
if err != nil {
|
|
|
|
return image.Config{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if filename == "" {
|
|
|
|
return image.Config{}, errors.New("config needs a filename")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check cache for image config.
|
2017-05-02 03:17:14 -04:00
|
|
|
ns.cacheMu.RLock()
|
2017-03-13 18:55:02 -04:00
|
|
|
config, ok := ns.cache[filename]
|
2017-05-02 03:17:14 -04:00
|
|
|
ns.cacheMu.RUnlock()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
if ok {
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2023-07-08 10:29:47 -04:00
|
|
|
f, err := ns.readFileFs.Open(filename)
|
2017-03-13 18:55:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return image.Config{}, err
|
|
|
|
}
|
2017-11-25 12:37:48 -05:00
|
|
|
defer f.Close()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
config, _, err = image.DecodeConfig(f)
|
2017-05-18 16:23:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
2017-03-13 18:55:02 -04:00
|
|
|
|
2017-05-02 03:17:14 -04:00
|
|
|
ns.cacheMu.Lock()
|
2017-03-13 18:55:02 -04:00
|
|
|
ns.cache[filename] = config
|
2017-05-02 03:17:14 -04:00
|
|
|
ns.cacheMu.Unlock()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
2017-05-18 16:23:29 -04:00
|
|
|
return config, nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
2019-08-26 13:12:41 -04:00
|
|
|
|
2022-12-30 03:20:58 -05:00
|
|
|
// Filter applies the given filters to the image given as the last element in args.
|
2022-04-21 04:59:13 -04:00
|
|
|
func (ns *Namespace) Filter(args ...any) (images.ImageResource, error) {
|
2019-08-26 13:12:41 -04:00
|
|
|
if len(args) < 2 {
|
|
|
|
return nil, errors.New("must provide an image and one or more filters")
|
|
|
|
}
|
|
|
|
|
2022-04-21 04:59:13 -04:00
|
|
|
img := args[len(args)-1].(images.ImageResource)
|
2019-08-26 13:12:41 -04:00
|
|
|
filtersv := args[:len(args)-1]
|
|
|
|
|
2019-09-04 08:07:10 -04:00
|
|
|
return img.Filter(filtersv...)
|
2019-08-26 13:12:41 -04:00
|
|
|
}
|