Meta: A Little BASH Script for Scaling Media 0
You may have noticed that I rescale embedded media to 500 pixels in width. When I started blogging, 800×600 monitors were still not uncommon, and 500px seemed a sane choice. I’ve maintained the practice for consistency’s sake.
At one time, I maintained a spreadsheet of common dimensions and their scaled values. I could enter the existing values and it would calculate the scaled ones, then I could save it with the additional data.
Ultimately I replaced the spreadsheet with a command line alternative, which works quite nicely (if you can remember high school math, you’ll recognize the equation; piping the output to bc is necessary because BASH by itself can parse only integers):
Code:
echo 500/[original width]*[original height] | bc -l
This outputs the proportional height.
Here’s an example:
Code:
bash-4.3$ echo 500/640*423 | bc -l
330.46875000000000000000 <---I round the output to the nearest integer
In the case of videos, the dimensions are right there in the embed code.
For images, finding out the height and width can be a bit more complex. In Firefox, which I normally use, right-clicking over an image brings up a menu which includes "View Image Info"; you can usually find the dimensions under the "Media" tab after selecting "View Image Info."
If the dimensions are blocked from that view, you can often find them by scrolling through the "Meta" section under the "General" tab. (Very rarely, they are so thoroughly obscured that I have copied the image and pasted it into the GIMP, then used the GIMP's Image-->Scale Image function to determine the correct height for a 500px width.)
I finally got tired of typing the command--all those keystrokes!--and decided to write a little script. As I am hardly an expert in scripting, I spent more time looking for the proper reference, which I found at The Linux Documentation Project, than I did writing the script.
A few notes on the technical terms:
- echo means display to STDOUT or "standard output," that is, the terminal.
- read [sometext] means remember the input from the screen and convert it into a variable named sometext.
- $ indicates that whatever immediately follows it is the name of a variable.
- bc is a powerful *nix command line program for doing calculations.
Here's my little script, in case it helps a fellow scripting neophyte.
Code:
#! /bin/bash
# This is a script to determine the correct height in pixels for for scaling embedded media to 500px width.
echo -n "Enter the width and press [ENTER]:"
read width
echo -n "Enter the height and press [ENTER]:"
read height
echo "The correct height is:"
echo 500/$width*$height | bc -l
Here's an example of the script in action:
Code:
$ sh resize.sh
Enter the width and press [ENTER]:600
Enter the height and press [ENTER]:322
The correct height is:
268.33333333333333333226
After doing the arithmetic, I paste this bit of HTML into the image embed link and enter the proper values:
Code:
class="aligncenter" width="px" height="px"
(Updated 2017-10-20 23:50)