Skip to content
Snippets Groups Projects
Commit 68e9d5b0 authored by Cee Nell's avatar Cee Nell
Browse files

update use to magick convert

parent 2a156e5f
No related branches found
No related tags found
1 merge request!71Responsive imgs
Showing
with 54 additions and 0 deletions
#!/bin/bash
# Check if ImageMagick is installed
if ! command -v magick convert &> /dev/null
then
echo "ImageMagick is not installed."
exit 1
fi
# Source and output directories
src_dir="src/assets/images"
output_dir="${src_dir}/responsive_images"
# Create the output directory if it doesn't exist
mkdir -p "$output_dir"
# Array of target widths for responsive images
declare -a widths=(320 640 1280 1920)
# Find all image files in the src_dir
# Modify the extensions if you want to support more formats (e.g., png, gif)
find "$src_dir" -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \) | while read input_image; do
# Extract the relative path and file name from the input image
relative_path=$(dirname "${input_image#"$src_dir/"}")
base_name=$(basename "$input_image" | cut -d. -f1)
extension="${input_image##*.}"
# Create corresponding subdirectory structure in the output directory
mkdir -p "${output_dir}/${relative_path}"
# Get the original image size (in bytes)
original_size=$(stat --printf="%s" "$input_image")
# Loop through each width and generate responsive images
for width in "${widths[@]}"
do
# Generate JPEG image
magick convert "$input_image" -resize "${width}x" "${output_dir}/${relative_path}/${base_name}-${width}.jpg"
echo "Created ${output_dir}/${relative_path}/${base_name}-${width}.jpg"
# Generate WebP image
magick convert "$input_image" -resize "${width}x" "${output_dir}/${relative_path}/${base_name}-${width}.webp"
echo "Created ${output_dir}/${relative_path}/${base_name}-${width}.webp"
done
# Get the size of the largest version (1920px JPEG)
largest_image="${output_dir}/${relative_path}/${base_name}-1920.jpg"
largest_size=$(stat --printf="%s" "$largest_image")
# Print out the original and largest version sizes (in kilobytes)
echo "$input_image was $(($original_size / 1024)) KB. The largest version of this image is now $(($largest_size / 1024)) KB."
done
echo "All responsive images have been created in ${output_dir}."
\ No newline at end of file
src/assets/images/responsive_images/alphanumeric-1280.jpg

30.2 KiB

src/assets/images/responsive_images/alphanumeric-1280.webp

22.1 KiB

src/assets/images/responsive_images/alphanumeric-1920.jpg

51.5 KiB

src/assets/images/responsive_images/alphanumeric-1920.webp

36.6 KiB

src/assets/images/responsive_images/alphanumeric-320.jpg

4.61 KiB

src/assets/images/responsive_images/alphanumeric-320.webp

4.65 KiB

src/assets/images/responsive_images/alphanumeric-640.jpg

12.4 KiB

src/assets/images/responsive_images/alphanumeric-640.webp

10.2 KiB

src/assets/images/responsive_images/avg_household_size_2022_en-1280.jpg

290 KiB

src/assets/images/responsive_images/avg_household_size_2022_en-1280.webp

80.5 KiB

src/assets/images/responsive_images/avg_household_size_2022_en-1920.jpg

482 KiB

src/assets/images/responsive_images/avg_household_size_2022_en-1920.webp

114 KiB

src/assets/images/responsive_images/avg_household_size_2022_en-320.jpg

28.9 KiB

src/assets/images/responsive_images/avg_household_size_2022_en-320.webp

9.46 KiB

src/assets/images/responsive_images/avg_household_size_2022_en-640.jpg

95 KiB

src/assets/images/responsive_images/avg_household_size_2022_en-640.webp

28.6 KiB

src/assets/images/responsive_images/avg_household_size_2022_es-1280.jpg

294 KiB

src/assets/images/responsive_images/avg_household_size_2022_es-1280.webp

81.5 KiB

src/assets/images/responsive_images/avg_household_size_2022_es-1920.jpg

487 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment