Scale images for the web — quickly

Starting Gimp just to scale down some photos to send them by mail? Meh.
How about mark them, right-click, "Send To" -> "Scale for web"?
No problemo:
First, get a tool to scale images:

sudo apt-get install imagemagick

Now, create a script that takes a list of images as input, scales them down, and stores them with a prefixed file name. Go!

#!/bin/bash
pwd > ~/f0.txt
echo "$@" > ~/f1.txt
for file in $@
do
	convert "$file" -quality 50 -resize 1024x768 "${file%/*}/web-${file##*/}"
done

Make sure we chmod +x the script file we just created.
Next, let’s integrate it into "Send To". (This is an example for Thunar)
Create a .desktop file in the sendto folder, e.g. /usr/share/Thunar/sendto/scale-for-web.desktop with following content:

# Scale images for the web
[Desktop Entry]
Type=Application
Version=1.0
Encoding=UTF-8
TryExec=scale-for-web
Exec=scale-for-web %F
Icon=
Name=Scale for the web
MimeType=image/jpeg;image/jpg;image/png

Replace both scale-for-web with the name you gave to your script file. Is the script’s folder in $PATH? No idea? Then just put the entire path+filename.
Now, open Thunar, select some images (Jpeg or Png), right-click, "Send To" -> "Scale for the web", wait a few seconds and you’ll see web-....jpg files appearing in the same folder. 🙂

References: how to scale images with imageMagick, extract path segments, more details on string manipulation

Hash in JS

Generating hashes from strings is a very handy thing to have. Surprisingly, in JavaScript, an arithmetic-operation-based algorithm is about *three times* faster than one using bit-wise operators–see for yourself. I suggested to add the most performant method as hash method to underscore.string.js.

So much about statements on stackoverflow like

The hash << 5 - hash is the same as hash * 31 + char but a LOT faster.

(cf. here)

In times of interpreted languages and virtual machines one should reconsider before applying the same old rules again.

p.s. If you know about alternative algorithms/implementations, please add them to the test and leave a comment here, I'm curious to see what else there is. Btw. I was very surprised that the neat reduce-based version failed so miserably.