1

I am having issues with the part of the following script where it takes the inputted image and checks its dimensions. If it's not a certain dimension then it resizes the image.

My issue is what would I put inside the if-block to check that using ImageMagick?

My current code:

#Change profile picture f(x)
#Ex. changeProfilePicture username /path/to/image.png
function changeProfilePicture() {
    userName="$1"
    filePath="$(readlink -f "$2")"
    fileName="${filePath##*/}" #baseName + fileExtension
    baseName="${fileName%.*}"
    fileExtension="${filePath##*.}"

    echo "Checking if imagemagick is installed..."
    if ! command brew ls --versions imagemagick >/dev/null 2>&1; then
        echo "Installing imagemagick..."
        brew install imagemagick -y
        echo "imagemagick has been installed."
    else
        echo "imagemagick has already been installed."
    fi

    # check the file extension. If it's not png, convert to png.
    echo "Checking file-extension..."
    if ! [[ $fileExtension = "png" ]]; then
            echo "Converting to ''.png'..."
            convert $fileName "${baseName}".png
            fileName=$baseName.png
            echo "File conversion was successful."
    else
        echo "File-extension is already '.png'"
    fi

    # check the dimensions, if its not 96x96, resize it to 96x96.
    #I don't know what to put inside the following if-block:
    if ! [[  ]]; then
        echo "Resizing image to '96x96'..."
        convert $fileName -resize 96x96 "${fileName}"
        echo "Image resizing was successful."
    else
        echo "Image already has the dimensions of '96x96'."
    fi

    echo "changing profile picture to " "$filePath"
    sudo cp "$filePath" /var/lib/AccountsService/icons/
    cd /var/lib/AccountsService/icons/
    sudo mv $fileName "${userName}"
    cd ~/Desktop
}

2 Answers2

4
identify -format '%w %h' your_file

Will output the width, a space, then the height.

If you wanted to store each separately, you could do:

width =`identify -format '%w' your_file`
height=`identify -format '%h' your_file`
Michael
  • 453
2
  1. First, unlikely you have 96x96 existing picture, so most of the case you need to convert. You don't have to identify and compare the dimensions.
  2. Don't trust extension of filename, .png doesn't means it's a PNG image.
  3. Test command and then install is an unnecessary checking and not portable (apt-get, dnf, ... etc). And the fact is it should output "command not found" if this case happen. Moreover this checking might slowing down your function.

So, why not simply do:

#Ex. changeProfilePicture username /path/to/image.png
function changeProfilePicture () {
    sudo mkdir -p -- '/var/lib/AccountsService/icons/'"$1"
    sudo convert "$2" -set filename:f '/var/lib/AccountsService/icons/'"$1/%t" -resize 96x96 '%[filename:f].png'
}

[Note]:

  1. If you want to ignore aspect ratio to ensure the output always 96x96, then change -resize 96x96 to -resize 96x96\!, read this.
  2. The reason why .png doesn't put as part of filename:f above is because:

Warning, do not include the file suffix in the filename setting! IM will not see it, and save the image using the original file format, rather than the one that was included in filename setting. That is the filename will have the suffix you specify, but the image format may different!

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
林果皞
  • 5,156
  • 3
  • 33
  • 46
  • Yeah that's more efficient, however, I get this error every time I run it. Take a look at this image: https://i.gyazo.com/b11c21ce82a0df7dc69214e84050b23b.png – Nicholas Adamou Jun 07 '16 at 17:26
  • @NicholasAdamou oops, edited. – 林果皞 Jun 07 '16 at 17:35
  • is not so unlikely if (some of) the image files have already been resized. checking to avoid resizing again is worthwhile. 2. is correct. and 3. is very good advice - users don't expect or want general non-installer scripts to install stuff...at most, it should print a warning message that required tools are missing, and maybe even a hint about how to install what it needs.
  • – cas Jun 08 '16 at 03:19
  • @cas 1. According to http://www.imagemagick.org/Usage/resize/ , Resize will do nothing at all if the resized image is the same size except "-filter" setting. I believe imagemagik smart enough to handle same size. – 林果皞 Jun 08 '16 at 08:21