Automatic resizing for 'non-retina' image versions

前端 未结 9 1529
生来不讨喜
生来不讨喜 2021-01-30 15:03

I\'m looking for a solution that can save me from maintaining two versions of the same image, one for Retina displays (aka @2x), one another for non-Retina displays. My goal is

9条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-30 15:43

    Old thread, but I found a use for @nschum's script - I noticed though that it doesn't round numbers for the @1x images if it's dividing an odd number. If I recall correctly this introduces a performance hit; wound up slightly revising it as below. Alters the awk call slightly to do the division there and round it accordingly, and won't re-create @1x images if one already exists (you might want to remove that, dunno).

    At this point we've pretty much hit the point where non-retina isn't a big deal (the iPad 2 is all that remains...? The original Mini too, I guess), so meh. Throwing it up for posterity.

    #!/bin/bash
    # Downsamples all retina ...@2x.png images.
    
    dir=$(pwd)
    echo "Downsampling Retina images..."
    
    find "$dir" -name "*@2x.png" | while read image; do
        outfile=$(dirname "$image")/$(basename "$image" @2x.png).png
    
        if [ "$image" -nt "$outfile" ] && [ ! -f "$outfile" ]; then
            if [[ $(dirname "$image") =~ "Images.xcassets" ]]; then
                echo "Skipping Image XCode Assets directory"
            else
                basename "$outfile"
                width=$(sips -g "pixelWidth" "$image" | awk 'FNR>1 {printf "%.0f\n", $2/2}')
                height=$(sips -g "pixelHeight" "$image" | awk 'FNR>1 {printf "%.0f\n", $2/2}')
                sips -z "$height" "$width" "$image" --out "$outfile"
                test "$outfile" -nt "$image" || exit 1
            fi
        fi
    done
    
    echo "Finished downsampling Retina images"
    

提交回复
热议问题