My site accepts file uploads, including PNGs with an alpha layer. Normally I'd add a white background and save it as a JPEG for web viewing, but if the original image is almost pure white then it would be better to put it on a dark background instead.
My server is running ImageMagick 6.0.7 and I'm executing commands through PHP's exec() function.
Right now I've come up with this method which sort of works:
Code: Select all
# Execute the "identify -verbose" command and store the results in $results
exec("identify -verbose " . $filename, $results);
# Go through and check $results
foreach ($results as $key => $result) {
if (eregi("Mean:", $result)) {
# If this $results line contains a Mean value for R, G, or B then store it in an array
$channel_mean[$i] = str_replace("Mean: ", '', $result);
$i++;
}
}
if ($channel_mean[0] > 250 && $channel_mean[1] > 250 && $channel_mean[2] > 250) {
# It's probably almost completely white since the R, G, and B Mean values are all over 250... so give it a dark background instead of white
}
- It doesn't seem to work on PNG-8 image files
- If the uploaded image is largely blank alpha area with a few specks of color (not necessarily white), it still gets flagged as almost white
Can anyone think of a better way to do this? I'm kind of stuck.
Thanks!