Subscribe RSS

Archive for the Category "Linux"

Creating Thumbnails with imagemagick Dec 31
thumbnails with imagemagick

thumbnails with imagemagick

So you want little square thumbnails like Flickr. Here is a bash script that uses imagemagick to generate square thumbnails.


makethumb:

#!/bin/bash
#
# makethumb
#
# A bash script to generate square thumbnails using imagemagick.
#
# Feel free to use and abuse, its just a script. Requires imagemagick!
# See www.websurfshack.net for more stuff like this
#

if [ "$1" = "" ]; then
echo "Usage: makethumb source_file [size] [output file]"
echo ""
echo "e.g."
echo " makethumb large.jpg"
echo " makethumb large.jpg 65"
echo " makethumb large.jpg 45 thumb.jpg"
echo " makethumb large.jpg 25 thumb.gif"
echo ""
echo "Note: You may specify an output file of a different type to the source. The image will be converted automatically to that type."
echo ""
exit
fi

if [ ! -e "$1" ]; then
echo "Input file "$1" not found. Please specify a valid input file!"
exit
fi

ORIG_FILE="$1"

if [ "$2" = "" ]; then
SIZE="45"
else
SIZE="$2"
fi

if [ "$3" = "" ]; then
THUMB_FILE="THUMB_${ORIG_FILE}"
else
THUMB_FILE="$3"
fi

echo "Creating ${SIZE}x${SIZE} thumbnail '${THUMB_FILE}' from source image '${ORIG_FILE}'"

#Grab the image dimensions
WIDTH=`identify -format '%w' ${ORIG_FILE}`
HEIGHT=`identify -format '%h' ${ORIG_FILE}`
echo "Source image is ${WIDTH}x${HEIGHT}"

if [ $WIDTH = $HEIGHT ]; then
echo "Image is already square so just resizing..."
`convert ${ORIG_FILE} -thumbnail "${SIZE}x${SIZE}" -strip ${THUMB_FILE}`
else
if [ $WIDTH -gt $HEIGHT ]; then
echo "Image is landscape"
`convert ${ORIG_FILE} -resize "x${SIZE}" -gravity NorthWest -crop ${SIZE}x${SIZE}+0+0 +repage -strip ${THUMB_FILE}`
fi
if [ $WIDTH -lt $HEIGHT ]; then
echo "Image is portrait"
`convert ${ORIG_FILE} -resize "${SIZE}" -gravity NorthWest -crop ${SIZE}x${SIZE}+0+0 +repage -strip ${THUMB_FILE}`
fi
fi

NEWFILEDATA=`identify ${THUMB_FILE}`
echo "Output file: ${NEWFILEDATA}"
echo "Done!"

MIME Email attachments from command line. Aug 13
marceau marcel

Couldnt resist

I searched for ages to find a half decient example to send mime attachments from the command line that allows you to specify the from and to address. I found some applications however some either didnt let you specify the from address and others didnt let you do multi-part mime messages and the rest suggested to use uuencode. Uuencode is horrible, no just no!

So I wrote my own using sendmail and openssl and it works very well and is incredibly flexible and small.

Here it is:

#!/bin/bash

SENDMAIL="/usr/sbin/sendmail"

FILESPATH="/home/user/"
FILENAME="1.pdf"
FILETOSEND="${FILESPATH}${FILENAME}"

SUBJ="Subject"
SNDR="test@websurfshack.net"
RECP="user@gmail.com"

MIMEBOUNDRY="0016364d261349d0be047105fb55"

(
echo "Mime-version: 1.0"
echo "Subject: $SUBJ"
echo "From: $SNDR"
echo "To: $RECP"
#cc:
#bcc:
echo "Content-Type: multipart/mixed; boundary=${MIMEBOUNDRY}"

echo "--${MIMEBOUNDRY}"
echo "Content-Type: text/plain; charset=ISO-8859-1"
echo "Content-Transfer-Encoding: 7bit"
echo # This echo is important or else you get a mime error
echo INSERT MESSAGE HERE

echo "--${MIMEBOUNDRY}"
echo "Content-Type: application/pdf; name=\"${FILENAME}\""
echo "Content-Disposition: attachment; filename=\"${FILENAME}\""
echo "Content-Transfer-Encoding: base64"
echo # This echo is important or else you get a mime error
openssl enc -base64 -in ${FILETOSEND}
echo "--${MIMEBOUNDRY}--"
) | ${SENDMAIL} -t

There we have it, Simple isnt it. It is very basic but im sure you’ll be able to modify it to suit your needs.

UPDATE: ive since found out that you can specify -t as a sendmail switch to force sendmail to use header data as reciepient information. This then enabled you to specify a BCC field. This works with postfix sendmail… not sure about others.

Category: Linux  | Tags: , , , ,  | Leave a Comment
Sed tricks Aug 11

If you know what Unix Sed is it is a way to use regular expressions to change the contents of a file and then save or display it. Generally sed commands are written like this…

cat filename | sed 's/thsi/this/'

Every occurance of ‘thsi’ will be replaced with this. You can also add on the redirect operator to output to a file ‘>’.

You may also want to use an enviroment variable in your statements. I this case use double quotes instead of single.

cat filename | sed "s/thsi/${MYVAR}/"

Now the problem I ran in to that made me want to write this is that you run in to problems if ${MYVAR} is a path as sed tries to make sense of the slashes in the path name and ofcourse we dont want this so you can use different seperators for example the equals sign eg.

MYVAR="/home/user/file"
cat filename | sed "s=%%path%%=${MYVAR}="

This command will take the variable MYVAR and replace '%%path%%' with the contents of it.

Category: Linux  | Tags: , , , ,  | Leave a Comment
Screen Jul 07
Aww isnt that cute! BUT ITS WRONG!

Hay look another irrelevant picture!

There is a very useful application with plenty of features that can be downloaded easily.

One of the best features is the ability to start a screen session in ssh from one location, disconnect it then reconnect it at a later time from a different session.

Get it now: aptitude install screen

As it is to be used in console and key commands need to be passed to the bash session that it contains the key commands can be quite complex.

As rule of the thumb all commands start with ^A that is control + a then let go followed by a key or combination of keys. You can get a full list of commands available by pressing ^a + ? in screen.

Create window ^a +c
Close window “exit” no prior key seq.
Go to window 0 ^a + 0
Go to window 2 ^a + 2
Go to last window ^a + ^a
View list of windows ^a  + ” (yes quote marks you’ll need to press shift too)

I could go on but this is it in a nutshell.

Category: Linux  | Tags: , , ,  | Leave a Comment