Skip to content

Instantly share code, notes, and snippets.

@jjaimealeman
Last active March 25, 2024 06:44
Show Gist options
  • Save jjaimealeman/23b4948fd3ad029bf28bb04f5d630af0 to your computer and use it in GitHub Desktop.
Save jjaimealeman/23b4948fd3ad029bf28bb04f5d630af0 to your computer and use it in GitHub Desktop.
My simple Unsplash image downloader scripts with width & height options.

Tuesday, April 26, 2022

https://gist.github.com/jjaimealeman/23b4948fd3ad029bf28bb04f5d630af0

During the development of a static website, I often need to use an image from Unsplash.

It's time-consuming to browse the site and search for something, and then download, resize and move into my working folder. I found this npm package https://github.com/MehediH/Bulksplash but it didn't work as I was hoping.

In my research I found this site https://picsum.photos/ that provides direct downloads from Unsplash with a set width and height. and It gave me an idea!

So I decided to write my own :)

This simple script gives options for width and height, from 100px to 900px, and the final option is for fullsize of 1980x1080.


Files:

I came up with two scripts:

  • ./funsplash - gives user a list of size options ranging from 100 to 900.

screenshot_funsplash

  • ./funsplash2 - allow user to send two arguments inline. Ex: ./funsplash2 500 300 will download an image with dimensions of 500x300 screenshot_funsplash2

To Do:

  • Make script to run inline with arguments for dimensions.
  • Provide additional option to download multiple images.
  • Make a JavaScript version? 🤔
  • Make a VS Code extension? 🤔
#!/bin/bash
# funsplash.sh
funsplash() {
clear
printf -- "\n ========================================\n"
printf -- "\n This script presents you with 2 options:\n"
printf -- "\n 1. Choose the width of your pic.\n"
printf -- "\n 2. Choose the height of your pic.\n"
printf -- "\n ----------------------------------------\n"
read -n 1 -s -r -p " Press the 'ANY' key to continue - LOL!!!"
sleep 3
clear
printf -- "\n =========================================\n"
printf -- "\n Choose the width of your pic.\n"
declare -a w_options=("100" "200" "300" "400" "500" "600" "700" "800" "900" "1000" "1920")
select width in "${w_options[@]}"; do
if [[ " ${w_options[*]} " == *" ${width} "* ]]; then
printf -- "Setting width to %s\n" "${width}"
else
printf -- "%s is not a valid option.\n" "${REPLY}"
return 1
fi
break
done
clear
printf -- "\n =========================================\n"
printf -- "\n Choose the height of your pic.\n"
declare -a h_options=("100" "200" "300" "400" "500" "600" "700" "800" "900" "1000" "1080")
select height in "${h_options[@]}"; do
if [[ " ${h_options[*]} " == *" ${height} "* ]]; then
printf -- " Setting height to %s\n" "${height}"
else
printf -- " %s is not a valid option.\n" "${REPLY}"
return 1
fi
break
done
clear
declare dimensions="${width}/${height}"
declare filename; filename=funsplash-$(tr -dc A-Za-z0-9 </dev/random | head -c 10 ; echo '')
declare https="https://unsplash.it"; declare random="?random"
mkdir -p "${width}/${height}"
printf -- "\n =========================================\n"
printf -- "\n Downloading: %s%s%s\n" "${https}/" "${dimensions}/" "${random}"
wget -q -O "${width}/${height}/${filename}.jpg" "${https}/${dimensions}/${random}"
sleep 1
printf -- "\n Your download is ready.\n"
printf -- "\n Location: %s%s%s.jpg\n" "$(pwd)" "/${dimensions}/" "${filename}"
sleep 1
printf -- "\n ----------------------------------------\n"
printf -- "\n Thanks for using funsplash."
}
funsplash
# I KNOW THERE ARE MANY WAYS OF GENERATING A HASH, HERE ARE OTHERS
# $ cat /proc/sys/kernel/random/uuid | sed 's/[-]//g' | head -c 10; echo
# └─> 1eaf38dd88
# └─> ed9b6ce52c
# └─> 3d4a4da3e1
# └─> 92a998e110
# $ tr -dc a-f0-9 < /dev/urandom | dd bs=10 count=1 2> /dev/null; echo
# └─> 73d22b8da8
# └─> 8111a8f0d4
# └─> 6d64f1c274
# └─> 69d2c7e437
# $ tr -dc A-Za-z0-9 </dev/urandom | head -c 10 ; echo
# └─> UyEYp2IAuX
# └─> hDmPghE0pJ
# └─> lLicTDmxfx
# └─> Lnml4asaYc
# $ cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1
# └─> mXsgotUkxx
# └─> qH2XKTIJ2j
# └─> GxVzTnWr5W
# └─> aslqZq7msQ
# $ date +%s | sha256sum | base64 | head -c 10 ; echo
# └─> NzExYjJhMD
# └─> MDY0MDMwYT
# └─> ZmYwMzMwZT
# └─> ZjZkODg2Zm
# THIS IS THE VERY FIRST VERSION OF MY SCRIPT.
# IT OFFERS NO OPTIONS, JUST DOWNLOADS A RANDOM IMAGE AND RESIZES TO 450px and 800px
# THE NEW VERSION PROVIDES OPTIONS FOR HEIGHT AND WIDTH.
# function unsplash {
# local FILENAME=unsplash-$(cat /proc/sys/kernel/random/uuid | sed 's/[-]//g' | head -c 10;echo)
# wget -q -O $FILENAME.jpg https://unsplash.it/1920/1080/?random
# echo $FILENAME.jpg
# convert -geometry 450x $FILENAME.jpg $FILENAME-450w.jpg | echo
# convert -geometry 800x $FILENAME.jpg $FILENAME-800w.jpg
# }
# unsplash
#!/bin/bash
# echo "Bash version ${BASH_VERSION}..."
# echo "The number of arguments passed to the script is: $#"
# for ITEM in "$@"
# do
# echo $ITEM
# done
if [ $# -ne 2 ]
then
printf -- "\n ========================================\n"
printf -- "\n Sorry, only two arguments are accepted."
printf -- "\n Please try again.\n"
printf -- "\n ========================================\n"
exit 1
fi
if ! [[ "$1" =~ ^[0-9]+$ ]] || ! [[ "$2" =~ ^[0-9]+$ ]]
then
printf -- "\n ========================================\n"
printf -- "\n Sorry, numbers only.\n"
printf -- "\n ========================================\n"
exit 1
fi
if [ "$1" -le 99 ] || [ "$1" -ge 1921 ] || [ "$2" -le 99 ] || [ "$2" -ge 1921 ]
then
printf -- "\n ===================================================\n"
printf -- "\n Sorry, please choose a number between 100 and 1920.\n"
printf -- "\n ===================================================\n"
exit 1
fi
# echo $1 and $2
width=$1
height=$2
declare filename; filename=funsplash-$(tr -dc A-Za-z0-9 </dev/urandom | head -c 10 ; echo '')
declare https="https://unsplash.it"; declare dimensions="${width}/${height}"; declare random="?random"
mkdir -p "${width}/${height}"
printf -- "\n =========================================\n"
printf -- "\n Downloading: %s%s%s\n" "${https}/" "${dimensions}/" "${random}"
wget -q -O "${width}/${height}/${filename}.jpg" "${https}/${dimensions}/${random}"
# sleep 1
printf -- "\n Your download is ready.\n"
printf -- "\n Location: %s%s%s.jpg\n" "$(pwd)" "/${dimensions}/" "${filename}"
# sleep 1
printf -- "\n ----------------------------------------\n"
printf -- "\n Thanks for using funsplash."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment