I'm trying to make a C# program that gets a directory path and iterate on images, then for each image it should check if that Image it's a fit for instagram post and I don't need to crop it.
Some infos about instagram post ratios:
I'm using these methods to find image aspect ratio
static string FindAspectRatio(int width, int height)
{
int gcd = FindGcd(width, height);
string ratio = $"{width / gcd}:{height / gcd}";
return ratio;
}
private static int FindGcd(int x, int y)
{
int gcd = 1;
int temp;
if (x > y)
{
temp = x;
x = y;
y = temp;
}
for (int i = 1; i < (x + 1); i++)
{
if (x % i == 0 && y % i == 0)
gcd = i;
}
return gcd;
}
then I added 3 if checks for instagram ratios to check with each image. The problem is that sometimes the aspect ratio is not 4:5 but the height is a bit shorter than max aspect ratio instagram allows for vertical posts and on that case I don't need the app to tell me that I should crop that image. Is there anyway to achieve this so the app can exactly tell when an image should be cropped?
if (aspectRatio == "4:5" || aspectRatio == "1:1" || aspectRatio == "16:9")
{
continue;
}
Okay I was able to solve it by experimenting with different values,
static bool InstaRatioValid(decimal ratio)
{
if(ratio >= 0.80M && ratio <= 1.92M)
{
return true;
}
return false;
}
static decimal FindAspectRatio(int width, int height)
{
decimal ratio = Math.Round((decimal)width / height, 2,MidpointRounding.ToZero);
return ratio;
}