-
Notifications
You must be signed in to change notification settings - Fork 22
/
optimise-images.sh
executable file
·2275 lines (2166 loc) · 115 KB
/
optimise-images.sh
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /bin/bash
########################################################################
# batch optimise images
# written by George Liu (eva2000) centminmod.com
# docs
# https://www.imagemagick.org/Usage/thumbnails/
# https://www.imagemagick.org/Usage/files/#read_mods
# https://www.imagemagick.org/Usage/advanced/
# https://www.imagemagick.org/Usage/basics/#mogrify
# https://www.imagemagick.org/script/command-line-options.php#define
# https://www.imagemagick.org/Usage/files/#write
# https://www.imagemagick.org/Usage/api/#scripts
# https://www.imagemagick.org/Usage/files/#massive
# https://www.imagemagick.org/script/architecture.php
# https://www.imagemagick.org/script/compare.php
# http://www.imagemagick.org/Usage/compare/#statistics
#
# webp
# http://caniuse.com/#feat=webp
# https://developers.google.com/speed/webp/
# https://www.imagemagick.org/script/webp.php
#
# guetzli
# https://github.com/google/guetzli/
# https://github.com/google/guetzli/issues/195
#
# mozjpeg
# https://github.com/mozilla/mozjpeg
#
# test images
# https://testimages.org/
# https://css-ig.net/png-test-corpus
# https://github.com/nwtn/image-resize-tests/blob/master/asset-sources.txt
# https://github.com/FLIF-hub/benchmarks
#
# butteraugli
# https://github.com/google/butteraugli
#
# GraphicsMagick
# http://www.graphicsmagick.org/convert.html
# http://www.graphicsmagick.org/identify.html
#
# lazy load gallery images
# http://dinbror.dk/blog/blazy/?ref=demo-page
########################################################################
DT=$(date +"%d%m%y-%H%M%S")
SCRIPTDIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
VER='6.5'
DEBUG='n'
# Used for optimise-age mod, set FIND_IMGAGE in minutes. So to only
# optimise images
# older than 1 hour set FIND_IMGAGE='60'
# older than 1 day set FIND_IMGAGE='1440'
# older than 1 week set FIND_IMGAGE='10080'
# older than 1 month set FIND_IMGAGE='43200'
FIND_IMGAGE=''
# Optional add comment to optimised images "optimised" to allow
# subsequent re-runs of script to detect the comment and skip
# re-optimising of the previously optimised image
ADD_COMMENT='y'
# System resource management for cpu and disk utilisation
NICE='/bin/nice'
# Nicenesses range from -20 (most favorable scheduling)
# to 19 (least favorable)
NICEOPT='-n 10'
IONICE='/usr/bin/ionice'
# -c class
# The scheduling class. 0 for none, 1 for real time, 2 for best-effort, 3 for idle.
# -n classdata
# The scheduling class data. This defines the class data, if the class accepts an
# argument. For real time and best-effort, 0-7 is valid data and the priority
# i.e. -c2 -n0 would be best effort with highest priority
IONICEOPT='-c2 -n7'
# Optimisation routine settings
# UNATTENDED_OPTIMISE controls whether optimise command will prompt
# user to ask if they have backed up the image directory before runs
# setting UNATTENDED_OPTIMISE='y' will skip that question prompt
# suited more for script runs i.e. a for or while loop of a batch of
# subdirectories within a parent directory to automate optimise-images.sh
# optimise runs against each subdirectory.
UNATTENDED_OPTIMISE='n'
# control sample image downloads
# allows you to control how many sample images to work with/download
# guetzli testing is very resource and time consuming so working with
# a smaller sample image set would be better
TESTFILES_MINIMAL='y'
TESTFILES_PNGONLY='n'
TESTFILES_JPEGONLY='n'
TESTFILES_WITHSPACES='n'
# max width and height
MAXRES='2048'
# Max directory depth to look for images
# currently only works at maxdepth=1 so
# do not edit yet
MAXDEPTH='1'
# ImageMagick Settings
# enable IMAGEMAGICK_HEIF='y' to install HEIF support
IMAGEMAGICK_HEIF='n'
IMAGICK_RESIZE='y'
IMAGICK_JPEGHINT='y'
# https://legacy.imagemagick.org/script/command-line-options.php#quality
IMAGICK_QUALITY='82'
IMAGICK_WEBP='n'
IMAGICK_WEBP_CONDITIONAL='y'
IMAGICK_WEBPQUALITY='75'
IMAGICK_WEBPQUALITYALPHA='100'
IMAGICK_WEBPMETHOD='4'
IMAGICK_WEBPLOSSLESS='n'
IMAGICK_WEBPTHREADS='1'
# Quantum depth 8 or 16 for ImageMagick 7
# Source installs
IMAGICK_QUANTUMDEPTH='8'
IMAGICK_SEVEN='n'
IMAGICK_SEVENHDRI='n'
IMAGICK_TMPDIR='/home/imagicktmp'
IMAGICK_JPGOPTS=' -filter triangle -define filter:support=2 -define jpeg:fancy-upsampling=off -unsharp 0.25x0.08+8.3+0.045'
# zlib compression level for PNG files with valid values between 0 to 9 where 0 will get Huffman compression and not zlib
IMAGICK_PNGOPTS_COMPLEVEL='9'
# valid strategy values are 1 or 2
IMAGICK_PNGOPTS_COMPSTRATEGY='2'
IMAGICK_PNGOPTS=" -define png:compression-filter=5 -define png:compression-level=${IMAGICK_PNGOPTS_COMPLEVEL} -define png:compression-strategy=${IMAGICK_PNGOPTS_COMPSTRATEGY}"
IMAGICK_WEBPOPTS=" -define webp:method=${IMAGICK_WEBPMETHOD} -define webp:alpha-quality=${IMAGICK_WEBPQUALITYALPHA} -define webp:lossless=false -quality ${IMAGICK_WEBPQUALITY}"
# GraphicsMagick Settings
GM_USE='n'
# strip meta-data
STRIP='y'
# additional image optimisations after imagemagick
# resizing
# choose one of the 3 JPEGOPTIM, GUETZLI or MOZJPEG
JPEGOPTIM='y'
GUETZLI='n'
MOZJPEG='n'
# choose either OPTIPNG or ZOPFLIPNG
OPTIPNG='y'
ZOPFLIPNG='n'
# Speed control
# default is -o2 set 2
OPTIPNG_COMPRESSION='2'
# Guetzli Options
# GUETZLI_JPEGONLY will only optimise original jpeg/jpg
# images and NOT convert png to Guetzli optimised jpgs
# set to = 'n' to convert png as well
GUETZLI_JPEGONLY='y'
GUETZLI_QUALITY='85'
GUETZLI_OPTS='--verbose'
# MozJPEG Options
# MOZJPEG_JPEGONLY will only optimise original jpeg/jpg
# images and NOT convert png to MozJPEG optimised jpgs
# set to = 'n' to convert png as well
MOZJPEG_JPEGONLY='y'
MOZJPEG_JPEGTRAN='y'
MOZJPEG_CJPEG='n'
MOZJPEG_QUALITY='-quality 82'
MOZJPEG_OPTS='-verbose'
MOZJPEG_VERSION='3.3.1'
# ZopfliPNG Settings
# Always create ZopfliPNG version even if original is
# smaller for benchmarking.
ZOPFLIPNG_ALWAYS='y'
# Default iterations is 15 for small files
# 5 for large files. Set to Auto for defaults
ZOPFLIPNG_ITERATIONS='auto'
# --lossy_8bit --lossy_transparent
ZOPFLIPNG_LOSSY='n'
# profile option display fields for transparency color and background color
# disabled by default to speed up profile processing
PROFILE_EXTEND='n'
# comparison mode when enabled will when resizing and optimising images
# write to a separate optimised image within the same directory as the
# original images but with a suffix attached to the end of original image
# file name i.e. image.png vs image_optimal.png
COMPARE_MODE='n'
COMPARE_SUFFIX='_optimal'
# optionally create thumbnails in separate directory
# within image directory and thumbnail width x height
# and thumbnail image format default = .jpg
THUMBNAILS='n'
THUMBNAILS_QUALITY='70'
THUMBNAILS_WIDTH='160'
THUMBNAILS_HEIGHT='160'
THUMBNAILS_FORMAT='jpg'
THUMBNAILS_DIRNAME='thumbnails'
# Gallery settings
# static gallery for specific modes can be enabled
# for optimise-web and optimise-web-nginx mods
GALLERY_WEBP='y'
GALLERY_THUMBNAILS='y'
GALLERY_THUMBNAILSCOMPRESS='n'
GALLERY_THUMBNAILSDIR='gallery-webp-thumbnails'
LOGDIR='/home/optimise-logs'
LOGNAME_PROFILE="profile-log-${DT}.log"
LOGNAME_OPTIMISE="optimise-log-${DT}.log"
LOGNAME_WEBP="optimise-webp-filter-log-${DT}.log"
LOG_PROFILE="${LOGDIR}/${LOGNAME_PROFILE}"
LOG_OPTIMISE="${LOGDIR}/${LOGNAME_OPTIMISE}"
LOG_WEBPFILTER="${LOGDIR}/${LOGNAME_WEBP}"
BENCHDIR='/home/optimise-benchmarks'
BUTTERAUGLI='y'
GUETZLI_BIN='/opt/guetzli/bin/Release/guetzli'
BUTTERAUGLI_BIN='/usr/bin/butteraugli'
GM_BIN='/usr/bin/gm'
DEVTOOLSETTEN='y'
########################################################################
# DO NOT EDIT BELOW THIS POINT
if [ -f "${SCRIPTDIR}/optimise-images.ini" ]; then
source "${SCRIPTDIR}/optimise-images.ini"
fi
CENTOSVER=$(cat /etc/redhat-release | awk '{ print $3 }')
if [ "$CENTOSVER" == 'release' ]; then
CENTOSVER=$(cat /etc/redhat-release | awk '{ print $4 }' | cut -d . -f1,2)
if [[ "$(cat /etc/redhat-release | awk '{ print $4 }' | cut -d . -f1)" = '7' ]]; then
CENTOS_SEVEN='7'
fi
fi
if [[ "$(cat /etc/redhat-release | awk '{ print $3 }' | cut -d . -f1)" = '6' ]]; then
CENTOS_SIX='6'
fi
if [ ! -f /usr/bin/wget ]; then
yum -q -y install wget
fi
if [ ! -f /etc/yum.repos.d/epel.repo ]; then
echo "Install & configure EPEL YUM Repo"
yum -q -y install epel-release
fi
# install remi version of IMAGEMAGICK instead of older centos native IMAGEMAGICK
# optimise-images.sh was written for centminmod.com LEMP stacks which already install
# remi ImageMagick. So if ImageMagick is not installed, assume the CentOS system is
# non=centminmod and install all required dependencies at once to bypass all the
# subsequent yum checks/installs
if [[ "$IMAGEMAGICK_HEIF" = [yY] ]]; then
IMG_HEIFOPT=' ImageMagick6-heic'
libheif_install
else
IMG_HEIFOPT=""
fi
if [[ ! -f /etc/centminmod-release && "$CENTOS_SEVEN" = '7' && ! "$(rpm -qa ImageMagick6 | grep -o 'ImageMagick6')" ]]; then
if [ ! -f /etc/yum.repos.d/remi.repo ]; then
echo
echo "Install & configure Remi YUM Repo & YUM dependencies"
wget -q -4 https://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm
fi
echo
echo "Install YUM dependencies"
yum -q -y install ImageMagick6 ImageMagick6-devel ImageMagick6-c++ ImageMagick6-c++-devel ImageMagick6-libs${IMG_HEIFOPT} autoconf automake libtool LibRaw libjpeg-turbo-devel libpng-devel wget bc git make nasm gcc gcc-c++ coreutils optipng jpegoptim jpegtran GraphicsMagick sysstat util-linux --enablerepo=remi
echo "YUM dependencies installed"
elif [[ ! -f /etc/centminmod-release && "$CENTOS_SIX" = '6' && ! "$(rpm -qa ImageMagick6 | grep -o 'ImageMagick6')" ]]; then
if [ ! -f /etc/yum.repos.d/remi.repo ]; then
echo
echo "Install & configure Remi YUM Repo & YUM dependencies"
wget -q -4 https://rpms.remirepo.net/enterprise/remi-release-6.rpm
rpm -Uvh remi-release-6.rpm
fi
echo
echo "Install YUM dependencies"
yum -q -y install ImageMagick6 ImageMagick6-devel ImageMagick6-c++ ImageMagick6-c++-devel ImageMagick6-libs${IMG_HEIFOPT} autoconf automake libtool LibRaw libjpeg-turbo-devel libpng-devel wget bc git make nasm gcc gcc-c++ coreutils optipng jpegoptim jpegtran GraphicsMagick sysstat util-linux-ng --enablerepo=remi
echo "YUM dependencies installed"
fi
if [[ "$IMAGEMAGICK_HEIF" = [yY] && ! -f /etc/centminmod-release && "$CENTOS_SEVEN" = '7' && ! "$(rpm -qa ImageMagick6-heic | grep -o 'ImageMagick6-heic')" ]]; then
echo
echo "Install ImageMagick6-heic"
yum -q -y install ImageMagick6-heic --enablerepo=remi
echo "ImageMagick6-heic installed"
elif [[ "$IMAGEMAGICK_HEIF" = [yY] && ! -f /etc/centminmod-release && "$CENTOS_SIX" = '6' && ! "$(rpm -qa ImageMagick6-heic | grep -o 'ImageMagick6-heic')" ]]; then
echo
echo "Install ImageMagick6-heic"
yum -q -y install ImageMagick6-heic --enablerepo=remi
echo "ImageMagick6-heic installed"
fi
if [[ "$IMAGEMAGICK_HEIF" = [yY] && -f /etc/yum/pluginconf.d/versionlock.conf && -f /etc/yum.repos.d/remi.repo ]]; then
yum versionlock delete ImageMagick6 ImageMagick6-devel ImageMagick6-c++ ImageMagick6-c++-devel ImageMagick6-libs ImageMagick6-heic LibRaw >/dev/null 2>&1
yum versionlock delete ImageMagick ImageMagick-devel ImageMagick-c++ ImageMagick-c++-devel ImageMagick-libs ImageMagick-heic LibRaw >/dev/null 2>&1
yum versionlock ImageMagick6 ImageMagick6-devel ImageMagick6-c++ ImageMagick6-c++-devel ImageMagick6-libs ImageMagick6-heic LibRaw >/dev/null 2>&1
elif [[ "$IMAGEMAGICK_HEIF" != [yY] && -f /etc/yum/pluginconf.d/versionlock.conf && -f /etc/yum.repos.d/remi.repo ]]; then
yum versionlock delete ImageMagick6 ImageMagick6-devel ImageMagick6-c++ ImageMagick6-c++-devel ImageMagick6-libs LibRaw >/dev/null 2>&1
yum versionlock delete ImageMagick ImageMagick-devel ImageMagick-c++ ImageMagick-c++-devel ImageMagick-libs LibRaw >/dev/null 2>&1
yum versionlock ImageMagick6 ImageMagick6-devel ImageMagick6-c++ ImageMagick6-c++-devel ImageMagick6-libs LibRaw >/dev/null 2>&1
fi
if [ ! -f /usr/bin/git ]; then
yum -q -y install git
fi
if [ ! -f /usr/bin/make ]; then
yum -q -y install make
fi
if [ ! -f /usr/bin/nasm ]; then
yum -q -y install nasm
fi
if [ ! -f /usr/bin/g++ ]; then
yum -q -y install gcc-c++
fi
if [[ "$MOZJPEG_JPEGTRAN" = [yY] ]]; then
MOZJPEG_BIN='/opt/mozjpeg/bin/jpegtran'
MOZJPEG_QUALITY=""
else
MOZJPEG_BIN='/opt/mozjpeg/bin/cjpeg'
fi
# Binary paths
if [[ "$GM_USE" = [yY] && -f /usr/bin/gm ]]; then
IDENTIFY_BIN='/usr/bin/gm identify'
CONVERT_BIN='/usr/bin/gm convert'
elif [[ "$IMAGICK_SEVEN" = [yY] && -f /opt/imagemagick7/bin/identify ]]; then
IDENTIFY_BIN='/opt/imagemagick7/bin/identify'
CONVERT_BIN='/opt/imagemagick7/bin/convert'
else
IDENTIFY_BIN='/usr/bin/identify'
CONVERT_BIN='/usr/bin/convert'
fi
if [ -f /proc/user_beancounters ]; then
CPUS=$(cat "/proc/cpuinfo" | grep "processor"|wc -l)
else
# speed up make
CPUS=$(cat "/proc/cpuinfo" | grep "processor"|wc -l)
fi
if [[ "$CPUS" -ge ' 4' ]]; then
IMAGICK_THREADLIMIT=$(($CPUS/2))
export MAGICK_THREAD_LIMIT="$IMAGICK_THREADLIMIT"
IMAGICK_WEBPTHREADSOPTS=" -define webp:thread-level=${IMAGICK_WEBPTHREADS}"
else
IMAGICK_WEBPTHREADSOPTS=""
fi
if [ ! -f /bin/nice ]; then
yum -q -y install coreutils
fi
if [ ! -f /usr/bin/ionice ]; then
if [[ "$CENTOS_SIX" = '6' ]]; then
yum -q -y install util-linux-ng
else
yum -q -y install util-linux
fi
fi
if [ ! -f /usr/bin/sar ]; then
yum -y -q install sysstat
if [[ "$(uname -m)" = 'x86_64' ]]; then
SARCALL='/usr/lib64/sa/sa1'
else
SARCALL='/usr/lib/sa/sa1'
fi
else
if [[ "$(uname -m)" = 'x86_64' ]]; then
SARCALL='/usr/lib64/sa/sa1'
else
SARCALL='/usr/lib/sa/sa1'
fi
fi
if [ ! -f /usr/bin/bc ]; then
yum -q -y install bc
fi
if [ ! -f /usr/bin/optipng ]; then
yum -q -y install optipng
fi
if [ ! -f /usr/bin/jpegoptim ]; then
yum -q -y install jpegoptim
fi
if [ ! -f /usr/bin/gm ]; then
yum -q -y install GraphicsMagick
fi
if [[ "$ZOPFLIPNG" = [yY] && ! -f /usr/bin/zopflipng ]]; then
echo
echo "installing zopflipng"
mkdir -p /opt/zopfli
cd /opt/zopfli
git clone https://github.com/google/zopfli
cd zopfli/
make -s -j2
make -s zopflipng
make -s libzopfli
\cp -f zopflipng /usr/bin/zopflipng
# OPTIPNG='n'
echo "installed zopflipng"
elif [[ "$ZOPFLIPNG" = [yY] && -f /usr/bin/zopflipng ]]; then
# OPTIPNG='n'
echo
fi
if [[ "$STRIP" = [Yy] ]]; then
STRIP_OPT=' -strip'
else
STRIP_OPT=""
fi
if [[ "$ADD_COMMENT" = [Yy] ]]; then
ADDCOMMENT_OPT=' -set comment optimised'
PNGSTRIP_OPT=""
else
ADDCOMMENT_OPT=""
PNGSTRIP_OPT="$STRIP_OPT"
fi
if [[ "$IMAGICK_JPEGHINT" = [yY] ]]; then
JPEGHINT_WIDTH=$(($MAXRES*2))
JPEGHINT_HEIGHT=$(($MAXRES*2))
JPEGHINT_OPT=" -define jpeg:size=${JPEGHINT_WIDTH}x${JPEGHINT_HEIGHT}"
else
JPEGHINT_OPT=""
fi
if [[ "$IMAGICK_WEBP" = [yY] ]]; then
FIND_WEBP=' -o -name "*.webp"'
else
FIND_WEBP=""
fi
if [[ "$IMAGICK_WEBPLOSSLESS" = [yY] ]]; then
IMAGICK_WEBPOPTS=" -define webp:method=${IMAGICK_WEBPMETHOD} -define webp:lossless=true"
fi
if [[ "$ZOPFLIPNG_ALWAYS" = [yY] ]]; then
ZOPFLIPNG_OPTSALWAYS=' --always_zopflify'
else
ZOPFLIPNG_OPTSALWAYS=''
fi
if [[ "$ZOPFLIPNG_LOSSY" = [yY] ]]; then
ZOPFLIPNG_OPTSLOSSY=' --lossy_8bit --lossy_transparent'
else
ZOPFLIPNG_OPTSLOSSY=''
fi
if [[ "$ZOPFLIPNG_ITERATIONS" = 'auto' ]]; then
# other options
# --filters=01234mepb
ZOPFLIPNG_OPTS=" -y${ZOPFLIPNG_OPTSALWAYS}${ZOPFLIPNG_OPTSLOSSY}"
else
ZOPFLIPNG_OPTS=" -y --iterations=${ZOPFLIPNG_ITERATIONS}${ZOPFLIPNG_OPTSALWAYS}${ZOPFLIPNG_OPTSLOSSY}"
fi
if [[ "$IMAGICK_RESIZE" = [nN] ]]; then
# imagemagick resizes and does image optimisation passing it to jpegotim
# for further optimisations but if you set IMAGEICK_RESIZE='n' and also
# set JPEGOPTIM='n' then jpg images won't have any optimisation done
# so when IMAGEICK_RESIZE='n' set force JPEGOPTIM='y' automatically
JPEGOPTIM='y'
fi
if [ ! -d "$IMAGICK_TMPDIR" ]; then
mkdir -p "$IMAGICK_TMPDIR"
chmod 1777 "$IMAGICK_TMPDIR"
elif [ -d "$IMAGICK_TMPDIR" ]; then
chmod 1777 "$IMAGICK_TMPDIR"
fi
if [ ! -d "$LOGDIR" ]; then
mkdir -p "$LOGDIR"
fi
if [ ! -d "$BENCHDIR" ]; then
mkdir -p "$BENCHDIR"
fi
IMAGICK_VERSION=$($CONVERT_BIN -version | head -n1 | awk '/^Version:/ {print $2,$3,$4,$5,$6}')
##########################################################################
# function
if [[ "$GM_USE" = [yY] ]]; then
IMAGICK_TMPDIR='/home/imagicktmp'
IMAGICK_JPGOPTS=' -filter triangle -unsharp 0.25x0.08+8.3+0.045'
IMAGICK_PNGOPTS=''
IMAGICK_WEBPOPTS=" -quality ${IMAGICK_WEBPQUALITY}"
JPEGHINT_WIDTH=$(($MAXRES*2))
JPEGHINT_HEIGHT=$(($MAXRES*2))
JPEGHINT_OPT=" -size ${JPEGHINT_WIDTH}x${JPEGHINT_HEIGHT}"
DEFINE_TMP=''
else
DEFINE_TMP=" -define registry:temporary-path="${IMAGICK_TMPDIR}""
fi
enable_devtoolset() {
if [[ "$DEVTOOLSETTEN" = [yY] ]]; then
if [[ "$(rpm -ql centos-release-scl >/dev/null 2>&1; echo $?)" -ne '0' ]]; then
time yum -y -q install centos-release-scl
fi
if [[ ! -f /opt/rh/devtoolset-10/root/usr/bin/gcc || ! -f /opt/rh/devtoolset-10/root/usr/bin/g++ ]]; then
if [[ "$(rpm -ql devtoolset-10-gcc >/dev/null 2>&1; echo $?)" -ne '0' ]] || [[ "$(rpm -ql devtoolset-10-gcc-c++ >/dev/null 2>&1; echo $?)" -ne '0' ]] || [[ "$(rpm -ql devtoolset-10-binutils >/dev/null 2>&1; echo $?)" -ne '0' ]]; then
time yum -y -q install devtoolset-10-gcc devtoolset-10-gcc-c++ devtoolset-10-binutils
source /opt/rh/devtoolset-10/enable
fi
else
source /opt/rh/devtoolset-10/enable
fi
# GENERALDEVTOOLSET_FALLTHROUGH=' -Wimplicit-fallthrough=0'
# GENERALDEVTOOLSET_EXTRAFLAGS=' -fcode-hoisting -Wno-cast-function-type -Wno-error=cast-align -Wno-implicit-function-declaration -Wno-builtin-declaration-mismatch -Wno-deprecated-declarations'
# export CFLAGS=="-O2 ${GENERALDEVTOOLSET_FALLTHROUGH}${GENERALDEVTOOLSET_EXTRAFLAGS}"
# export CXXFLAGS="$CFLAGS"
# export CC="ccache gcc"
# export CXX="ccache g++"
fi
}
libheif_install() {
echo "Install libheif routine"
echo "installing x265, libde265 & libheif"
enable_devtoolset
if [[ ! "$(rpm -qa cmake3)" || ! "$(rpm -qa cmake3-data)" ]]; then
yum -q -y install cmake3 cmake3-data
fi
if [[ ! "$(rpm -qa mercurial)" ]]; then
yum -q -y install mercurial
fi
echo "Install x265 for libheif"
pushd "$DIR_TMP"
rm -rf x265
git clone https://bitbucket.org/multicoreware/x265_git x265
cd x265/build/linux
cmake -G "Unix Makefiles" -DENABLE_SHARED:bool=off -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ ../../source
make -s -j$(nproc)
make install
echo
echo "Install libde265 for libheif"
pushd "$DIR_TMP"
rm -rf libde265
git clone --depth=1 https://github.com/strukturag/libde265
cd libde265
# mkdir -p build
# cd build
# rm -rf CMakeCache.txt
# make clean
# cmake3 ..
./autogen.sh
./configure
make -s -j$(nproc)
make install
echo
echo "libde265 install completed"
export CC="gcc"
export CXX="g++"
echo "Install libheif for HEIF file format decoder/encoder"
pushd "$DIR_TMP"
rm -rf libheif
git clone --depth=1 https://github.com/strukturag/libheif
cd libheif
./autogen.sh
make clean
# export libde265_CFLAGS='-I/usr/local/include'
# export libde265_LIBS='-L/usr/local/lib'
# export x265_CFLAGS='-I/usr/local/include'
# export x265_LIBS='-L/usr/local/lib'
PKG_CONFIG_PATH="/usr/lib64/pkgconfig:/usr/share/pkgconfig:/usr/local/lib/pkgconfig" LD_LIBRARY_PATH=/usr/local/lib LDFLAGS="-L/usr/local/lib" CPPFLAGS="-I/usr/local/include" ./configure --disable-examples
make -s -j$(nproc)
make install
echo
echo "libheif install completed"
# unset libde265_CFLAGS
# unset libde265_LIBS
# unset x265_CFLAGS
# unset x265_LIBS
ln -s /usr/local/lib/libheif.so.1.6.2 /usr/lib64/libheif.so.1.6.2
# ln -s /usr/local/lib/libheif.so.1.6.2 /usr/lib64/libheif.so.1.3.2
ln -s /usr/local/lib/libheif.a /usr/lib64/libheif.a
ln -s /usr/local/lib/libheif.la /usr/lib64/libheif.la
ln -s /usr/local/lib/libheif.so /usr/lib64/libheif.so
ln -s /usr/local/lib/libheif.so.1 /usr/lib64/libheif.so.1
# ln -s /usr/local/lib/libheif.la /usr/lib64/ImageMagick-6.9.11/modules-Q16/coders/libheif.la
# ln -s /usr/local/lib/libheif.so /usr/lib64/ImageMagick-6.9.11/modules-Q16/coders/libheif.so
ln -s /usr/local/lib/pkgconfig/libheif.pc /usr/lib64/pkgconfig/libheif.pc
# rm -rf /usr/lib64/libheif.so.1.6.2
# rm -rf /usr/lib64/libheif.so.1.3.2
# rm -rf /usr/lib64/ImageMagick-6.9.11/modules-Q16/coders/libheif.la
# rm -rf /usr/lib64/ImageMagick-6.9.11/modules-Q16/coders/libheif.so
# rm -rf /usr/lib64/libheif.a
# rm -rf /usr/lib64/libheif.la
# rm -rf /usr/lib64/libheif.so
# rm -rf /usr/lib64/libheif.so.1
# rm -rf /usr/lib64/pkgconfig/libheif.pc
ldconfig
}
mozjpeg_install() {
if [[ ! -f "$MOZJPEG_BIN" ]] || [[ -f "$MOZJPEG_BIN" && "$($MOZJPEG_BIN -version 2>&1 | awk '{print $3}')" != "$MOZJPEG_VERSION" ]]; then
if [ ! -f /usr/bin/nasm ]; then
yum -q -y install nasm
fi
echo
echo "installing mozjpeg"
cd /usr/src
wget -4 "https://github.com/mozilla/mozjpeg/archive/v${MOZJPEG_VERSION}.tar.gz" -O "mozjpeg-${MOZJPEG_VERSION}.tar.gz"
tar xzf "mozjpeg-${MOZJPEG_VERSION}.tar.gz"
rm -rf "mozjpeg-${MOZJPEG_VERSION}.tar.gz"
cd "mozjpeg-${MOZJPEG_VERSION}"
make clean -s
autoreconf -fiv
./configure
make -s -j$(nproc)
make -s install
rm -rf ../mozjpeg-${MOZJPEG_VERSION}
MOZJPEG_BIN='/opt/mozjpeg/bin/jpegtran'
echo "installed mozjpeg"
fi
}
butteraugli_install() {
if [[ "$BUTTERAUGLI" = [yY] ]]; then
echo
echo "installing butteraugli"
cd /opt
rm -rf butteraugli
git clone https://github.com/google/butteraugli
cd butteraugli/butteraugli
make -s -j$(nproc)
if [ -f butteraugli ]; then
\cp -af butteraugli /usr/bin/butteraugli
fi
if [ -f /usr/bin/butteraugli ]; then
BUTTERAUGLI_BIN='/usr/bin/butteraugli'
echo "installed butteraugli"
else
echo "failed to install butteraugli"
fi
fi
}
guetzli_install() {
echo
echo "installing guetzli"
cd /opt
rm -rf guetzli
git clone https://github.com/google/guetzli
cd guetzli
make
GUETZLI_BIN='/opt/guetzli/bin/Release/guetzli'
echo "installed guetzli"
}
if [[ "$GUETZLI" = [yY] && ! -f /usr/bin/libpng-config && ! -f "$GUETZLI_BIN" ]]; then
yum -q -y install libpng-devel
guetzli_install
elif [[ "$GUETZLI" = [yY] && -f /usr/bin/libpng-config && ! -f "$GUETZLI_BIN" ]]; then
guetzli_install
fi
if [[ "$CENTOS_SEVEN" -eq '7' && ! -f "$BUTTERAUGLI_BIN" ]]; then
butteraugli_install
fi
install_source() {
echo "------------------------------------"
echo "install ImageMagick 7 at:"
echo "/opt/imagemagick7/bin/identify"
echo "/opt/imagemagick7/bin/convert"
echo "------------------------------------"
CPUVENDOR=$(cat /proc/cpuinfo | awk '/vendor_id/ {print $3}' | sort -u | head -n1)
SSECHECK=$(gcc -c -Q -march=native --help=target | awk '/ -msse/ {print $2}' | head -n1)
if [[ "$(uname -m)" = 'x86_64' && "$CPUVENDOR" = 'GenuineIntel' && "$SSECHECK" = '[enabled]' ]]; then
CCM=64
CRYPTOGCC_OPT="-m${CCM} -march=native"
# if only 1 cpu thread use -O2 to keep compile times sane
if [[ "$CPUS" = '1' ]]; then
export CFLAGS="-O2 $CRYPTOGCC_OPT -pipe"
else
export CFLAGS="-O3 $CRYPTOGCC_OPT -pipe"
fi
export CXXFLAGS="$CFLAGS"
fi
if [[ "$IMAGICK_SEVENHDRI" = [yY] ]]; then
HDRI_OPT='--enable-hdri'
else
HDRI_OPT='--disable-hdri'
fi
# built Q8 instead system Q16 for speed
# http://www.imagemagick.org/script/advanced-unix-installation.php
cd /usr/src
rm -rf ImageMagick.tar.gz
rm -rf ImageMagick-7*
wget -cnv https://www.imagemagick.org/download/ImageMagick.tar.gz
tar xzf ImageMagick.tar.gz
cd ImageMagick-7*
make clean
./configure CFLAGS="$CFLAGS" --prefix=/opt/imagemagick7 --with-quantum-depth="${IMAGICK_QUANTUMDEPTH}" "${HDRI_OPT}"
make -j${CPUS}
make install
IDENTIFY_BIN='/opt/imagemagick7/bin/identify'
CONVERT_BIN='/opt/imagemagick7/bin/convert'
echo "------------------------------------"
echo ""
echo "------------------------------------"
}
sar_call() {
$SARCALL 1 1
}
gallery_webp() {
WORKDIR=$1
if [[ "$IMAGICK_RESIZE" = [nN] ]]; then
LEFTLABEL='original'
LEFTTITLELABEL='Original'
else
LEFTLABEL='resized/optimised'
LEFTTITLELABEL='Resized/Optimised'
fi
if [[ "$AGE" = [yY] && ! -z "$FIND_IMGAGE" ]]; then
FIND_IMGAGEOPT=" -mmin -${FIND_IMGAGE}"
FIND_IMGAGETXT="filtered: $FIND_IMGAGE minutes old"
else
FIND_IMGAGEOPT=""
FIND_IMGAGETXT=""
fi
cd "$WORKDIR"
echo "<!DOCTYPE html>" | tee "${WORKDIR}/gallery-webp.html"
echo "<html lang='en-us'>" | tee -a "${WORKDIR}/gallery-webp.html"
echo "<head>" | tee -a "${WORKDIR}/gallery-webp.html"
echo " <meta charset='utf-8'>" | tee -a "${WORKDIR}/gallery-webp.html"
echo " <title>${LEFTTITLELABEL} vs WebP</title>" | tee -a "${WORKDIR}/gallery-webp.html"
echo " <meta name='viewport' content='width=device-width, initial-scale=1'>" | tee -a "${WORKDIR}/gallery-webp.html"
echo "<style>" | tee -a "${WORKDIR}/gallery-webp.html"
echo "body { font-family: Tahoma, Helvetica, Arial; }" | tee -a "${WORKDIR}/gallery-webp.html"
echo "#group-wrap { width:100%; max-width:640px; margin:auto 0; text-align:center }" | tee -a "${WORKDIR}/gallery-webp.html"
echo ".section { clear:both; padding:0; margin:0 }" | tee -a "${WORKDIR}/gallery-webp.html"
echo ".col { display:block; float:left; margin:1% 0 1% 1.6% }" | tee -a "${WORKDIR}/gallery-webp.html"
echo ".col:first-child { margin-left:1.6% }" | tee -a "${WORKDIR}/gallery-webp.html"
echo ".group:before,.group:after { content:\"\"; display:table }" | tee -a "${WORKDIR}/gallery-webp.html"
echo ".group:after { clear:both }" | tee -a "${WORKDIR}/gallery-webp.html"
echo ".group { zoom:1 }" | tee -a "${WORKDIR}/gallery-webp.html"
echo ".span_2_of_2 { width:100% }" | tee -a "${WORKDIR}/gallery-webp.html"
echo ".span_1_of_2 { width:48.2% }" | tee -a "${WORKDIR}/gallery-webp.html"
echo "p.medium-font { font-size: 12.5px; }" | tee -a "${WORKDIR}/gallery-webp.html"
echo ".b-lazy { -webkit-transition:opacity 500ms ease-in-out; -moz-transition:opacity 500ms ease-in-out; -o-transition:opacity 500ms ease-in-out; transition:opacity 500ms ease-in-out; max-width:100%; opacity:0 }"
echo ".b-lazy.b-loaded { opacity:1 }"
echo "@media only screen and (max-width: 480px) { .col { margin:1% 0 } .span_2_of_2,.span_1_of_2 { width:50% } }" | tee -a "${WORKDIR}/gallery-webp.html"
echo "</style>" | tee -a "${WORKDIR}/gallery-webp.html"
echo "</head>" | tee -a "${WORKDIR}/gallery-webp.html"
echo "" | tee -a "${WORKDIR}/gallery-webp.html"
echo "<body>" | tee -a "${WORKDIR}/gallery-webp.html"
echo "<div id=\"group-wrap\">" | tee -a "${WORKDIR}/gallery-webp.html"
echo " <div class=\"section group\">" | tee -a "${WORKDIR}/gallery-webp.html"
# gather the images for gallery 2 arguments at a time via xargs -n2 for X and Y for original vs webp
find "$WORKDIR" -maxdepth ${MAXDEPTH}${FIND_IMGAGEOPT} \( -name "*.jpg" -o -name "*.png" -o -name "*.jpeg" \) | sort | xargs -0 | while read x; do
X=$(basename "$x");
Y="${X}.webp";
X_EXT="${X##*.}"
Y_EXT="${Y##*.}"
X_SIZE=$(stat -c "%s" "$X")
X_SIZE=$(echo "scale=2;$X_SIZE/1024"|bc)
Y_SIZE=$(stat -c "%s" "$Y")
Y_SIZE=$(echo "scale=2;$Y_SIZE/1024"|bc)
X_DIMENS=$($IDENTIFY_BIN -format '%wx%h' "$X")
Y_DIMENS=$($IDENTIFY_BIN -format '%wx%h' "$Y")
# generate thumbnails for gallery start
if [[ "$GALLERY_THUMBNAILS" = [yY] ]]; then
# check thumbnail image info
xtn_file="$X"
xtn_extension="${xtn_file##*.}"
xtn_filename="${xtn_file%.*}"
ytn_file="$Y"
ytn_extension="${ytn_file##*.}"
ytn_filename="${ytn_file%.*}"
if [ ! -d "$GALLERY_THUMBNAILSDIR" ]; then
mkdir -p "$GALLERY_THUMBNAILSDIR"
fi
# download blazy.min.js
wget -q -O "$WORKDIR/${GALLERY_THUMBNAILSDIR}/blazy.min.js" https://github.com/centminmod/optimise-images/raw/master/js/blazy.min.js
#####################################################################
# X start
if [[ "$X_EXT" = 'jpg' ]] || [[ "$X_EXT" = 'jpeg' ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT ${CONVERT_BIN}${DEFINE_TMP} "${X}"${JPEGHINT_OPT}${IMAGICK_JPGOPTS}${INTERLACE_OPT}${STRIP_OPT} \
-thumbnail '240x240>' -unsharp 0x.5 "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT ${CONVERT_BIN}${DEFINE_TMP} "${X}"${JPEGHINT_OPT}${IMAGICK_JPGOPTS}${INTERLACE_OPT} \
-thumbnail '240x240>' -unsharp 0x.5 "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}"
elif [[ "$X_EXT" = 'png' ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT ${CONVERT_BIN}${DEFINE_TMP} "${X}"${INTERLACE_OPT}${IMAGICK_PNGOPTS} \
-thumbnail '240x240>' -unsharp 0x.5 "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT ${CONVERT_BIN}${DEFINE_TMP} "${X}"${INTERLACE_OPT}${IMAGICK_PNGOPTS} \
-thumbnail '240x240>' -unsharp 0x.5 "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}"
elif [[ "$X_EXT" = 'webp' ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT ${CONVERT_BIN}${DEFINE_TMP} "${X}"${INTERLACE_OPT}${IMAGICK_PNGOPTS} \
-thumbnail '240x240>' -unsharp 0x.5 "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT ${CONVERT_BIN}${DEFINE_TMP} "${X}"${INTERLACE_OPT}${IMAGICK_PNGOPTS} \
-thumbnail '240x240>' -unsharp 0x.5 "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}"
fi
if [[ "$GALLERY_THUMBNAILSCOMPRESS" = [yY] ]]; then
# for X optimise thumbnails
if [[ "$tn_extension" = 'png' ]]; then
if [[ "$OPTIPNG" = [yY] && "$ZOPFLIPNG" = [nN] ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT optipng -o${OPTIPNG_COMPRESSION} "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}" -preserve -out "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT optipng -o${OPTIPNG_COMPRESSION} "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}" -preserve -out "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}" 2>&1 | grep '^Output'
sar_call
elif [[ "$ZOPFLIPNG" = [yY] && "$OPTIPNG" = [nN] ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT zopflipng${ZOPFLIPNG_OPTS} "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}" "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT zopflipng${ZOPFLIPNG_OPTS} "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}" "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}"
sar_call
fi
elif [[ "$tn_extension" = 'jpg' || "$tn_extension" = 'jpeg' ]]; then
if [[ "$MOZJPEG" = [yY] && "$JPEGOPTIM" = [nN] && "$GUETZLI" = [nN] ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT $MOZJPEG_BIN"${MOZJPEG_QUALITY}" "$MOZJPEG_OPTS" -outfile "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}" "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT $MOZJPEG_BIN"${MOZJPEG_QUALITY}" "$MOZJPEG_OPTS" -outfile "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}" "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}"
sar_call
elif [[ "$MOZJPEG" = [nN] && "$JPEGOPTIM" = [yY] && "$GUETZLI" = [nN] ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT jpegoptim${JPEGOPTIM_PROGRESSIVE} -p --max="$THUMBNAILS_QUALITY" "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT jpegoptim${JPEGOPTIM_PROGRESSIVE} -p --max="$THUMBNAILS_QUALITY" "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}"
sar_call
elif [[ "$MOZJPEG" = [nN] && "$GUETZLI" = [yY] && "$JPEGOPTIM" = [nN] ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT $GUETZLI_BIN --quality "$GUETZLI_QUALITY" "$GUETZLI_OPTS" "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}" "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT $GUETZLI_BIN --quality "$GUETZLI_QUALITY" "$GUETZLI_OPTS" "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}" "${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}"
sar_call
fi
fi
fi
# X end
#####################################################################
#####################################################################
# Y start
if [[ "$Y_EXT" = 'jpg' ]] || [[ "$Y_EXT" = 'jpeg' ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT ${CONVERT_BIN}${DEFINE_TMP} "${Y}"${JPEGHINT_OPT}${IMAGICK_JPGOPTS}${INTERLACE_OPT} \
-thumbnail '240x240>' -unsharp 0x.5 "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT ${CONVERT_BIN}${DEFINE_TMP} "${Y}"${JPEGHINT_OPT}${IMAGICK_JPGOPTS}${INTERLACE_OPT} \
-thumbnail '240x240>' -unsharp 0x.5 "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}"
elif [[ "$Y_EXT" = 'png' ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT ${CONVERT_BIN}${DEFINE_TMP} "${Y}"${INTERLACE_OPT}${IMAGICK_PNGOPTS} \
-thumbnail '240x240>' -unsharp 0x.5 "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT ${CONVERT_BIN}${DEFINE_TMP} "${Y}"${INTERLACE_OPT}${IMAGICK_PNGOPTS} \
-thumbnail '240x240>' -unsharp 0x.5 "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}"
elif [[ "$Y_EXT" = 'webp' ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT ${CONVERT_BIN}${DEFINE_TMP} "${Y}"${INTERLACE_OPT}${IMAGICK_PNGOPTS} \
-thumbnail '240x240>' -unsharp 0x.5 "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT ${CONVERT_BIN}${DEFINE_TMP} "${Y}"${INTERLACE_OPT}${IMAGICK_PNGOPTS} \
-thumbnail '240x240>' -unsharp 0x.5 "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}"
fi
if [[ "$GALLERY_THUMBNAILSCOMPRESS" = [yY] ]]; then
# for Y optimise thumbnails
if [[ "$tn_extension" = 'png' ]]; then
if [[ "$OPTIPNG" = [yY] && "$ZOPFLIPNG" = [nN] ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT optipng -o${OPTIPNG_COMPRESSION} "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}" -preserve -out "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT optipng -o${OPTIPNG_COMPRESSION} "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}" -preserve -out "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}" 2>&1 | grep '^Output'
sar_call
elif [[ "$ZOPFLIPNG" = [yY] && "$OPTIPNG" = [nN] ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT zopflipng${ZOPFLIPNG_OPTS} "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}" "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT zopflipng${ZOPFLIPNG_OPTS} "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}" "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}"
sar_call
fi
elif [[ "$tn_extension" = 'jpg' || "$tn_extension" = 'jpeg' ]]; then
if [[ "$MOZJPEG" = [yY] && "$JPEGOPTIM" = [nN] && "$GUETZLI" = [nN] ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT $MOZJPEG_BIN"${MOZJPEG_QUALITY}" "$MOZJPEG_OPTS" -outfile "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}" "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT $MOZJPEG_BIN"${MOZJPEG_QUALITY}" "$MOZJPEG_OPTS" -outfile "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}" "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}"
sar_call
elif [[ "$MOZJPEG" = [nN] && "$JPEGOPTIM" = [yY] && "$GUETZLI" = [nN] ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT jpegoptim${JPEGOPTIM_PROGRESSIVE} -p --max="$THUMBNAILS_QUALITY" "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT jpegoptim${JPEGOPTIM_PROGRESSIVE} -p --max="$THUMBNAILS_QUALITY" "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}"
sar_call
elif [[ "$MOZJPEG" = [nN] && "$GUETZLI" = [yY] && "$JPEGOPTIM" = [nN] ]]; then
if [[ "$DEBUG" = [yY] ]]; then
echo "$NICE $NICEOPT $IONICE $IONICEOPT $GUETZLI_BIN --quality "$GUETZLI_QUALITY" "$GUETZLI_OPTS" "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}" "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}""
fi
$NICE $NICEOPT $IONICE $IONICEOPT $GUETZLI_BIN --quality "$GUETZLI_QUALITY" "$GUETZLI_OPTS" "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}" "${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}"
sar_call
fi
fi
fi
# Y end
#####################################################################
fi
# generate thumbnails for gallery end
echo "
<div class=\"col span_1_of_2\"><a href=\"$X\"> <img class=\"b-lazy\" src=\"data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"${GALLERY_THUMBNAILSDIR}/${xtn_filename}.${xtn_extension}\" alt=\"${LEFTLABEL} $X_DIMENS ($X_EXT $X_SIZE KB)\" width=\"240px\" /></a>
<p class=\"medium-font\">${xtn_filename}.${xtn_extension}<br>${LEFTLABEL} $X_DIMENS ($X_EXT $X_SIZE KB)</p></div>
<div class=\"col span_1_of_2\"><a href=\"$Y\"> <img class=\"b-lazy\" src=\"data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"${GALLERY_THUMBNAILSDIR}/${ytn_filename}.${ytn_extension}\" alt=\"webp $Y_DIMENS ($Y_EXT $Y_SIZE KB)\" width=\"240px\" /></a>
<p class=\"medium-font\">${ytn_filename}.${ytn_extension}<br>webp $Y_DIMENS ($Y_EXT $Y_SIZE KB)</p></div>" | tee -a "${WORKDIR}/gallery-webp.html"
done
echo " </div>" | tee -a "${WORKDIR}/gallery-webp.html"
echo "</div>" | tee -a "${WORKDIR}/gallery-webp.html"
echo "<script src=\"${GALLERY_THUMBNAILSDIR}/blazy.min.js\"></script>" | tee -a "${WORKDIR}/gallery-webp.html"
echo "<script> window.bLazy=new Blazy({ successClass: 'b-loaded',success:function(e){console.log(\"Element loaded: \",e.nodeName)}}); </script>" | tee -a "${WORKDIR}/gallery-webp.html"
echo " </body>" | tee -a "${WORKDIR}/gallery-webp.html"
echo " </html>" | tee -a "${WORKDIR}/gallery-webp.html"
}
genngx() {
WORKDIR=$1
NGXDIR=$(basename "$WORKDIR")
echo
echo "See https://centminmod.com/webp/ for more details"
echo "sample nginx vhost locaton context for conditional webp serving"
echo
echo "create /usr/local/nginx/conf/webp.conf and add to it:"
echo
echo "
map \$http_accept \$webpok {
default 0;
\"~*webp\" 1;
}
map \$http_cf_cache_status \$iscf {
default 1;
"" 0;
}
map \$webpok\$iscf \$webp_extension {
11 \"\";
10 \".webp\";
01 \"\";
00 \"\";
}"
echo
echo "add to your nginx.conf i.e. /usr/local/nginx/conf/nginx.conf and"
echo "include file for /usr/local/nginx/conf/webp.conf within the"
echo "http{} context location"
echo "
include /usr/local/nginx/conf/webp.conf;"
echo
echo "Then within your nginx vhost add or append/edit your location for"
echo
echo "
location /${NGXDIR} {
#pagespeed off;
autoindex on;
add_header X-Robots-Tag \"noindex, nofollow\";
location ~* ^/${NGXDIR}/.+\.(png|jpe?g)\$ {
expires 30d;
add_header Vary \"Accept-Encoding\";
add_header Cache-Control \"public, no-transform\";
try_files \$uri\$webp_extension \$uri =404;
}
}"
}
testfiles() {
WORKDIR=$1
echo "Downloading sample image files"
echo "to $WORKDIR"
cd "$WORKDIR"