Automating the simple stuff
We all use computers for one thing or another, and in most cases we'll do the same things on them over and over. Part of being a developer is realising the things which need doing a lot, and automating them to increase productivity. Doesn't matter what it is, there's generally a way to automate things.
As part of building my (other) website, I came to realise that there's a lot of things which would need to be set up as repetitive tasks, and thought about these from the start. Other jobs were things which needed to be coded, but as I was building the code in PHP, it was going to be inefficient to write the code in that. This is where we have to pick the right tool for the job. In my case, it came down to some simple Python scripts.
As the other website is (in time) going to have a lot of pages, I needed to set up something to build a sitemap, and update this regularly. To do this I get a list of articles out of the database and use the imported ElementTree to build the XML before outputting. Simple really, and here's basically what it does to build the site map:
def add_sitemap_element(loc, mod, change, priority): url = et.Element('url') # build the sub-elements # add loc node loc_node = et.Element('loc') loc_node.text = loc # add last modified modified_node = et.Element('lastmod') modified_node.text = mod # priority priority_node = et.Element('priority') priority_node.text = priority # change frequency change_frequency_node = et.Element('changefreq') change_frequency_node.text = change # add nodes to URL level url.append(loc_node) url.append(modified_node) url.append(change_frequency_node) url.append(priority_node) sitemap_root.append(url)
An area where PHP isn't particularly great is with image manipulation. It can be done, and it's fairly trivial once you get into it, but I've always found the amount of code needed for it was huge. I decided that to create thumbnails I was going to use Python for that too. Every few minutes I check to see what images don't have thumbnails linked to them and create them. I use the PIL library and import Image, but resizing an image is as simple as follows:
image = Image.open(input_image_name) # full path new_image = image.resize((int(new_width), int(new_height))) new_image.save(output_image_name) # full path
Okay, so there's other processing in between the lines, like calculating the new sizes, but there's no need to worry about what type of image it is. If it's a PNG or JPEG and you want to output it as a bitmap or GIF, then you can. I also found the performance of the re-size in Python much better than in PHP. The right tool for the job (even if there's probably faster libraries out there).
There's other jobs I have automated, and more which I need to automate, but when something needs automating do it. Some things might be part of an application scope to do (such as create thumbnails) but you're better off creating a background job to do it rather than keeping the user waiting for them to be created upon save.
A little thought and a little code can help in many ways!