I think this would actually be a massive headache to implement. I can't see a way of implementing this without stepping through every pixel in an image (which is sure to significantly slow upload - especially for those people who choose to use a .PNG without transparency).
I guess the first thing to do would be to check if a particular PNG image contains alpha or not:
<?php
function get_colourtype($file){
return (ord(@file_get_contents($file, NULL, NULL, 25, 1,)) ==6);
}
?>
This should work because the color-type of a PNG image is stored at byte offset 25. If the 25th byte is 6 the colour-type is RGB + alpha. This will only work for PNGs however.
The above function will detect, at the very least, if a PNG will allow transparency, whether there actually is transparency present is another matter entirely. But at least with the function above we can make a semi-intelligent guess and prevent the overhead of checking unnecessarily.
The next step however, is unfortunately to perform a complete search, pixel-by-pixel. I cannot think of another way to do it. I guess if users who choose to use PNGs for texture upload with a RGB + alpha colour type were okay with longer wait times then this solution might be acceptable.
<?php
function has_Transparency($imageData){
//Get the width and height of the image for looping
$w =imagesx($imageData);
$h = imagesy($imageData);
//Define an acceptable level of transparency
$cutOff = ($w * $h) * 0.7;
$transparentCounter = 0;
//Iterate through pixels and amend transparency counter.
//Break loop if cut off point (70% is reached).
for($i = 0; $i < $w; $i++){
for($j = 0; $j < $h; j++){
$rgba = imagecolorat($imgData, $i, $j);
if(($rgba & 0x7F000000) >> 24) $transparentCounter++;
if($transparentCounter == $cutOff) return true;
}
}
return false;
}
?>
That should work (I haven't tested it), although it is not going to be particularly efficient. There is probably a more intelligent way to search for transparency, but I'm starting to think that'll be beyond the effort of just having users report misuse and dealing with it then.