Crystal Lover

Utility scripts

Upload iamges recursively to Cloudinary

# Workdir: containerized-wordpress/media/image/photo
upload_all_avif_files_recursively() {
  local dry_run=0;
  [ -n $1 ] && [ "$1" = "-n" ] && dry_run=1
  [ $dry_run -eq 1 ] && echo "Dry run - no image will be uploaded"
  # Find every dir with AVIF files
  local img_width=2048;
  for avif_file in $(find . -mindepth 2 -type f -iname "*${img_width}x.avif"); do
    local product_dir=$(basename $(dirname $avif_file))
    local avif_file_path=$(readlink -f $avif_file)
    echo "Uploading: $avif_file_path in $product_dir"
    [ $dry_run -eq 1 ] && echo "Dry run - $avif_file_path" && continue
    cloudinary_upload_product_image $product_dir $avif_file_path
  done
}

Resize images recursively to smaller than the maximum width

# Workdir: containerized-wordpress/media/image/photo
resize_images_recursively() {
  if (( $# < 1 )); then
    echo "Usage: resize_images_recursively <img_ext> [dry_run]"
    echo "Example: resize_images_recursively jpg -n"
    return 1
  fi
  if [ -z $1 ]; then
    echo "Image extension is required"
    return 1
  fi
  local img_ext=$1;
  local img_width=2048;
  local dry_run=0;
  [ -n "$2" ] && [ "$2" = '-n' ] && dry_run=1;
  [ $dry_run -eq 1 ] && echo "Dry run - no image will be resized"
  # Find every dir with image files of the specified format
  for img_file in $(find . -mindepth 2 -type f -iname "*.${img_ext}"); do
    echo '--------------------------------------------------------------------';
    local img_filename_without_ext=$(basename $img_file | cut -f 1 -d '.')
    local product_dir=$(basename $(dirname $img_file))
    local img_file_path=$(readlink -f $img_file)
    echo "Resizing: $img_file_path in $product_dir"
    local output="$product_dir/$img_filename_without_ext-${img_width}x.$img_ext"
    echo "Output: $output"
    # if file name contains 2048, skip it
    [[ $img_file =~ 2048 ]] && echo 'Got 2048 in file name, skip' && continue
    [ -f $output ] && echo "Output file already exists" && continue
    [ $dry_run -eq 1 ] && continue
    convert $img_file_path -resize ${img_width}x $output
  done
  echo '--------------------------------------------------------------------';
  [ $dry_run -eq 1 ] && echo "Dry run - no image is resized"
}

TODO

  • Find a free block editor to replace Elementor