Skip to content

Instantly share code, notes, and snippets.

@caubry
Last active December 13, 2023 14:50
Show Gist options
  • Select an option

  • Save caubry/7103438 to your computer and use it in GitHub Desktop.

Select an option

Save caubry/7103438 to your computer and use it in GitHub Desktop.
Bash script using pngquant and jpegoptim library to compress PNG, JPG and JPEG, with batch compression as an option.
#!bin/bash
pwd=`pwd`
echo -n "Please enter the file path: ";
read filePath;
if [ ! -f "$filePath" ]
then
echo "File "$filePath" not found!";
exit;
fi
imageType="${filePath##*.}";
lengthPath="${#filePath}";
originalName="${filePath:0:($lengthPath-4)}";
pause()
{
read -p "$*";
}
install_pngquant()
{
cd ~/Documents/;
git clone git://github.com/pornel/pngquant.git;
if [[ -d 'pngquant' ]]
then
cd pngquant;
make -s;
make -s install;
cd ..;
rm -rf ~/Documents/pngquant;
command -v pngquant >/dev/null 2>&1 ||
{
echo "Something went horribly wrong and pngquant hasn't been installed! (╯°□°)╯︵ ┻━┻";
exit;
}
echo -e "********************************** \n******* Installation done! ******* \n********************************** \n";
pause 'Press [Enter] key to compress your image...';
run_pngquant;
else
echo "Couldn't clone pngquant git repo. \nDo you have an Internet access?";
exit;
fi
}
run_pngquant()
{
command -v pngquant >/dev/null 2>&1 ||
{
echo -e >&2 "Pngquant command line tool hasn't been installed.";
while true; do
read -p "Do you wish to install this program (Y/N)? " yn
case $yn in
[Yy]* ) install_pngquant;;
[Nn]* ) echo "End"; exit;;
* ) echo "Please answer Yes or No.";;
esac
done
exit;
}
pngquant -vf "$filePath" --ext "$fileExtension";
imageOutput="${originalName}$fileExtension";
outputSize=$(du -h "$imageOutput");
echo -e "\xF0\x9f\x8d\xba Compression done! \nOutput: $outputSize";
exit;
}
install_libjpeg()
{
cd /usr/local/include/;
if [[ -f 'jpeglib.h' ]]
then
install_jpegoptim;
else
cd ~/Documents/;
curl -O http://www.ijg.org/files/jpegsrc.v8c.tar.gz
tar -xf jpegsrc.v8c.tar.gz
rm jpegsrc.v8c.tar.gz
if [[ -d 'jpeg-8c' ]]
then
cd jpeg-8c/;
./configure;
make;
make -s install;
cd ..;
rm -rf jpeg-8c/;
pause 'Press [Enter] key to install jpegoptim...';
install_jpegoptim;
else
echo -e "Something went horribly wrong and libjpeg hasn't been installed! (╯°□°)╯︵ ┻━┻ \nDo you have Internet access?";
exit;
fi
fi
}
install_jpegoptim()
{
cd ~/Documents/;
git clone https://github.com/tjko/jpegoptim.git;
if [[ -d 'jpegoptim' ]]
then
cd jpegoptim;
./configure;
make -s;
make -s install;
cd ..;
rm -rf ~/Documents/jpegoptim;
command -v jpegoptim >/dev/null 2>&1 ||
{
echo "Something went horribly wrong and jpegoptim hasn't been installed! (╯°□°)╯︵ ┻━┻";
exit;
}
echo -e "********************************** \n******* Installation done! ******* \n********************************** \n";
pause 'Press [Enter] key to compress your image...';
run_jpegoptim;
else
echo -e "Couldn't get jpegoptim. \nDo you have Internet access?";
exit;
fi
}
run_jpegoptim_twenty_kb()
{
make_image_copy;
twenty_kb=20000;
copy_ext="_copy";
copyTempFilePath=${tempFilePath}$copy_ext;
cp $tempFilePath $copyTempFilePath;
while [ $twenty_kb -le $outputSize -a $qualityCompression -gt 20 ]
do
cp $copyTempFilePath $tempFilePath;
qualityCompression=$((qualityCompression-1));
jpegoptim -v --max=$qualityCompression "$tempFilePath";
outputSize=$(ls -l "$tempFilePath" | awk '{print $5}');
done
if $overwrite
then
extension=$fileExtension;
else
extension=$imageType;
fi
rm $copyTempFilePath;
mv $tempFilePath ${originalName}$extension;
echo -e "\xF0\x9f\x8d\xba Compression done! \nOutput: ${originalName}$extension, $outputSize";
remove_temp_folder;
exit;
}
run_jpegoptim_quality()
{
if $overwrite
then
make_image_copy;
currentFile=$tempFilePath;
extension=$fileExtension;
else
currentFile=$filePath;
extension=$imageType;
fi
jpegoptim -v --max=$qualityCompression "$currentFile";
mv $currentFile ${originalName}$extension;
echo -e "\xF0\x9f\x8d\xba Compression done!";
remove_temp_folder;
exit;
}
make_image_copy()
{
temp="/temp";
mkdir ${pwd}$temp;
tempName="/_temp";
tempDir=${pwd}$temp;
cp $filePath ${tempDir}$tempName;
tempFilePath=${tempDir}$tempName;
}
remove_temp_folder()
{
rm -rf ${pwd}$temp;
}
run_jpegoptim_command()
{
while true; do
read -p "Do you wish to overwrite your image (Y/N)? " yn
case $yn in
[Yy]* ) overwrite=false; break;;
[Nn]* ) overwrite=true; break;;
* ) echo "Please answer Yes or No.";;
esac
done
if $once
then
while true; do
read -p "Please enter the maximum image quality factor [0-100]: " qualityCompression;
case $qualityCompression in
[0-99]*) run_jpegoptim_quality;;
* ) echo "$qualityCompression, Not a number between 0 and 100.";;
esac
done
else
qualityCompression=80;
jpegoptim -v --max=$qualityCompression "$filePath";
outputSize=$(ls -l "$filePath" | awk '{print $5}');
run_jpegoptim_twenty_kb;
fi
}
run_jpegoptim()
{
command -v jpegoptim >/dev/null 2>&1 ||
{
echo -e >&2 "Jpegoptim hasn't been installed.";
while true; do
read -p "Do you wish to install this program (Y/N)? " yn
case $yn in
[Yy]* ) install_libjpeg;;
[Nn]* ) echo "End"; exit;;
* ) echo "Please answer Yes or No.";;
esac
done
exit;
}
while true; do
read -p "Do you need this image to be under 20kb (Y/N): " yn;
case $yn in
[Yy]* ) once=false; run_jpegoptim_command $once; break;;
[Nn]* ) once=true; run_jpegoptim_command $once; break;;
* ) echo "Please answer Yes or No.";;
esac
done
}
customExt="_MINI";
pngExt="png";
jpgExt="jpg";
jpegExt="jpeg";
case ${imageType:(-1)} in
[A-Z]*) upperLetter=true;;
[a-z]*) upperLetter=false;;
esac
if $upperLetter
then
fileExtension="${customExt}".$imageType"";
lowerCaseLetters=$(echo $imageType | awk '{print tolower($0)}');
imageType="$lowerCaseLetters";
else
imageType="$imageType";
fileExtension="${customExt}".$imageType"";
fi
if [[ "$imageType" == *$pngExt* ]]
then
run_pngquant;
exit;
elif [[ "$imageType" == *$jpgExt* ]] || [[ "$imageType" == *$jpegExt* ]]
then
run_jpegoptim;
exit;
else
echo "Compression works with PNG, JPG and JPEG only.";
exit;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment