Browse Source

image-processing script at MVP

master
Devin Dooley 1 year ago
parent
commit
40fe45b739
7 changed files with 222 additions and 5 deletions
  1. +11
    -1
      README.md
  2. +8
    -0
      build/image-processing/go.mod
  3. +49
    -0
      build/image-processing/go.sum
  4. +150
    -0
      build/image-processing/image-processing.go
  5. +1
    -1
      build/photo_rename.sh
  6. +2
    -2
      content/blog/reboot-failures.md
  7. +1
    -1
      layouts/gallery/list.html

+ 11
- 1
README.md View File

@ -25,7 +25,8 @@ file with the type of `gallery` defined in the front matter.
Gallery pages will generate and display any `.jpg` files
in the directory with scaled down thumbnails.
When a thumbnail is clicked, it will open a lightbox modal
containing the original image. If javascript is disabled,
containing a compressed version of the original image.
If javascript is disabled,
the thumbnail will link to the image directly.
The `.jpg` files are hosted elsewhere to keep
@ -33,3 +34,12 @@ the repository lightweight and flexible. It is assumed
that you (really, me) will pull the images into the correct
directory using `scp` or `rsync` or some other
remote file copying tool.
Gallery pages are not expected to load correctly
from a Hugo development server, as the thumbnails
and compressed images are generated through a custom
script that runs after Hugo's build process.
To view a gallery page during local development,
run `make build` and load the page directly from
the output directory in your browser.

+ 8
- 0
build/image-processing/go.mod View File

@ -0,0 +1,8 @@
module image-processing
go 1.14
require (
github.com/go-delve/delve v1.4.0 // indirect
github.com/h2non/bimg v1.0.19
)

+ 49
- 0
build/image-processing/go.sum View File

@ -0,0 +1,49 @@
github.com/cosiner/argv v0.0.0-20170225145430-13bacc38a0a5/go.mod h1:p/NrK5tF6ICIly4qwEDsf6VDirFiWWz0FenfYBwJaKQ=
github.com/cpuguy83/go-md2man v1.0.8/go.mod h1:N6JayAiVKtlHSnuTCeuLSQVs75hb8q+dYQLjr7cDsKY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-delve/delve v1.4.0 h1:O+1dw1XBZXqhC6fIPQwGxLlbd2wDRau7NxNhVpw02ag=
github.com/go-delve/delve v1.4.0/go.mod h1:gQM0ReOJLNAvPuKAXfjHngtE93C2yc/ekTbo7YbAHSo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/h2non/bimg v1.0.19 h1:lrKA2oi4DttnscGeb/oRiBIcRiAX33mmp3yQX8JzGIc=
github.com/h2non/bimg v1.0.19/go.mod h1:R3+UiYwkK4rQl6KVFTOFJHitgLbZXBZNFh2cv3AEbp8=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.0.0-20170327083344-ded68f7a9561/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/peterh/liner v0.0.0-20170317030525-88609521dc4b/go.mod h1:xIteQHvHuaLYG9IFj6mSxM0fCKrs34IrEQUhOYuGPHc=
github.com/pkg/profile v0.0.0-20170413231811-06b906832ed0/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v0.0.0-20180428102519-11635eb403ff/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/sirupsen/logrus v0.0.0-20180523074243-ea8897e79973/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
github.com/spf13/cobra v0.0.0-20170417170307-b6cb39589372/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v0.0.0-20170417173400-9e4c21054fa1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
go.starlark.net v0.0.0-20190702223751-32f345186213/go.mod h1:c1/X6cHgvdXj6pUlmWKMkuqRnW4K8x2vwt6JAaaircg=
golang.org/x/arch v0.0.0-20190927153633-4e8777c89be4/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191127201027-ecd32218bd7f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

+ 150
- 0
build/image-processing/image-processing.go View File

