1
votes

I am looking for a fast an reliable way of knowing if a PNG file hosted on an HTTP server is fully transparent (not even one pixel has color).

The PHP script doing that would have to process tens of thousands of images so it must be as fast as possible. I cannot afford a manual PHP for loop on all the pixels.

The images will be roughly 300x300px. They will be in PNG-8 format, so transparency is present but without alpha channel. I can use GD of course.

(If there is absolutely no way with PHP i can still switch to Node.js)

Fully Transparent image file example Transparent image

Not fully transparent image file example Not fully transparent image

1
If speed is your main concern, a language like C should be what you're looking for. Neither node nor PHP will come close in terms of performance.deceze
@deceze sure, but a C lib called by PHP or node may do it quite well actuallyRayjax
"They will be in PNG-8 format, so transparency is present but without alpha channel. " You still have two different cases to consider (indexed or not) stackoverflow.com/questions/13569887/…leonbloy

1 Answers

1
votes

Usefull library : http://github.com/thephpleague/color-extractor

Basic usage :

require 'vendor/autoload.php';

use League\ColorExtractor\Client as ColorExtractor;

$client = new ColorExtractor;

$image = $client->loadPng('./some/image.png');

// Get the most used color hexadecimal codes from image.png
$palette = $image->extract();