Interesting question! Gimp includes a TCP Script-Fu server that we can run from the command-line:
gimp -i -b '(plug-in-script-fu-server 1 "127.0.0.1" 10008 "/path/to/log")'
This starts a headless server that listens on 127.0.0.1 at port 10008. It's the same server we can start through the UI under Filters → Script-Fu → Start Server.... You might try adding the -d
option to skip loading patterns, gradients, palettes, and brushes, or the -f
option to skip fonts (for faster start-up and lower memory usage). We can then send Script-Fu statements written in Scheme to that socket. Here's how with PHP:
$script = '(gimp-message "Hello Gimp!")';
$scriptLength = strlen($script);
$highByte = (int)($scriptLength / 256);
$lowByte = $scriptLength % 256;
$packet = pack('C3a*', ord('G'), $highByte, $lowByte, $script);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, '127.0.0.1', 10008) or die('connect error');
socket_write($socket, $packet, strlen($packet)) or die('write error');
socket_close($socket);
This implements the (really simple) protocol defined in that link up there. It needs much better error-handling for any serious use, but it's fine for a demo. If we invoke this script, we should see connection information in the server log file, and the console should display:
Start Server-Warning: Hello Gimp!
In the UI, this would open a dialog window with that message. Depending on your environment, Gimp or PHP may not have socket creation capabilities. You might try running both as root just to test it out.
As cool as this is, I do sympathize with other commenters: ImageMagick or GD may be better choices for a large application. I hear your concerns, but Gimp is not designed for web-scale deployments. It's foremost a desktop application and uses a lot of resources. If security is a concern, Gimp may expose attack surfaces that we don't normally see in web environments (including, perhaps, a server that executes arbitrary Scheme code!). You may also want to consider the cost of maintaining a homegrown client library used to interact with Gimp.
That said, if you can justify this project, I think it would be a lot of fun.
Notes:
- As you may know, Gimp also includes a Python Script-Fu interpreter. We could extend that to create a more robust server in Python.
- We could attribute part of the loading time improvement you experience when running a second instance of Gimp to the filesystem cache for recently-accessed files.
- Gimp also utilizes X11's D-Bus to dramatically improve loading time when an instance is already running. These facilities may not be available in a bare server environment.