Compare commits
10 commits
250dc2d0bf
...
0326837a62
Author | SHA1 | Date | |
---|---|---|---|
|
0326837a62 | ||
|
1b406e97bd | ||
|
2885143d58 | ||
|
31bf102452 | ||
|
973cfabe69 | ||
|
f77a385101 | ||
|
63ee7ac32d | ||
|
555567e321 | ||
|
188de5661b | ||
|
decebd46b1 |
3 changed files with 64 additions and 17 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,5 @@
|
|||
*~
|
||||
\#*
|
||||
.\#*
|
||||
*.swp
|
||||
*.vcf
|
||||
|
|
17
README.md
Normal file
17
README.md
Normal file
|
@ -0,0 +1,17 @@
|
|||
# VCF Compressor
|
||||
|
||||
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)
|
|
@ -29,21 +29,46 @@ usage () {
|
|||
echo ""
|
||||
echo "Options:"
|
||||
echo " -o, --output"
|
||||
echo " -v, --verbose"
|
||||
echo " -V, --version"
|
||||
echo " --version"
|
||||
echo " -h, --help"
|
||||
exit "$exit_status"
|
||||
}
|
||||
|
||||
echo_verbose () {
|
||||
if [ $verbose -gt 0 ]; then
|
||||
echo "$@"
|
||||
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)
|
||||
width=$(raw_img "$line" | identify -format '%w' -)
|
||||
height=$(raw_img "$line" | identify -format '%h' -)
|
||||
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
|
||||
echo "$line"
|
||||
fi
|
||||
}
|
||||
|
||||
process_line () {
|
||||
line="$1"
|
||||
|
||||
echo "$line" | egrep "^PHOTO;" | grep ";ENCODING=b;" >/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,11 +82,7 @@ while :; do
|
|||
usage
|
||||
fi
|
||||
;;
|
||||
-v|--verbose)
|
||||
verbose=1
|
||||
shift
|
||||
;;
|
||||
-V|--version)
|
||||
--version)
|
||||
version
|
||||
;;
|
||||
-h|--help)
|
||||
|
@ -79,12 +100,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"
|
||||
|
|
Reference in a new issue