Simple image size range detection

·

3 min read

This uses the Ruby programming language and the FastImage gem.

— I have a boatload of finished artwork on my art PC and all over the place… And need to find way to separate the ludacris size/resolution images from the sized down ones, problem: the resolution is never exact.

I only really have to size images down for a certain web gallery, but I don’t feel like re-uploading and exploiting that oh so stupid neglected bug where the resizer does nothing if you upload your image file again–the image will take over one’s screen.

I’m thinking up an algorithm on paper; its trivial.

The width and height could be defined like:

DETECT_WIDTH = (x..y) -- where x is the minimum image width and y is the max.

Do the same thing: DETECT_HEIGHT=(a..b) -- where a is the minimum image width and b is the max

In psuedo-code…

if ( P >= X and P <=Y)
 #image_detection_code

end

If you’re looking for some P in the range (X .. Y), its like saying: Is X < P < Y true–with respect to P? Where P could also equal X or Y, not just be in between (and X and Y are integers, and X < Y)…

But its easier than that in Ruby… This is the code I have so far: gist.github.com/ZeroPivot/f69b9dc169a650355..

#Ruby v2.5.3
#gem install fastimage
require "fastimage"

# Note: assumes MIN_* < MAX_* 

#Constants#############
WIDTH=0
HEIGHT=1
################

DEBUG=true

#MIN_WIDTH < MAX_WIDTH
MAX_WIDTH=6000
MIN_WIDTH=2000

#MIN_HEIGHT < MAX_HEIGHT
MAX_HEIGHT=8000
MIN_HEIGHT=2000
#######################

def in_range?(file_location_or_uri)


    is_in_range = false
    width_range = MIN_WIDTH .. MAX_WIDTH
    height_range = MIN_HEIGHT .. MAX_HEIGHT 

    size_array = FastImage.size(file_location_or_uri)

    p size_array if DEBUG

    image_width  = size_array[WIDTH]
    image_height = size_array[HEIGHT]


    if (width_range.include?(image_width) && height_range.include?(image_height))
        is_in_range = true 
    end

    return is_in_range
end

# Testing out below...

puts in_range?("a.png") #=> [5270, 7000]
                                        #=> true

More to come.