Batch autocropping images the easy way with ImageMagick

I had almost 600 images I wanted to autocrop. It would take about a minute to autocrop each image manually since I would have to repeatedly go through a process of open image, autocrop, export, close. You can imagine how much time it would take to do this manually almost 600 times (about a full working day).

Being the type of person I am I decided I would Google a solution rather than spend hours doing this task manually. Instead of a full working day I did it in 5 minutes, by using an application called ImageMagick.

ImageMagick is mostly a command line based tool which lets you automate processing images in lots of different ways. For example, if you want to build a web application that crops a photo or apply a filter then you might use ImageMagick because it’s easier to interface it with code and is available pre-installed on a large number of web hosting plans.

This is a bit of a power user trick, but if you ever use basic command line functions then this should be relatively simple.

Here’s my solution to overwrite the existing images so they become cropped:

for a in *.jpg; do morgify -trim "$a" "$a"; done

Or if you don’t want to overwrite all the existing images then use this:

for a in *.jpg; do convert -trim "$a" "$a"; done

So if we assume you’re using Linux and you have a directory full of images you want to autocrop in a directory called “images”, then the code to autocrop all the images in the images directory would be:

for a in images/*.jpg; do morgify -trim "$a" "$a"; done

Or if the image format is .png for example then replace .jpg with .png.

How to randomise the order of paragraphs with PHP

One part of launching new websites which offer pretty much the same products and services is rewriting content so that it doesn’t appear to be a direct copy of exiting websites.

To help me rewrite articles, I find it helps me to randomise the order of key selling points so I can focus on rewriting them rather then thinking about how they should be ordered.

To randomise the order of paragraphs I wrote a PHP script using the shuffle function to randomise the appearance of selling points in an array

$sellingpoints = array(
'Double sided printing.',
'Same day print & dispatch',
'Delivery before 12pm (Express) & 10am (Express Plus).',
'Round corner finish.',
'UV coating (extra rich colours and more durable finish).',
'Various quantities ranging from 100 - 5,000.', );
foreach ($sellingpoints as $sellingpoints){

Backing up a file in Windows with a batch script

One part of my job means working with a CMS (content management system) to add some functionality to a site with custom code.

Without having source code access this can mean easily breaking things on the backend admin section because the WYSIWYG (what you see is what you get) editor can strip away parts of the custom code as soon as you switch off the HTML view or even just load the page because code view is switched off by default.

I wanted to encourage frequent backups of files which are edited because sometimes minor edits might be required when I am not around to fix things that could potentially go wrong.

Version control such as GIT for example was a bit too much of a learning curve for the people concerned, so I wrote up a batch script that would allow a user to copy a file into an archive directory before making edits to the current working version.

I also wanted the bath file to work regardless of where you move it to, so as long as there is a directory called Archive in the same directory as the batch file it would work.

What I came up with was a solution that would only require a user to click on an icon, which would open a command shell that can be used to paste the name of a file within the directory you wanted to make a copy of. Once the file name is entered they would hit enter and the current set date and time would be added as a prefix to the copied file.

Here is what I came up with:

set /p Input=Enter some text:
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%

set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%

copy "%Input%" "%Input% - %stamp%"

By saving this in Notepad or other code editor of your choice (I like Notepad++) I could save the file with a .bat extension which would create the batch file.