View Single Post
Originally Posted by Fee View Post
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.

Checking every pixel would not be that slow, 512x512 textures have only 262144 pixels, which is pretty small for a computer to check, it would take at worst a few milliseconds.
Originally Posted by dengue View Post
nice one man,
I recommend a list to deal with that and make it scan every neighbour pixel.
with this you would not be so messed with sequential overhead.

I know sequential loops are a bit of overhead as the complexity grows as a power of loops count.

with lists you would do the same as the bucket fill of computer graphics libs,
make sure to use graphics processor.

Most servers don't have GPUs though, sequential accesses are the fastest BTW, checking every pixel wouldn't be that slow, it would take a few milliseconds at worst.