Speeding up GD when making 2 sizes

Summary: If you’re using GD to generate 2 image sizes from an original, first generate the larger resized image, then generate the smaller size based on the resized image (not the original) to save a tiny bit of execution time.

I’m working on a webcam project where I need to pull in a large webcam image and resize it to 2 smaller sizes. The first user to visit the webcam page in a while triggers a resize, so speeding up the resize operation means they won’t have to wait long for the page to appear.

First, to get the execution time for a block of PHP code:

$startexec = microtime(true);

/* code you want to measure goes here */

$endexec = microtime(true) - $startexec;

This is for PHP 5+ – see the microtime entry in the manual if you’re working with an earlier PHP version.

There are a lot of ways to fetch a remote image, such as fopen() and CURL, but imagecreatefromjpeg($url) has been working fine for me so far. Here’s the code to get the external image, resample it, delete the old resized image if it exists, then write out the new version.

$bigimage = imagecreatefromjpeg($bigpath);

$midimage = imagecreatetruecolor($midwidth,$midheight);

imagecopyresampled($midimage,$bigimage,0,0,0,0,
$midwidth,$midheight,$bigwidth,$bigheight);

if (is_file($midpath))
unlink($midpath);

imagejpeg($midimage, $midpath);

On my testing machine it was taking about 1.2 seconds to do this twice, using a 2048×1536 original to generate both smaller-sized images. I found that by doing the mid-sized resample first, then using the mid-sized image for the smaller resample, resulted in a slightly faster execution time of a little less than 1 second.

That’s not much of an improvement, so next I’m going to investigate using ImageMagick instead of GD since that could be twice as fast.

Update: After a bit of testing I’m not seeing much difference between using GD (around 0.88 seconds) and ImageMagick (around 0.75 seconds), at least for the sort of resizing I’m doing – from 2048×1536 down to 850×638 and 500×375.

Tagged , Leave a comment

Grilled-braised beef short ribs

Braised short ribs

Ingredients: beef short ribs, onion, carrot, celery, red wine, rub spices, salt.

Short ribs are great grilled, but they’ve got some connective tissue that really wants to be braised. So, why not do both? First, your ribs should be big and chunky. If you’ve got thin-sliced ribs you should probably just grill them.

Start by getting your grill set up for indirect cooking. On my Weber, that means getting a smallish batch of coals going in the charcoal chimney and dumping those off to one side of the grill when they’re red-hot. If there’s greasy residue in your grill wait until that burns off – otherwise you’ll end up with a gross layer of black stuff on your meat.

Put some rub on your ribs. I like a mixture of salt, pepper, paprika, cumin, coriander, and allspice plus some fresh thyme or rosemary. Once the grill is hot and clean place the ribs on the side opposite the coals. Put the lid on. Turn them over after 10 or 15 minutes. Close the vent on the grill a bit if it’s too hot – you don’t want the ribs to cook too quickly. Check on the ribs every 15 minutes or so.

Meanwhile, heat up your oven to about 275F. Get out a braising dish the ribs will fit in. Pretty much any non-reactive (glass, enamel, stainless steel) dish or pot with a lid will work for braising. I like to throw some chopped onion, carrot, and celery in the bottom of the pot and soften that up a bit in some bacon drippings. This also gets the braising pot nice and hot for the ribs.

The ribs could be ready to braise after 30 minutes, but I like to let them go about an hour for more smoky flavor. When you’re ready, move them to the braising pot and pour some red wine in with them. Use enough wine to come about halfway up the ribs – not enough to cover them up completely. Bring the pot to a boil then cover and place in the oven. Check for tenderness after an hour, but it usually takes about 2 to achieve that desirable “falling off the bone” texture.

They’re good immediately, and even better reheated the next day. If you chill them you can easily separate out the layer of fat and harvest the gelatinous stuff for an excellent sauce.

Tagged , , 1 Comment

Nashville notes

I got to go to Nashville for the first time a couple of weeks ago for a friend’s wedding. What a cool place! It combines the crusty industrial feel of Pittsburgh with the artistic party-town feel of New Orleans. Like Pittsburgh, it’s wrapped around a river, the Cumberland.

Goofy fish Murky path

While waiting for Mandi to get a manicure I went for a walk to Centennial Park. The centerpiece of the park is a replica of the Parthenon that was built for Tennessee’s 1897 Centennial Exposition – like the Palace of Fine Arts in San Francisco. On Memorial Day we went back to the park to check out a music festival but only caught a couple of notes before heading off to find cold drinks and barbecue.

Shelby Street Bridge Bricks and sky

We happened to be in town for Memorial Day, which curtailed our ability to shop. Instead, we walked around downtown and across the Shelby Street Bridge, an old bridge converted to pedestrian use in 2003.

While we weren’t shopping or walking, we were eating. Some highlights:

Mandi knew about Fido, a great espresso shop, from previous trips to Nashville. They serve breakfast too – I don’t know what everyone’s waiting in line for down the street at Pancake Pantry – the eggs and sausage at Fido were excellent and we didn’t have to wait.

Our first night in town we had dinner at Marché, a sort of French bistro-market in the East End. My asparagus and salmon with poached egg was about as good as it gets.

I was whining about how we’d eaten everything except barbecue so Mandi found Tom’s Elite Carryout, a funky little place in the East End. The barbecue pork and a double-side of mustard greens made a perfect lunch.

We had sausage, pig knuckles, sauerkraut, and beer at Gerst Haus. I had pretty low expectations, but this place served me a plate of pig knuckles that looked more-or-less like something I last saw in Prague. I only wish I’d had the sense to order the knuckle appetizer instead – I only managed to finish 2 or 3 knuckles. To top things off, they had a live band and real stuffed deer on the walls!

Only known treatment for my condition. Our dinner companions.

Tagged , Leave a comment

Updated image renaming script

To help keep track of my photos, I like to rename them so their filenames are based on the date and time they were taken. I’ve used a couple of different scripts to do this, settling on a single-line bash script that runs exiftool. This works really well, but sometimes the extensions on my photos are jpg and sometimes they’re JPG. So, I added a couple of lines to the renaming script to first lowercase the extensions. Here’s the new script:


#!/bin/bash
ls -l *.JPG > /dev/null 2>&1
if [ "$?" = "0" ]; then
for f in *.JPG
do
mv "$f" "${f%.JPG}.jpg"
done
fi
ls -l *.jpg > /dev/null 2>&1
if [ "$?" = "0" ]; then
exiftool -d %Y%m%d_%H%M%S%%-c.%%e "-filename<CreateDate" *.jpg
fi

Download camcon:camcon.zip

Leave a comment

Crawfish party recap

We had the second annual crawfish party at our friend Aliza’s house earlier this month, once again stuffing ourselves with crawfish late into the night. This year we somehow managed to save a few crawfish to shell the next day for a tasty frittata.

Just like last year, I put together the boil spices based on the recipe from the Good Eats crawfish episode. This year I made two batches of spices – one less spicy and one with the standard amount of cayenne. I think the amount in the recipe is just right, but taste buds vary. The quantity of spices in the recipe is for one 40 quart boil, so I multiplied everything by 4 so we’d have enough for a couple of boils each in our 30 quart pots.

Instead of the corn in the recipe, we cooked a bunch of vegetables – brussels sprouts, asparagus, and broccoli. Having learned our lesson last year, we threw the potatoes in first then added the vegetables with the crawfish. The vegetables still end up well done, but not quite as boiled to death.

Ready for crawfish.

Big claws

Tagged , Leave a comment