Copyright Notice

This article is Copyright (c) Al Andersen, http://www.alandersen.com/

You are free to use this code in your programs providing you adhere to the most current version of the GNU General Public License (GPL).


Disclaimers And Caveats

This code is provided, as is. I don't guarantee that it will work, and I don't guarantee that it will not destroy data on your hard disk. Use at your own risk! Backup important files!

This code requires that a command line version of PHP is installed on your computer.

This program requires that ufraw be installed on your computer. Check your package manager or get it from http://www.exiv2.org/. Make sure it is in your executable path.

This code is for use on your local computer only. Do not use this program on a server – it is not secure!


Recursive PHP Function For Converting RAW Image Files

RAW images are great for capturing all the detail that's possible from a digital exposure. Unfortunately, RAW images are also proprietary, meaning that they are in a unique format that varies from vendor to vendor. Simply put, you can't post them on the world-wide-web and expect people to have the necessary software to view them. To share your photos you're going to have to convert them into a standards-compliant format that any web browser can read, such as TIFF, PNG, or JPEG (JPG).

JPEG is probably the most popular photo-sharing file format. Its main advantage is that it can be compressed down to a relatively small file size while retaining a good amount of detail and image quality. Photographers like it because it saves them money when it comes to hosting file space and bandwidth costs. Users like it because of shorter upload / download times.

While converting a single RAW image to a JPEG is a trivial task for modern-day post-processing software like PhotoShop, GIMP, iPhoto, etc., converting your latest 300+ images recently downloaded from your SD card and now sitting on your hard disk would be a time consuming endeavor without some means of automation. There are software packages that provide various degrees of flexibility for batch-processing large queues of RAW images. However, once again we have the problem of proprietary formats. Software is usually tied to a specific platform. Some runs only on PC's, some only on Macs, some on both but not linux, and very rarely will you find a program that can run on all three platforms, such as Bibble 5.

While an automated vendor-supplied processiong solution is nice, some of us prefer to roll-our-own software using scripting languages like PHP or PERL, which are excellent solutions for this kind of problem. I prefer using PHP. A little bit of scripting and a call to high-quality RAW converter like ufraw are all I need to automate processing any amount of RAW files I want. The only limitation is whether or not ufraw supports the camera format and if my hard disk has enough space to hold the output.


The Code

The script I use is a variation on the recursive directory script we've reviewed in our last couple of tutorials. I'll cover the pertinent parts of the script here. Those not familar with the recursive process should review the previous tutorials which cover it in detail.

To begin, we make a modification to our recursive function, replacing the ELSE part of the if ($filetype == "dir") statement with the following code.

	else
{
// We're looking for a specific file extension.
if (preg_match ("/{$p_match}$/i", $file))
{
// Create the ufraw-batch command and execute it.
$cmd = "ufraw-batch --wb=camera --base-curve=camera
--curve=linear --out-type=jpeg --compression=100
--exif --overwrite {$pathname}/{$file}";
$result = shell_exec ($cmd);
}
}

Let's go over the above code.

First, we need to ensure that we're only looking at file names that match the RAW files we want to convert. The recursive function accepts a $p_match parameter which contains the file extension of the RAW files we want to convert. The preg_match function looks for the appropriate extension in each file name. If it finds it, the conversion code gets executed. If it doesn't, it falls through and nothing happens to that file. Using a function parameter like this lets us use this function for many types of RAW files by simply calling the function with the appropriate extension name.

To convert the found RAW file we construct a $cmd string that calls the ufraw-batch program and pass to it the appropriate parameters. There's no getting around having to read the ufraw-batch documentation for it's important that you understand what needs to be passed to the program.

Also, you need to be aware that if you use the ufraw standalone program (ufraw), it usually saves its processing settings into a file from which ufraw-batch creates its default settings. This may lead to undesirable conversion results. For example, if you were processing an underexposed image in ufraw, you may end up with overexposed JPGs the next time you use ufraw-batch due to the exposure compensation you used in your ufraw processing.

My personal recommendation is to pull a normally exposed RAW file into ufraw, set the exposure compensation to automatic, camera white balance to camera, and then reset all other settings to their default values. Be sure to set "Save image defaults" to "Just this once" on the save tab and then save the image. Then use ufraw-batch and see if the converted JPGs look the way you want them to. If not, make adjustments in ufraw to your base image and then save it again. Repeat the refinement process until you like what you see in the converted JPGs. Then copy the ufraw configuration file (.ufrawrc in linux) somewhere so you can use it as a default whenever you use ufraw-batch, e.g., have a command in your PHP script to copy the default .ufrawrc file over whatever one is currently there.

Alternatively, you can send ufraw-batch various parameters to alter your conversion results. For example, I took a series of photos on a dark overcast day which came out slightly underexposed. I used the following parameters to tell ufraw-batch to up brighten them up a bit, add some contrast, and cut back on the saturation.

	$cmd = "ufraw-batch --wb=camera --base-curve=camera --black-point=0.08
--curve=linear --exposure=0.64 --linearity=0.01 --saturation=0.76 -
-out-type=jpeg --compression=100 --exif --overwrite {$pathname}/{$file}";

Learn all you can about your software tools for understanding how and why they work allows you to put them to optimal use!

To finish up our RAW batch conversion program, we add the code that calls our recursive ufraw function.

First, we put up a syntax prompt to remind us how to use the program.

	if (empty ($argv[1]))
{
echo "\nSYNTAX: {$argv[0]} directory_to_process";
echo "\n\tdirectory_to_process: MUST be a complete pathname!";
echo "\n\n\ti.e., {$argv[0]} /home/mystuff/photos\n\n";
exit();
}

Next, we put any arguments we pass to the recursive function into some variables we can use. This is mostly for readability--we could simply use these arguments in our function calls. Please note that we don't test the order of the arguments. Therefore, make sure you pass the arguments into the program in the required order!

	$extension = "nef";
$pathname = $argv[1];

Now, we change to the directory we want to process.

	chdir ($pathname);
echo "\n";

Finally, we process any RAW files we have there.

	read_directory ($pathname, $extension);
echo "\n[DONE]\n\n";

The complete program code is in the attachment below.