From decebd46b1b8c993d12c9db27f24f760d94bf175 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 5 Sep 2015 21:25:13 +0200 Subject: [PATCH 01/10] Compressing images using imagemagick. --- vcf_compressor.sh | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/vcf_compressor.sh b/vcf_compressor.sh index 3ddc164..492040c 100755 --- a/vcf_compressor.sh +++ b/vcf_compressor.sh @@ -29,21 +29,32 @@ usage () { echo "" echo "Options:" echo " -o, --output" - echo " -v, --verbose" echo " -V, --version" echo " -h, --help" exit "$exit_status" } -echo_verbose () { - if [ $verbose -gt 0 ]; then - echo "$@" +compress_photo () { + line="$1" + + prefix=$(echo "$line" | cut -d ":" -f1) + picture=$(echo "$line" | cut -d ":" -f2 | base64 -d | convert "-" -quality 50 -resize "300x300>" "-" | base64 -w0) + echo "$prefix:$picture" +} + +process_line () { + line="$1" + + echo "$line" | egrep "^PHOTO;" >/dev/null 2>&1 + if [ $? -eq 0 ]; then + compress_photo "$line" + else + echo "$line" fi } input_file="" output_file="" -verbose=0 while :; do test $# -gt 0 || break @@ -57,10 +68,6 @@ while :; do usage fi ;; - -v|--verbose) - verbose=1 - shift - ;; -V|--version) version ;; @@ -79,12 +86,19 @@ while :; do esac done test -z "$input_file" && usage +if [ ! -f "$input_file" ]; then + echo "$input_file: file not found" + exit 1 +fi if [ -z "$output_file" ]; then dir=$(dirname "$input_file") name=$(basename "$input_file" ".vcf") output_file="$dir/$name.new.vcf" fi -echo "in: $input_file" -echo "out: $output_file" -echo_verbose "verbose mode on" +echo -n > "$output_file" + +while IFS='' read -r line || [[ -n "$line" ]]; do + line=$(process_line "$line") + echo "$line" >> "$output_file" +done < "$input_file" From 188de5661b1b59920e3c776b96b1a46b7604d4d8 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 5 Sep 2015 21:38:18 +0200 Subject: [PATCH 02/10] Removing the useless -V alias. --- vcf_compressor.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vcf_compressor.sh b/vcf_compressor.sh index 492040c..6c5c311 100755 --- a/vcf_compressor.sh +++ b/vcf_compressor.sh @@ -29,7 +29,7 @@ usage () { echo "" echo "Options:" echo " -o, --output" - echo " -V, --version" + echo " --version" echo " -h, --help" exit "$exit_status" } @@ -68,7 +68,7 @@ while :; do usage fi ;; - -V|--version) + --version) version ;; -h|--help) From 555567e3216de5efe288094c4638222cbe1fc9e8 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 5 Sep 2015 21:38:54 +0200 Subject: [PATCH 03/10] Ignoring test files. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f418d8d..4955756 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *~ \#* .\#* +*.vcf From 63ee7ac32d2bd669d0c28cd32bed1ddcf7209279 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 5 Sep 2015 21:39:33 +0200 Subject: [PATCH 04/10] Adding a minimalistic readme. --- README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..404fecb --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# VCF Compressor + +A simple bash script that compress images inside a vCard. From f77a38510197d0bf57a22754dec5bf3010dccb49 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 5 Sep 2015 22:15:31 +0200 Subject: [PATCH 05/10] Ignoring photo URLs. Photos inserted as an URL instead of being base64 encoded should not be compressed. --- vcf_compressor.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vcf_compressor.sh b/vcf_compressor.sh index 6c5c311..1634ba4 100755 --- a/vcf_compressor.sh +++ b/vcf_compressor.sh @@ -45,7 +45,7 @@ compress_photo () { process_line () { line="$1" - echo "$line" | egrep "^PHOTO;" >/dev/null 2>&1 + echo "$line" | egrep "^PHOTO;" | grep ";ENCODING=b;" >/dev/null 2>&1 if [ $? -eq 0 ]; then compress_photo "$line" else From 973cfabe69db1fba82e0ffb60d40821a361b4c5d Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 5 Sep 2015 22:17:49 +0200 Subject: [PATCH 06/10] Improving the README. It's still too simplistic, but the following items have to be here: - specifying the vCard version supported ; - specifying the requirements ; - links to the specifications and useful stuff. --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 404fecb..e4e6a59 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,17 @@ # VCF Compressor -A simple bash script that compress images inside a vCard. +A simple bash script that compress images inside a vCard 3.0. + + +## Requirements + +* bash +* imagemagick + + +## Further reading + +* [vCard on Wikipedia](https://en.wikipedia.org/wiki/VCard) +* vCard 3.0: [RFC 2425](https://tools.ietf.org/html/rfc2425) and [RFC 2426](https://tools.ietf.org/html/rfc2426) +* vCard 4.0: [RFC 6350](https://tools.ietf.org/html/rfc6350) +* CardDAV: [RFC 6352](https://tools.ietf.org/html/rfc6352) From 31bf10245282d24870ddb170f653dd1c12c5887d Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 12 Sep 2015 18:43:06 +0200 Subject: [PATCH 07/10] Do not compress small images. Before this commit, every images were reprocessed all of the time. Because the address book may be re-compressed when new pictures are added, we want to process them and not reprocess the previous ones. This commit include a size detection so images below the size limit are not processed. --- vcf_compressor.sh | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/vcf_compressor.sh b/vcf_compressor.sh index 1634ba4..fa2f16d 100755 --- a/vcf_compressor.sh +++ b/vcf_compressor.sh @@ -34,12 +34,26 @@ usage () { exit "$exit_status" } -compress_photo () { +raw_img () { line="$1" + echo "$line" | cut -d ":" -f2 | base64 -d +} + +compress_photo () { + line="$1" + max_width="300" + max_height="300" + prefix=$(echo "$line" | cut -d ":" -f1) - picture=$(echo "$line" | cut -d ":" -f2 | base64 -d | convert "-" -quality 50 -resize "300x300>" "-" | base64 -w0) - echo "$prefix:$picture" + width=$(raw_img "$line" | identify -format '%w' -) + height=$(raw_img "$line" | identify -format '%h' -) + if [ "$width" -le "$max_width" ] && [ "$height" -le "$max_height" ] ; then + picture=$(raw_img "$line" | convert "-" -quality 50 -resize "${max_width}x${max_height}>" "-" | base64 -w0) + echo "$prefix:$picture" + else + echo "$line" + fi } process_line () { From 2885143d584df0d8a1c88e8d923070d381351e7a Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 12 Sep 2015 19:03:38 +0200 Subject: [PATCH 08/10] Fixing a size detection bug. The conditions used to determine whether or not an image have to be processed depending on its size were boggus. This commit fixes the issue, images are now processed as they should. --- vcf_compressor.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vcf_compressor.sh b/vcf_compressor.sh index fa2f16d..6b4df2e 100755 --- a/vcf_compressor.sh +++ b/vcf_compressor.sh @@ -48,7 +48,7 @@ compress_photo () { prefix=$(echo "$line" | cut -d ":" -f1) width=$(raw_img "$line" | identify -format '%w' -) height=$(raw_img "$line" | identify -format '%h' -) - if [ "$width" -le "$max_width" ] && [ "$height" -le "$max_height" ] ; then + if [ "$width" -gt "$max_width" ] || [ "$height" -gt "$max_height" ] ; then picture=$(raw_img "$line" | convert "-" -quality 50 -resize "${max_width}x${max_height}>" "-" | base64 -w0) echo "$prefix:$picture" else From 1b406e97bd03782464025fda828ba972ab465d30 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Wed, 4 Nov 2015 20:43:26 +0100 Subject: [PATCH 09/10] Ignoring vim swapfiles. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4955756..3d981c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *~ \#* .\#* +*.swp *.vcf From 0326837a62bc5e82a0c18b27783e0e8221c32208 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Wed, 4 Nov 2015 20:43:49 +0100 Subject: [PATCH 10/10] Removing trailing spaces. --- vcf_compressor.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vcf_compressor.sh b/vcf_compressor.sh index 6b4df2e..99ff3ae 100755 --- a/vcf_compressor.sh +++ b/vcf_compressor.sh @@ -1,16 +1,16 @@ #!/bin/bash ## Copyright (c) 2015 Rodolphe Breard -## +## ## This program is free software: you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation, either version 3 of the License, or ## (at your option) any later version. -## +## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. -## +## ## You should have received a copy of the GNU General Public License ## along with this program. If not, see . ##