@ -0,0 +1,150 @@
// Processes images to be served by the site
// Every jpeg found in the given directory
// will get a thumbnail and lower quality
// version generated
// Only jpegs are supported for now as that
// is all that is needed
// The bimg package depends on libvips being
// installed on the calling machine
package main
import (
"flag"
"github.com/h2non/bimg"
"log"
"os"
"path/filepath"
"strings"
)
// Images are compressed by first doubling the size, then
// reducing the jpeg quality to 30
// This lowers the size and still allows for good rendering
// on high dpi screens of any size
func generateCompressed(img *bimg.Image, outputPath string) error {
originalSize, err := img.Size()
if err != nil {
return err
}
originalWidth := originalSize.Width
originalHeight := originalSize.Height
options := bimg.Options{
Width: originalWidth * 2,
Height: originalHeight * 2,
Quality: 30,
}
compressed, err := img.Process(options)
if err != nil {
return err
}
bimg.Write(outputPath, compressed)
return nil
}
func generateThumbnail(img *bimg.Image, outputPath string) error {
thumbnail, err := img.Thumbnail(200)
if err != nil {
return err
}
bimg.Write(outputPath, thumbnail)
return nil
}
func getImage(path string) (*bimg.Image, error) {
buffer, err := bimg.Read(path)
if err != nil {
return &bimg.Image{}, err
}
img := bimg.NewImage(buffer)
if err != nil {
return &bimg.Image{}, err
}
return img, nil
}
func getAllPaths(photosDir string) ([][]string, error) {
paths := [][]string{}
err := filepath.Walk(photosDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Although not a great way to handle things, we skip any files without a .jpg extension
nameSplit := strings.Split(info.Name(), ".")
if nameSplit[len(nameSplit)-1] != "jpg" {
return nil
}
pathSplit := strings.Split(path, "/")
var workingPath string
for i, dir := range pathSplit {
if i == len(pathSplit)-1 {
continue
}
workingPath = workingPath + dir + "/"
}
thumbnailPath := workingPath + nameSplit[0] + "_thumbnail.jpg"
compressedPath := workingPath + nameSplit[0] + "_compressed.jpg"
paths = append(paths, []string{path, thumbnailPath, compressedPath})
return nil
})
if err != nil {
return [][]string{}, err
}
return paths, nil
}
func processImages(photosDir string) error {
paths, err := getAllPaths(photosDir)
if err != nil {
return err
}
for _, path := range paths {
img, err := getImage(path[0])
if err != nil {
return err
}
err = generateThumbnail(img, path[1])
if err != nil {
return err
}
err = generateCompressed(img, path[2])
if err != nil {
return err
}
}
return nil
}
func main() {
var dir string
// Script will search recursively from the directory given
flag.StringVar(&dir, "dir", "./content/photos", "directory where photos are kept")
flag.Parse()
err := processImages(dir)
if err != nil {
log.Fatal(err)
}
}

+ 1
- 1
build/photo_rename.sh View File

@ -1,5 +1,5 @@
#!/bin/sh
for file in public/photos/*/*resize_q30_box.jpg; do
for file in public/photos/*/*resize_q25_box.jpg; do
if [ -e "$file" ]; then
newname=`echo "$file" | sed -E 's/(.*)\/(.*)\/(.*)\/(.*)_(.*)_(.*)__(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_box.jpg/\1\/\2\/\3\/\4_\5_\6__\7_\8_lightbox.jpg/'`
mv "$file" "$newname"


+ 2
- 2
content/blog/reboot-failures.md View File

@ -60,5 +60,5 @@ UUID=$UUID1 / ext4 rw,relatime 0 1
UUID=$UUID2 /media ntfs-3g defaults 0 0
{{< / highlight >}}
After another reboot, everything came up without an issue, and my certificates finally renewed on my
next attempt :)
After another reboot, everything came up without an issue.

+ 1
- 1
layouts/gallery/list.html View File

@ -12,7 +12,7 @@
<!--
{{ with .Resources.ByType "image" }}
{{ range $index, $image := . }}
{{ $image.Resize "5000x q30" }}
{{ $image.Resize "5000x q25" }}
{{ end }}
{{ end }}
-->


Loading…
Cancel
Save