1

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

Rayjax
  • 6,614
  • 11
  • 54
  • 77
  • 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 Oct 31 '16 at 14:40
  • maybe this can help https://github.com/thephpleague/color-extractor – Fky Oct 31 '16 at 14:48
  • @deceze sure, but a C lib called by PHP or node may do it quite well actually – Rayjax Oct 31 '16 at 15:04
  • "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) http://stackoverflow.com/questions/13569887/libpng-palette-png-with-alpha-or-not/13570973#13570973 – leonbloy Oct 31 '16 at 17:03

1 Answers1

1

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();
Fky
  • 2,053
  • 1
  • 12
  • 21