74 lines
1.8 KiB
Bash
Executable File
74 lines
1.8 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Check if two arguments are provided
|
|
if [ $# -lt 2 ]; then
|
|
echo "Usage: $0 <input_file.webp> <output_prefix>"
|
|
exit 1
|
|
fi
|
|
|
|
# Assign arguments to variables
|
|
input_file="$1"
|
|
output_prefix="$2"
|
|
|
|
# Check if the input file exists
|
|
if [ ! -f "$input_file" ]; then
|
|
echo "Error: File '$input_file' does not exist."
|
|
exit 1
|
|
fi
|
|
|
|
input_dimensions=$(magick identify -format "%wx%h" "$input_file")
|
|
echo "Input file is ${input_dimensions}"
|
|
|
|
# Determine the file extension
|
|
extension="${input_file##*.}"
|
|
|
|
# Skip conversion if the file is already a .jpg
|
|
if [[ "$extension" != "jpg" ]]; then
|
|
|
|
# Set the output file name based on the input file extension
|
|
if [[ "$extension" == "webp" ]]; then
|
|
output_file="${input_file%.webp}.jpg"
|
|
elif [[ "$extension" == "png" ]]; then
|
|
output_file="${input_file%.png}.jpg"
|
|
else
|
|
echo "Error: Unsupported file type. Only webp, jpg and pgn files are supported."
|
|
exit 1
|
|
fi
|
|
|
|
# Check if the output file already exists
|
|
if [ -f "$output_file" ]; then
|
|
echo "Error: Output file '$output_file' already exists."
|
|
exit 1
|
|
fi
|
|
|
|
# Perform the conversion
|
|
echo "Converting '$input_file' to '$output_file'..."
|
|
if magick "$input_file" "$output_file"; then
|
|
echo "Conversion successful. '$output_file' created."
|
|
else
|
|
echo "Error: Conversion failed."
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
|
|
# Define the output widths
|
|
widths=(1600 1400 1200 800)
|
|
|
|
# Loop through the widths and resize the image
|
|
for width in "${widths[@]}"; do
|
|
# Create the output file name with the .jpg extension
|
|
output_file="${output_prefix}-${width}x.jpg"
|
|
|
|
# Resize the image and convert to JPG
|
|
magick "$input_file" -resize "${width}x" "$output_file"
|
|
|
|
# Check if the operation was successful
|
|
if [ $? -eq 0 ]; then
|
|
echo "Created $output_file"
|
|
else
|
|
echo "Error resizing to ${width}px"
|
|
fi
|
|
done
|
|
|