[pacman-dev] [PATCH v3 2/2] add option to optimize PNG images with optipng

Christian Hesse list at eworm.de
Mon Jan 26 10:25:23 UTC 2015


From: Christian Hesse <mail at eworm.de>

This can decrease package size by optimizing PNG image size. Images are
just stored with better compression and/or filter options. The actual
image content is not altered.

Additionally this can automatically fix broken PNG images which caused
some trouble lately.

Signed-off-by: Christian Hesse <mail at eworm.de>
---
 contrib/PKGBUILD.vim   |  2 +-
 doc/PKGBUILD.5.txt     |  3 +++
 doc/makepkg.conf.5.txt |  6 +++++-
 etc/makepkg.conf.in    |  5 +++--
 scripts/makepkg.sh.in  | 21 ++++++++++++++++++++-
 5 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/contrib/PKGBUILD.vim b/contrib/PKGBUILD.vim
index 9157063..4d520ac 100644
--- a/contrib/PKGBUILD.vim
+++ b/contrib/PKGBUILD.vim
@@ -211,7 +211,7 @@ hi def link pbValidSha512sums  Number
 
 " options
 syn keyword pb_k_options options contained
-syn match pbOptions /\(no\)\?\(strip\|docs\|libtool\|emptydirs\|zipman\|purge\|upx\|fakeroot\|distcc\|color\|ccache\|check\|sign\|makeflags\|buildflags\)/ contained
+syn match pbOptions /\(no\)\?\(strip\|docs\|libtool\|emptydirs\|zipman\|purge\|upx\|optipng\|fakeroot\|distcc\|color\|ccache\|check\|sign\|makeflags\|buildflags\)/ contained
 syn match   pbOptionsNeg     /\!/ contained
 syn match   pbOptionsDeprec  /no/ contained
 syn region pbOptionsGroup start=/^options=(/ end=/)/ contains=pb_k_options,pbOptions,pbOptionsNeg,pbOptionsDeprec,pbIllegalOption
diff --git a/doc/PKGBUILD.5.txt b/doc/PKGBUILD.5.txt
index c95c41d..10980ab 100644
--- a/doc/PKGBUILD.5.txt
+++ b/doc/PKGBUILD.5.txt
@@ -296,6 +296,9 @@ underscore and the architecture name e.g., 'replaces_x86_64=()'.
 	*upx*;;
 		Compress binary executable files using UPX.
 
+	*optipng*;;
+		Optimize PNG images with optipng.
+
 	*ccache*;;
 		Allow the use of ccache during build. More useful in its negative
 		form `!ccache` with select packages that have problems building
diff --git a/doc/makepkg.conf.5.txt b/doc/makepkg.conf.5.txt
index 5e34b91..ac5791f 100644
--- a/doc/makepkg.conf.5.txt
+++ b/doc/makepkg.conf.5.txt
@@ -140,7 +140,7 @@ Options
 	Specify a key to use for GPG signing instead of the default key in the
 	keyring. Can be overridden with makepkg's '\--key' option.
 
-**OPTIONS=(**strip docs libtool staticlibs emptydirs zipman purge !upx**)**::
+**OPTIONS=(**strip docs libtool staticlibs emptydirs zipman purge !upx !optipng**)**::
 	This array contains options that affect default packaging. They are
 	equivalent to options that can be placed in the PKGBUILD; the defaults are
 	shown here. All options should always be left in the array; to enable or
@@ -181,6 +181,10 @@ Options
 		Compress binary executable files using UPX. Additional options
 		can be passed to UPX by specifying the `UPXFLAGS` array variable.
 
+	*optipng*;;
+		Optimize PNG images with optipng. Additional options can be passed
+		to optipng by specifying the `OPTIPNGFLAGS` array variable.
+
 	*debug*;;
 		Add the user-specified debug flags as specified in DEBUG_CFLAGS and
 		DEBUG_CXXFLAGS to their counterpart buildflags. Creates a separate
diff --git a/etc/makepkg.conf.in b/etc/makepkg.conf.in
index 19f673d..71ec624 100644
--- a/etc/makepkg.conf.in
+++ b/etc/makepkg.conf.in
@@ -71,7 +71,7 @@ BUILDENV=(!distcc color !ccache check !sign)
 #   These are default values for the options=() settings
 #########################################################################
 #
-# Default: OPTIONS=(strip docs libtool staticlibs emptydirs zipman purge !upx !debug)
+# Default: OPTIONS=(strip docs libtool staticlibs emptydirs zipman purge !upx !optipng !debug)
 #  A negated option will do the opposite of the comments below.
 #
 #-- strip:      Strip symbols from binaries/libraries
@@ -82,9 +82,10 @@ BUILDENV=(!distcc color !ccache check !sign)
 #-- zipman:     Compress manual (man and info) pages in MAN_DIRS with gzip
 #-- purge:      Remove files specified by PURGE_TARGETS
 #-- upx:        Compress binary executable files using UPX
+#-- optipng:    Optimize PNG images with optipng
 #-- debug:      Add debugging flags as specified in DEBUG_* variables
 #
-OPTIONS=(strip docs libtool staticlibs emptydirs zipman purge !upx !debug)
+OPTIONS=(strip docs libtool staticlibs emptydirs zipman purge !upx !optipng !debug)
 
 #-- File integrity checks to use. Valid: md5, sha1, sha256, sha384, sha512
 INTEGRITY_CHECK=(md5)
diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in
index 72f5b04..ee940e5 100644
--- a/scripts/makepkg.sh.in
+++ b/scripts/makepkg.sh.in
@@ -49,7 +49,7 @@ declare -r startdir="$PWD"
 LIBRARY=${LIBRARY:-'@libmakepkgdir@'}
 
 packaging_options=('strip' 'docs' 'libtool' 'staticlibs' 'emptydirs' 'zipman'
-                   'purge' 'upx' 'debug')
+                   'purge' 'upx' 'optipng' 'debug')
 other_options=('ccache' 'distcc' 'buildflags' 'makeflags')
 splitpkg_overrides=('pkgdesc' 'arch' 'url' 'license' 'groups' 'depends'
                     'optdepends' 'provides' 'conflicts' 'replaces' 'backup'
@@ -1918,6 +1918,17 @@ tidy_install() {
 			fi
 		done
 	fi
+
+	if check_option "optipng" "y"; then
+		msg2 "$(gettext "Optimizing PNG images...")"
+		local png
+		find . -type f -iname "*.png" 2>/dev/null | while read -r png ; do
+			if [[ $(file --brief --mime-type "$png") = 'image/png' ]]; then
+				optipng "${OPTIPNGFLAGS[@]}" "$png" &>/dev/null ||
+					warning "$(gettext "Could not optimize PNG image : %s")" "${png/$pkgdir\//}"
+			fi
+		done
+	fi
 }
 
 find_libdepends() {
@@ -3008,6 +3019,14 @@ check_software() {
 		fi
 	fi
 
+	# optipng - PNG image optimization
+	if check_option "optipng" "y"; then
+		if ! type -p optipng >/dev/null; then
+			error "$(gettext "Cannot find the %s binary required for optimizing PNG images.")" "optipng"
+			ret=1
+		fi
+	fi
+
 	# distcc - compilation with distcc
 	if check_buildenv "distcc" "y" && ! check_option "distcc" "n"; then
 		if ! type -p distcc >/dev/null; then
-- 
2.2.2


More information about the pacman-dev mailing list