All of the above works and now I've got it to store the images it creates and added a check to see if the image is there before it tries to create it too. The linked example has been updated with all the code but here's the new product-image.php file for anyone that's interested.
$style = "0001"; // Dummy values to avoid errors
$color1 = array(247,228,064);
$color2 = array(031,076,146);
$templatePath = "../templates/"; // Relative path from this file to your templates
if (isset($_GET["style"])) {
$style = $_GET["style"]; // Replace $style with real value if recieved
}
if (isset($_GET["color1"])) {
$color1 = explode(',', $_GET["color1"]); // Replace $color1 with real RGB array if recieved
}
if (isset($_GET["color2"])) {
$color2 = explode(',', $_GET["color2"]); // Replace $color2 with real RGB array if recieved
}
// Create unique output file name by concatenating all numerical values eg:0001247228522562146.png
$outputFileName = $style . implode("", $color1) . implode("", $color2) . ".png";
// Check if the image we want already exists
if (file_exists($outputFileName)) {
// If it does then open the file in a binary mode
$fp = fopen($outputFileName, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($outputFileName));
// dump the picture and stop the script
fpassthru($fp);
exit;
} else { // If it doesn't already exist then lets create the image...
$colorFile = $templatePath . $style . "colors.gif";
$outlineFile = $templatePath . $style . "outline.png";
$image_1 = imagecreatefromgif($colorFile); // Load image with coloured sections
$image_2 = imagecreatefrompng($outlineFile); // Load image with outlines
imagealphablending($image_1, true);
imagesavealpha($image_1, true);
imagetruecolortopalette($image_1, false, 255);
$region1 = imagecolorclosest ( $image_1, 255,0,0); // Get Primary (red) region
imagecolorset($image_1, $region1, $color1[0], $color1[1], $color1[2]); // Set new colour for $region1
$region2 = imagecolorclosest ( $image_1, 0,0,255); // Get Secondary (blue) region
imagecolorset($image_1, $region2, $color2[0], $color2[1], $color2[2]); // Set new colour for $region2
$merged_image = imagecreatetruecolor(339, 390); // Create a true color canvas
imagecopy($merged_image, $image_1, 0, 0, 0, 0, 339, 390); // Merge the newly coloured sections
imagecopy($merged_image, $image_2, 0, 0, 0, 0, 339, 390); // Merge the outlines on top
header("Content-type: image/png"); // Tell browser to expect PNG
imagepng($merged_image, $outputFileName); // Save new PNG to server
imagedestroy($image_1); // Tidy up
imagedestroy($image_2);
imagedestroy($merged_image);
// open the image we just created in a binary mode
$fp = fopen($outputFileName, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($outputFileName));
// dump the picture and stop the script
fpassthru($fp);
exit;
}