Sunday, February 9, 2014

Wallpaper editing math kinda sucks

I don't think I ever posted about this, and I'm too lazy to go check.  And no, I'm not resuming wallpaper updates.  I don't browse /w/ anymore (it sucks now) and I only occasionally save an image for use as a wallpaper.

Anyway.

For quite a while, I would resize and rescale wallpapers to the nearest standard monitor aspect ratio before adding them to my collection or uploading them in one of my wallpaper updates.  To facilitate doing this, I wrote a script in PHP that I could give the dimensions and the target aspect ratio to, and it would calculate the closest dimensions that I could resize the image to in order to achieve that aspect ratio, to minimize the amount of editing I'd have to do.

Like I said in the first sentence of this post, I don't know if I ever made the existence of this script known here, so this post will do just that.  But first, an overview of its features.
  • When supplying the image dimensions, you can either type them in manually, or upload the image.
  • When selecting the aspect ratio, there are a number of common aspect ratios available in a dropdown for easy access.
  • You can also specify a custom aspect ratio, for if you're a n00b and don't know your monitor's aspect ratio, or you're editing something for a mobile device that might use some weird aspect ratio.
  • It returns a minimum of two resolutions and a maximum of four, and tells you the difference in number of pixels for each dimension, so you can decide which one you want to use.
  • The resolutions returned require an absolute minimum amount of editing.
I'm really only posting this because I just added some stuff and reworked some stuff that was stupid to be less stupid.  For shits and giggles, here's what I changed.
  • Added: Ability to specify a custom aspect ratio.
  • Added: Reducing the supplied custom aspect ratio, as the function that looks for target resolutions needs this to be reduced.
  • Added: Slightly better error messages.  Now, instead of just insulting you, they also tell you what you did wrong, so you can go fix it, you horrible person.
  • Changed: Dimension differences were previously "width bigger, height smaller", etc., they are now pixel differences per dimension (i.e. "width + 8 pixels, height - 4 pixels").
  • Changed: Better input verification (regular expressions go).
  • Removed: Arbitrary restriction on image width and height that existed because of my previous shitty input verification.
Necessity is the mother of all invention, and this most recent update (and the script as a whole) is certainly no exception.  I'm fucking around with making wallpapers for my phone, which need to be in a 4:5 aspect ratio and ideally 176x220.  Rather than just adding 4:5 to the aspect ratio dropdown, like I apparently did with 8:7 for making ToxicOS wallpapers for my PS2, I figured it would be better to just allow any aspect ratio to be used.

Reducing a fraction programmatically is a fairly simple process, but involves calculating the greatest common denominator of the two values, which is a rather interesting calculation.  However, once you have the GCD, you simply divide the two values by said GCD and there you go, reduced fraction.  This is how I reduce the values provided in the custom aspect ratio boxes so that the script will work correctly.

Anyway, here's what you've probably been wanting this entire time if you're still reading this: the link.  Enjoy.  It's rather lacking in bells and whistles, but oh boy is it ever functional.

No comments:

Post a Comment

I moderate comments because when Blogger originally implemented a spam filter it wouldn't work without comment moderation enabled. So if your comment doesn't show up right away, that would be why.