Search code examples
phpimagemagickimagick

Determine max possible intensity of image


Starting to use the PHP Imagick classes and the lack of documentation. In particular using the paintTransparentImage method.

In the CLI version of ImageMagick one can simply pass a percentage for the fuzz parameter. However, in PHP it seems you have to specify it as an amount relative to the "maximum possible intensity" of the image.

How do you determine the max possible intensity of an image then? Apparently it can be 255, 65535, or 4294967295.


Solution

  • With PHP you would determine the quantum range, and calculate the intensity with pow(2,Q)

    $img = new Imagick('source.png');
    $quantum = $img->getQuantumDepth()['quantumDepthLong'];
    $target = 'black';
    $alpha = 0.0; // Fully transparent
    $fuzz = 0.5 * pow(2,$quantum); // From black to gray50
    $img->paintTransparentImage($target, $alpha, $fuzz);
    

    From the above comments, the maximum intensity can be mapped by the quantum sizes

    +---------------+-------------+---------------+
    | Quantum Range | Packet Size | Max Intensity |
    +---------------+-------------+---------------+
    |  8            |  32 bits    |        255    |
    | 16            |  64 bits    |      25535    |
    | 32            | 128 bits    | 4294967295    |
    +---------------+-------------+---------------+