Created
May 9, 2018 01:12
-
-
Save dailenspencer/f2e2358577b3b73656493a3f3b785f35 to your computer and use it in GitHub Desktop.
Compress Media Folder Bash Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# OVERVIEW | |
# This bash script handles the organization and compression for blog media items winthin the app | |
# when a directory is suppied to the script, the script loops through the directory's items and handles the | |
# renaming and compression accordingly. Currently, there is only one type of media item that is handled by the script | |
# -- image items - .JPG, .jpg | |
targetedFolder=$1 | |
# Ensure argument is a valid directory | |
if [ -z "$targetedFolder" ]; then | |
echo "ERROR: the compress-media-folder requires a valid directory as the first argument" | |
exit 1 | |
elif [ ! -d "$targetedFolder" ]; then | |
echo "ERROR: the supplied argument is not a valid directory" | |
exit 1 | |
fi | |
# Move into supplied directory | |
echo "moving into supplied directory" | |
cd $targetedFolder | |
# ensure items are present inside supplied directory | |
imageCount=0; | |
shopt -s nullglob | |
for image in ./*.{jpg,JPG}; do | |
$((imageCount++)) | |
done; | |
if [ $imageCount == 0 ]; then | |
echo "ERROR: no images found in the supplied directory with the extensions: .jpg, .JPG" | |
exit 1; | |
fi | |
echo "Found $imageCount .jpg images to compress" | |
# function to calculate the index of the last renamed image | |
function calculate_last_renamed_image_index { | |
lastRenamedImageIndex=0 | |
shopt -s nullglob | |
for image in ./*.{jpg,JPG}; do | |
if is_image_renamed $image; then | |
$((lastRenamedImageIndex++)) | |
fi | |
done; | |
return $lastRenamedImageIndex | |
} | |
# function to check if image is specified as a cover image | |
# will return truthy value if image has -cover extension attached | |
# will return falsey value if image does not have -cover extension attached | |
function is_image_cover { | |
image=$1 | |
#check if image has already been renamed(if it fits regex "<parent folder name>_<media type>_<last index of all renamed pics>") | |
COVER_IMAGE_REGEX='(-cover)' | |
if [[ "$image" =~ $COVER_IMAGE_REGEX ]]; then | |
return | |
fi | |
false | |
} | |
# function to check if image has been renamed | |
# will return truthy value if image has already been re-named | |
# will return falsey value if image has not been re-named | |
function is_image_renamed { | |
image=$1 | |
parentFolder=$(echo "$PWD" | sed -E 's/\/[A-Za-z0-9/]+\/([A-Za-z0-9\-]+)$/\1/') | |
#check if image has already been renamed(if it fits regex "<parent folder name>_<media type>_<last index of all renamed pics>") | |
RENAMED_REGEX="(" + parentFolder + ")_(image)_[0-9]+" | |
if [[ "$image" =~ $RENAMED_REGEX ]]; then | |
return | |
fi | |
false | |
} | |
# function to handle renaming of an image | |
# will return truthy value if image is re-named by function | |
# will return falsey value if the image has already previously been re-named | |
function rename_image { | |
image=$1 | |
# if image has already been renamed, return with falsey value | |
if is_image_renamed $image; then | |
echo "$image has been renamed already" | |
false | |
return | |
fi | |
echo "$image has not been renamed, renaming..." | |
lastRenamedImageIndex=$2 | |
parentFolder=$(echo "$PWD" | sed -E 's/\/[A-Za-z0-9/]+\/([A-Za-z0-9\-]+)$/\1/') | |
mediaType='image' | |
renamedImage="$parentFolder"_"$mediaType"_"$lastRenamedImageIndex" | |
echo $parentFolder | |
# if image is specified as a cover image, add cover extension | |
if is_image_cover $image; then | |
renamedImage+="-cover" | |
fi | |
# if image has been compressed, add compression extension | |
if is_image_compressed $image; then | |
renamedImage+="-compressed" | |
fi | |
renamedImage+=".jpg" | |
echo "$image" has been renamed to "$renamedImage" | |
mv $image $renamedImage | |
return | |
} | |
# function to check if image has been compressed | |
# will return truthy value if image has already been compressed | |
# will return falsey value if image has not already been compressed | |
function is_image_compressed { | |
image=$1 | |
COMPRESSED_REGEX='(-compressed)' | |
if [[ "$image" =~ $COMPRESSED_REGEX ]]; then | |
return | |
fi | |
false | |
} | |
# function to handle conversion, resize, and compression of an image | |
# will return truthy value if image is compressed by function | |
# will return falsey value if image has not already previously been compressed | |
function compress_image { | |
image=$1 | |
fileName=${image%.*} | |
fileExtension=${image##*.} | |
# if image has been compressed, return | |
if is_image_compressed $image; then | |
echo "$image has been compressed already" | |
false | |
return | |
fi | |
echo "$image has not been compressed already, compressing..." | |
# re-size image (re-size dimensions are based on whether the file is a cover image or not) | |
if is_image_cover $image; then | |
magick $image -resize 2200x1800 $image | |
else | |
magick $image -resize 1500x1200 $image | |
fi | |
# compress image - this outputs a new image with the -compressed extension | |
# NOTE: commenting jpegtran out as it doesnt let you specify image quality. Using cjpeg instead | |
# jpegtran -outfile "$fileName"-compressed."$fileExtension" -quality 85 -optimise -copy none $image | |
cjpeg -outfile "$fileName"-compressed."$fileExtension" -quality 85 -optimise $image | |
# remove un-compressed image | |
rm $image | |
} | |
calculate_last_renamed_image_index | |
lastRenamedImageIndex=$? | |
# Loop through image items in folder and rename/compress | |
shopt -s nullglob | |
for image in ./*.{jpg,JPG,jpeg}; do | |
echo $image | |
# call re-name image func., and if the func .returns 1 (false), this means the image has already been re-named | |
# if the func. returns 0 (true), this means the image has not been re-named and we need to increment the lastRenamedImageIndex | |
rename_image $image $lastRenamedImageIndex | |
if [[ ! "$renamedImage" == "" ]]; then | |
$((lastRenamedImageIndex++)) | |
compress_image $renamedImage | |
else | |
compress_image $image | |
fi | |
done; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment