aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/helpers/thumbnails
blob: ec834fe842b5ba02d85410d70ea16b8d6b318833 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh -eu

timestamp='.update-thumbnails'
directory='images'
jpegoptim='jpegoptim --strip-all --verbose'
jpegoptim_images="$jpegoptim --max 80"
jpegoptim_thumbnails="$jpegoptim --max 20"
pngquant='pngquant --skip-if-larger --strip --verbose --ext .png --force'
pngquant_images="$pngquant --quality 80"
pngquant_thumbnails="$pngquant --quality 20"
cwebp='cwebp -m 6'
cwebp_images="$cwebp -near_lossless 0 -q 80"
cwebp_thumbnails="$cwebp -q 20"
gif2webp='gif2webp -min_size -loop_compatibility -metadata none -m 6 -q 80'
gifsicle='gifsicle --optimize=3 --crop 270x160 --colors=64 --lossy=100'
mogrify_thumbnails='mogrify -path thumbnails -auto-orient -quiet -thumbnail x222'

cd "$directory";
mkdir -p thumbnails;

for image in *; do

  file=$(file --mime-type "$image")
  name=${image%.*}
  mime=${file#*: }

  if test "$image" -nt "$timestamp"; then

    if [ "$mime" = "image/png" ] || [ "$mime" = "image/jpeg" ]; then
      while true; do
        { [ -f "$name.png" ] && [ -f "$name.jpg" ]; } && break;
        mogrify -format jpg -quiet "$name.png" || true;
        mogrify -format png -quiet "$name.jpg" || true;
      done

      size_png=$(wc -c < "$name.png")
      size_jpg=$(wc -c < "$name.jpg")

      $pngquant_images "$name.png" || true;
      $mogrify_thumbnails "$name.png";
      $pngquant_thumbnails "thumbnails/$name.png" || true;

      $jpegoptim_images "$name.jpg";
      $mogrify_thumbnails "$name.jpg";
      $jpegoptim_thumbnails "thumbnails/$name.jpg";

      if [ "$size_png" -lt "$size_jpg" ]; then
        $cwebp_images "$name.png" -o "$name.webp";
        $cwebp_thumbnails "thumbnails/$name.png" -o "thumbnails/$name.webp";
      else
        $cwebp_images "$name.jpg" -o "$name.webp";
        $cwebp_thumbnails "thumbnails/$name.jpg" -o "thumbnails/$name.webp";
      fi
    fi

    if [ "$mime" = "image/gif" ]; then
      $gif2webp "$image" -o "$name.webp";
      $gifsicle "$image" "#0-50" -o "thumbnails/$image";
    fi
  fi
done

touch "$timestamp";
printf "Images and thumbnails in '%s' updated.\n" "$PWD";