Okay, okay, I know I’m crap at doing these blog-type things, but I guess this is just becoming a bin for things I’ve done that I find interesting.

And on that note…

I’m working on what I’d call an exiting new project – it’s a new website that I’m working on that’s somewhat taking up all my time… I’ll reveal the address at a later date.

It’s proved interesting so far – the biggest problems I’ve had are file permissions and relearning bash… Meaning that I’ve had to learn quite a lot about the Linux file permission way of working, and get my C based head around bash!

The issue comes, mostly, from the fact that I have two user upload paths for content – a php driven upload and a java driven upload. The php upload saves files as “project-user” and the java upload saves as “www-user”. This isn’t too much of an issue when it comes to viewing the content itself – it displays the same on the website, so on the face of it, isn’t too much of a problem.

The problem is that the uploaded content is images, and I want to, every night, resize the original images using imagemagick to a maximum dimension, either h or w depending on aspect ratio, to 1980 and a quality of 85%. This reduces my storage requirements for images by a whole 80%, however lets the system generate the thumbnail and smaller size detail from the original full resolution image, but in slideshow mode it still displays it at a good resolution – 1980×1435 is bigger than most displays so overall the quality is, to the untrained eye, the same. It’s pretty crazy!

Straying from the permissions error, the images are being resized by a bash script that was developed by myself (with some assistance from Scotlug), mostly as a way of keeping my bash a wee bit sharper than it currently is. It completely has reminded me just how useful and powewrful linux commands can really be!

The script was required to start off in the directory containing all the “original” image files and find all the images in every subdirectory. Once it’s done that, it has to detect the dimensions of the image, and if either h or w is greater than 1980, resize it whilst maintaining the aspect ratio, so that the maximum dimension is 1980, and then replace the original with this new file.

Due to my relative inability with bash, It’s set up as two scripts, one which calls the other – resize.sh and image.sh, both of which are seen and explained below.

Resize.sh

We see the most important part of the script here – this took me ages to get my head around. The linux find command is used to great effect here. We’re telling it to find in the current directory, (find .) look for file type .jpg (-type /.jpg) where the / is an escape character, and then * as a wildcard to select all .jpg files. Next we see -exec ./image.sh {} /; – this executes the image.sh script, iterating through the list of images found and passes the current filename to the script as an argument.

For all the image types I expect the site to have to deal with – .jpg .png I can just run the find command again for another  -type.

Image.sh

This one’s pretty cool – I really like how functional imagemagick is in this situation!

We start off by setting up some variables – infile=$s1 sets the variable $infile equal to the first argument passed from the command line, in this case the filename we’re working on. outfile=$infile.new creates an outfile that will be, for example, picture.jpg.new. It then sets $w and $h to the image’s height and width using identify – a fantastic imagemagick program that parses EXIF data amongst other things to gather information on the image.

Next, we compare the height and width to my value – if ($h > “1980″ || $w > “1980″) in C-speak is translated to [$h -gt “1980” -o $w -gt “1980”] in bash. Assuming either or both is true, it runs the commands within the if statement.

At this point, though, I got a little bored. And permission issues meant that things couldn’t be seen. And I got frustrated.

You know what I did? I changed to Drupal and did it in an evening. With no bash. And no permission issues.

Wasn’t that easy?

Hxx