Monday, May 28, 2012

Yet Another Way for Detecting Height x Width for an Image in PHP with ImageMagick

The PHP function 'getImagePage' will return values indicating the height and width of an image, just like the getHeight and getWidth, except these values are returned as an array.  There's also two other values returned in the array with the key values of 'x' and 'y'.  After running this function on animated .Gif's, super-compressed jpeg's, bmp's, png's, and every type of awkward file format you could think of, I kept getting back the result of '0' for both the 'x' and 'y' associated values in the returned array.  It's likely that these indicate the "start-positions" of the width and height, compared to the "end-values" you receive for accessing the values associated with the keys 'width' and 'height'.

Oddly enough, the PHP function 'getPage' will return an array with the same exact keys, except the values all default to '0', making it much less useful than the function 'getImagePage'.

And some sample code :

<?php

            // Author: holdoffhunger@gmail.com
   
        // Imagick Type
        // ---------------------------------------------

    $imagick_type = new Imagick();
   
        // Open File
        // ---------------------------------------------
       
    $file_to_grab = "image_workshop_directory/test.gif";
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
   
        // Grab File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
   
        // Get Image Page
        // ---------------------------------------------
       
    $image_page = $imagick_type->getImagePage();
   
        // Print Iteration Value Interpreted
        // ---------------------------------------------

    print_r($image_page);

?>

Expected Result :

Array
(
    [width] => 600
    [height] => 450
    [x] => 0
    [y] => 0
)

Official Function Page: http://www.php.net/manual/en/imagick.getimagepage.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php

Saturday, May 26, 2012

Detecting an Animate-able Image in PHP for Linux Server Image-Processing

By using the PHP function getImageIterations, you'll receive back a value indicating the animated nature of the image.  You'll get back a '0' for a still image that is not animate-able (like a .BMP or a .JPEG file) and a '1' for an animate-able image (like an animated .GIF file).

I have been unable to get any other results from this function, after extensive use.  There is some discussion among the ImageMagick user's group saying that the Iterations should indicate the number of times an animated .Gif file repeats itself.  However, it's possible that either modern browsers default the value to infinity or that this ImageMagick functionality is only available at the Linux command-line.  See more at the discussion group here: http://studio.imagemagick.org/pipermail/magick-users/2002-October/005814.html

And some sample code :

<?php

            // Author: holdoffhunger@gmail.com
   
        // Imagick Type
        // ---------------------------------------------

    $imagick_type = new Imagick();
   
        // Open File
        // ---------------------------------------------
       
    $file_to_grab = "image_workshop_directory/test.gif";
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
   
        // Grab File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
   
        // Get Image Iterations
        //    (Detect Animated Image Versus Non-Animated Image)
        // ---------------------------------------------
       
    $image_iterations = $imagick_type->getImageIterations();
   
        // Print Iteration Value Interpreted
        // ---------------------------------------------
   
    if($image_iterations == 1)
    {
        print("$file_to_grab *IS* an animate-able image.");
    }
    else
    {
        print("$file_to_grab *IS NOT* an animate-able image.");
    }

?>

Official Function Page: http://www.php.net/manual/en/imagick.getimageiterations.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php

Wednesday, May 23, 2012

Get the Maximum Memory Use Allowed in PHP for Linux Server Image-Processing

By using the PHP function getResourceLimit, you'll be given the maximum allowed amount of a particular type of resource.  The integer returned is the number of bytes allowed to the resource you specified in the input parameter.  For the input parameter options, you have the predefined ResourceType constants for the ImageMagick package.  In the code, they look like imagick::RESOURCETYPE_AREA, but you have "_VALUE" options of: undefined, area, disk, file, map, and memory.

What do each of these particular values represent?  The ImageMagick official documentation is helpful in that matter.  File indicates "maximum number of open pixel cache files", Area indicates "maximum area in bytes of any one image that can reside in the pixel cache memory", Memory indicates "maximum amount of memory in bytes to allocate for the pixel cache", map indicates "maximum amount of memory map in bytes to allocate for the pixel cache", and disk indicates "maximum amount of disk space in bytes permitted for use by the pixel cache".  This is according to the Official ImageMagick Architecture page: http://www.imagemagick.org/script/architecture.php .

The Official ImageMagick Resource page has more information on how these parameters function.  For instance, the files limit documentation mentions that when a user goes over the limit, any more files that are "cached to disk are closed and reopened on demand."  (Again, a performance hit.)  See that page here: http://www.imagemagick.org/script/resources.php .

What happens when someone exceeds their limit?  It doesn't cause the PHP script to error out, but simply, it relocates their user activity to non-cache memory (i.e.: virtual memory, which is slow).  So, even if you're worried about the limit, it really only indicates the point in a server at which a user's request is devalued compared to other user requests.

And remember, you can always set the defaults in the policy.xml file on your own server.

Some sample code :

<?php

            // Author: holdoffhunger@gmail.com
   
        // Imagick Type
        // ---------------------------------------------

    $imagick_type = new Imagick();
   
        // Open File
        // ---------------------------------------------
       
    $file_to_grab = "image_workshop_directory/test.gif";
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
   
        // Grab File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
   
        // Get/Display Resource Values
        // ---------------------------------------------
   
    print("Undefined: ");
    print($imagick_type->getResourceLimit(imagick::RESOURCETYPE_UNDEFINED));

    print("<br><br>Area: ");
    print($imagick_type->getResourceLimit(imagick::RESOURCETYPE_AREA));
   
    print("<br><br>Disk: ");
    print($imagick_type->getResourceLimit(imagick::RESOURCETYPE_DISK));
   
    print("<br><br>File: ");
    print($imagick_type->getResourceLimit(imagick::RESOURCETYPE_FILE));
   
    print("<br><br>Map: ");
    print($imagick_type->getResourceLimit(imagick::RESOURCETYPE_MAP));
   
    print("<br><br>Memory: ");
    print($imagick_type->getResourceLimit(imagick::RESOURCETYPE_MEMORY));

?>

Official Function Page: http://www.php.net/manual/en/imagick.getresourcelimit.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php

Tuesday, May 22, 2012

Get the Number of Frames in a GIF File in PHP with the ImageMagick Package

The getIteratorIndex function of the ImageMagick function works on any type of image, but it is built for measuring the number of frames in an animated .gif file.  For non-animated image files, like regular .bmp or .jpg files, this function will always return the number '0', meaning that there is only one frame.  Counting starts from zero with this function, so a .gif file with five different frames will return a value of '4' on this.  Highly repetitive, but psychedelic animated GIF's are often anywhere between 10 and 30 frames, but a .gif file that is nothing more than video footage converted to a datafile may have hundreds of frames.

According to Wikipedia (in the article for "Graphics Interchange Format"), an animated GIf image is an image that uses the GIF standard "which allows various images (frames) in the file to be painted with time delays."  This function won't get you the amount of time that delays between each frame, but it will give you the number of unique frames in a .gif file.  This will tell you how complicated or simple the animation may be.

Is the 'getIteratorIndex' function not working for you?  Try the 'getImageIndex' function, which produces the same exact result.

Some sample code :

<?php

            // Author: holdoffhunger@gmail.com
   
        // Imagick Type
        // ---------------------------------------------

    $imagick_type = new Imagick();
   
        // Open File
        // ---------------------------------------------
       
    $file_to_grab = "image_workshop_directory/test.gif";
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
   
        // Grab File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
   
        // Get Image Type Value
        // ---------------------------------------------
       
    $image_iterator_index = $imagick_type->getIteratorIndex();
   
        // Print Image Type Value
        // ---------------------------------------------
       
    print("Number of Unique Frames in the .GIF File: $image_iterator_index");

?>

Official Function Page: http://www.php.net/manual/en/imagick.getiteratorindex.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php

Monday, May 21, 2012

Understanding Image Type in PHP with the ImageMagick Package

The function PHP 'getImageType' will return an integer, with that value being equal to the evaluated value of the IMGType Constants as defined by the ImageMagick class.  When accessing them, they look like "imagick::IMGTYPE_PALETTE", but with "_VALUE" values of: undefined, bilevel, grayscale, grayscalematte, palette, palettematte, truecolor, truecolormatte, colorseparation, colorseparationmatte, and optimize.  If you were to print out these values, you would have '0' for undefined, '1' for bilevel, '2' for grayscale, and so on.

For a BMP picture drawn in paint, I got back the value # 4 - Palette.  For a BMP or JPEG photograph of the ocean or jungle, I got back the value # 6 - Truecolor.  For an animated GIF file, I got back the value # 5 - Palette Matte.  For a black-and-white JPEG drawing, I got back the value # 2 - Grayscale.  These are the most common results I have gotten back with this function.

Some sample code :

<?php

            // Author: holdoffhunger@gmail.com
   
        // Imagick Type
        // ---------------------------------------------

    $imagick_type = new Imagick();
   
        // Open File
        // ---------------------------------------------
       
    $file_to_grab = "image_workshop_directory/test.bmp";
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
   
        // Grab File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
   
        // Get Image Type Value
        // ---------------------------------------------
       
    $image_type = $imagick_type->getImageType();
   
        // Interpret Image Type Value
        // ---------------------------------------------

    switch($image_type)
    {
        case imagick::IMGTYPE_UNDEFINED:
            $image_type_title = "Undefined";
            break;
           
        case imagick::IMGTYPE_BILEVEL:
            $image_type_title = "Bilevel";
            break;
           
        case imagick::IMGTYPE_GRAYSCALE:
            $image_type_title = "Grayscale";
            break;
           
        case imagick::IMGTYPE_GRAYSCALEMATTE:
            $image_type_title = "Grayscale Matte";
            break;
           
        case imagick::IMGTYPE_PALETTE:
            $image_type_title = "Palette";
            break;
           
        case imagick::IMGTYPE_PALETTEMATTE:
            $image_type_title = "Palette Matte";
            break;
           
        case imagick::IMGTYPE_TRUECOLOR:
            $image_type_title = "Truecolor";
            break;
           
        case imagick::IMGTYPE_TRUECOLORMATTE:
            $image_type_title = "Truecolor Matte";
            break;
           
        case imagick::IMGTYPE_COLORSEPARATION:
            $image_type_title = "Color Separation";
            break;
           
        case imagick::IMGTYPE_COLORSEPARATIONMATTE:
            $image_type_title = "Color Separation Matte";
            break;
           
        case imagick::IMGTYPE_OPTIMIZE:
            $image_type_title = "Optimize";
            break;
    }
   
        // Print Image Type Value
        // ---------------------------------------------
       
    print("# $image_type - $image_type_title");

?>

Official Function Page: http://www.php.net/manual/en/imagick.getimagetype.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php

Understanding Image SHA-256 Hash Values in PHP with the ImageMagick Package

The getImageSignature function returns the SHA-256 hash value, which is 256 bits (or 32 bytes) in length.  SHA-256 is part of the SHA-2 set of cryptographic hash functions designed by the NSA, which also includes SHA-224, SHA-384, and SHA-512.  According to Wikipedia, there are some security flaws in it similar to the set of SHA-1 hash functions, which should be fixed with SHA-3, eventually.  Unlike MD5 or the SHA-1 set of cryptographic functions, SHA-2 has had no collisions discovered yet (a collision is an incident where two different pieces of data result in the same hash value from the hashing function).  For the time being, it seems to be the most efficient method for creating a small (32-byte), uniquely-identifiable, generally-secure value for either a file or a piece of data.

Some sample code :

<?php

            // Author: holdoffhunger@gmail.com
   
        // Imagick Type
        // ---------------------------------------------

    $imagick_type = new Imagick();
   
        // Open File
        // ---------------------------------------------
       
    $file_to_grab = "image_workshop_directory/test.bmp";
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
   
        // Grab File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
   
        // Get Image SHA-256 Signature / Hash Value
        // ---------------------------------------------
       
    $imagick_type_signature = $imagick_type->getImageSignature();
   
        // Print Image Signature / Hash Value
        // ---------------------------------------------
       
    print($imagick_type_signature);

?>

Results of this done on a standard BMP image :

cb2f387a7b23d11340ad1f5ba9c765125ea6b2d50a0d25412abe1ce568adac68

Official Function Page: http://www.php.net/manual/en/imagick.getimagesignature.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php

Saturday, May 19, 2012

Understanding Image Properties in PHP with the ImageMagick Package

The getImageProperties function in PHP returns an array of property keys available for an image.  To get these property values, you use getImageProperty function, giving it one of the available keys provided by the getImageProperties function result.  For some images, you may have a lot of properties and for some, you may have few.  The two that almost every image seems to have are "date:create" and "date:modify", but some images may have forty or more properties, some titled "exif:Compression", "photoshop:Credit", "jpeg:colorspace", "rdf:Alt", "stRef:documentID", and "xap:CreatorTool."  PNG files will also have properties like "png:IHDR.bit_depth" and "png:IHDR.width,height."  So far, it appears generally that GIF and BMP files, being simpler, have fewer properties, whereas JPEG and PNG files, being more complicated, have much wider array of properties.  It seems incredibly useful in document management.

And now, some sample code and results :

<?php

            // Author: holdoffhunger@gmail.com
   
        // Imagick Type
        // ---------------------------------------------

    $imagick_type = new Imagick();
   
        // Open File
        // ---------------------------------------------
       
    $file_to_grab = "image_workshop_directory/test.png";
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
   
        // Grab File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
   
        // Get Image Properties
        // ---------------------------------------------
       
    $imagick_type_properties = $imagick_type->getImageProperties('*', FALSE);
   
        // Print Image Properties
        // ---------------------------------------------
   
    print("<pre>");
       
    print_r($imagick_type_properties);
   
        // Print Each Individual, Image Property
        // ---------------------------------------------
   
    foreach($imagick_type_properties as $value)
    {
        print("$value --- ");
        print($imagick_type->getImageProperty("$value"));
        print("<br><br>");
    }
               
    print("</pre>");

?>

Results of this done on a standard PNG image :

Array
(
    [0] => date:create
    [1] => date:modify
    [2] => png:cHRM                
    [3] => png:gAMA                
    [4] => png:IHDR.bit_depth      
    [5] => png:IHDR.color_type     
    [6] => png:IHDR.interlace_method
    [7] => png:IHDR.width,height   
    [8] => png:sRGB                
)
date:create --- 2012-05-19T18:26:45-05:00

date:modify --- 2012-05-19T18:26:45-05:00

png:cHRM                  --- chunk was found (see Chromaticity, above)

png:gAMA                  --- gamma=0.45455 (See Gamma, above)

png:IHDR.bit_depth        --- 8

png:IHDR.color_type       --- 2

png:IHDR.interlace_method --- 0

png:IHDR.width,height     --- 320, 320

png:sRGB                  --- intent=0 (See Rendering intent)

Official Function Page: http://www.php.net/manual/en/imagick.getimageproperties.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php

Thursday, May 17, 2012

Understanding Image Format in PHP with the ImageMagick Package

The getFormat function merely returns the value of the image format, which is not automatically loaded when you read an image.  The value you're getting back, then, is merely the value that you set the format to using the setFormat function.  That function takes pretty much any string that matches a popular or well-known image filename.  It accepts "jpeg" and "gif", but errors out on anything that is not an image filename, like "xyz123" or "zip".  How do you know what filetypes you can enter?  Use the PHP ImageMagick function 'queryFormats'.  If you want to know what type of file you have, you should try using the PHP page for the function 'filetype'.  Or, on the other hand, you can use the ImageMagick function getImageFormat (as opposed to the ImageMagick function getFormat).  The difference is that getImageFormat actually returns the format of the inputted image.

And now, a simple demonstration of the set/get activity in the Format :

<?php

            // Author: holdoffhunger@gmail.com
   
        // Imagick Type
        // ---------------------------------------------

    $imagick_type = new Imagick();
   
        // Open File
        // ---------------------------------------------
       
    $file_to_grab = "image_workshop_directory/test.bmp";
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
   
        // Grab File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
   
        // Set/Get Format
        // ---------------------------------------------
       
    $imagick_type->setFormat("bmp");
    $imagick_type_format = $imagick_type->getFormat();
   
        // Print Results
        // ---------------------------------------------

    print("<pre>");
    print($imagick_type_format);
    print("</pre>");

?>

Official Function Page: http://www.php.net/manual/en/imagick.getformat.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php

Wednesday, May 16, 2012

Understanding Image Compression Type in PHP with the ImageMagick Package

The ImageMagick PHP function for getCompression returns an integer representing the value associated with the ImageMagick Compression constant.  You'll get numbers from 0 to 13, each representing a different particular type of compression.  If printed out, the ImageMagick constants for compression come out as...

imagick::COMPRESSION_UNDEFINED    0
imagick::COMPRESSION_NO    1
imagick::COMPRESSION_BZIP    2
imagick::COMPRESSION_DXT1    3
imagick::COMPRESSION_DXT3    4
imagick::COMPRESSION_DXT5    5
imagick::COMPRESSION_FAX    6
imagick::COMPRESSION_GROUP4    7
imagick::COMPRESSION_JPEG    8
imagick::COMPRESSION_JPEG2000    9
imagick::COMPRESSION_LOSSLESSJPEG    10
imagick::COMPRESSION_LZW    11
imagick::COMPRESSION_RLE    12
imagick::COMPRESSION_ZIP    13

Every time I have used this, whether on a jpeg image, a png image, a gif image, or a bmp image, it has always returned '0' as a value.  There's a good chance that this is simply a value that is set by means of get/set, as opposed to actually producing values for a given image.

Some sample code :

<?php

            // Author: holdoffhunger@gmail.com
   
        // Imagick Type
        // ---------------------------------------------

    $imagick_type = new Imagick();
   
        // Open File
        // ---------------------------------------------
       
    $file_to_grab = "image_workshop_directory/test.bmp";
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
   
        // Grab File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
   
        // Get Quantum Range
        // ---------------------------------------------
       
    $imagick_type_compression = $imagick_type->getCompression();
   
        // Print Results
        // ---------------------------------------------

    print("<pre>");
    print($imagick_type_compression);
    print("</pre>");

?>

Official Function Page: http://www.php.net/manual/en/imagick.getcompression.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php

Tuesday, May 15, 2012

Understanding Image Kurtosis and Skewness in PHP with the ImageMagick Package

The getImageChannelKurtosis function accepts as a parameter any of the ImageMagick channel constants and returns an array containing elements with the two element values of 'kurtosis' and 'skewness.'

For the inputted color values, you have the ImageMagick channel constant values that look like imagick::CHANNEL_UNDEFINED, with "_VALUE" values of: undefined, red, gray, cyan, green, magenta, blue, yellow, alpha, opacity, matte, black, index, all, and default.  This is for measuring the particular channel for these colors of their own associated colorspaces.

According to Wikipedia, the Kurtosis is "any measure of the 'peakedness' of the probability distribution of a real-valued random variable" and Skewness (its opposite) is "a measure of the asymmetry of the probability distribution of a real-valued random variable."  The more colors vary from the inputted channel constant, you'll be more likely to get a skewness close to zero, and you'll get the opposite behavior from checking the kurtosis value, which makes sense, since skewness and kurtosis are considered mathematical antonyms.

Some sample code :

<?php

            // Author: holdoffhunger@gmail.com
   
        // Imagick Type
        // ---------------------------------------------

    $imagick_type = new Imagick();
   
        // Open File
        // ---------------------------------------------
       
    $file_to_grab = "image_workshop_directory/test.bmp";
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
   
        // Grab File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
   
        // Get Quantum Range
        // ---------------------------------------------
       
    $imagick_type_channel_kurtosis_red = $imagick_type->getImageChannelKurtosis(imagick::CHANNEL_RED);
   
        // Print Results
        // ---------------------------------------------

    print("<pre>");
    print_r($imagick_type_channel_kurtosis_red);
    print("</pre>");

?>

The results for performing this function upon a photo of the ocean...

ImageMagick - Channel Kurtosis
  Channel - Undefined :

Kurtosis : 0
Skewness : 0

  Channel - Red :

Kurtosis : 0.920611158888
Skewness : 1.52701528976

  Channel - Gray :

Kurtosis : 0.920611158888
Skewness : 1.52701528976

  Channel - Cyan :

Kurtosis : 0.920611158888
Skewness : 1.52701528976

  Channel - Green :

Kurtosis : -0.286520845928
Skewness : 1.05203603783

  Channel - Magenta :

Kurtosis : -0.286520845928
Skewness : 1.05203603783

  Channel - Blue :

Kurtosis : -0.849997917796
Skewness : 0.807852655795

  Channel - Yellow :

Kurtosis : -0.849997917796
Skewness : 0.807852655795

  Channel - Alpha :

Kurtosis : 0
Skewness : 0

  Channel - Opacity :

Kurtosis : 0
Skewness : 0

  Channel - Matte :

Kurtosis : 0
Skewness : 0

  Channel - Black :

Kurtosis : 0
Skewness : 0

  Channel - Index :

Kurtosis : 0
Skewness : 0

  Channel - All :

Kurtosis : 0.509521246195
Skewness : 1.33491479552

  Channel - Default :

Kurtosis : -0.349955122843
Skewness : 1.00561407035

Official Function Page: http://www.php.net/manual/en/imagick.getimagechannelkurtosis.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php

Monday, May 14, 2012

Understanding Quantum Range and Depth in PHP with the ImageMagick Package

The getQuantumRange is a useful function, since many of the ImageMagick functions accept parameters from 0 to a maximum of the Quantum Range.  When getting a return value for this, it doesn't return a string.  It actually returns an array, with one type being a String ('quantumRangeLong') and the other type being a Long Int ('quantumRangeString').  The Quantum value (the difference between the high and the low) is the maximum number of color values expressible in a single pixel within an image.  Sometimes, this is called the number of unique colors expressible in a single pixel.  Here's some sample code and the results, given a color, BMP file photograph that is 600x450 pixels...

<?php

            // Author: holdoffhunger@gmail.com
   
        // Imagick Type
        // ---------------------------------------------

    $imagick_type = new Imagick();
   
        // Open File
        // ---------------------------------------------
       
    $file_to_grab = "image_workshop_directory/test.bmp";
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
   
        // Grab File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
   
        // Get Quantum Range
        // ---------------------------------------------
       
    $imagick_type_quantum_range = $imagick_type->getQuantumRange();
   
        // Print Results
        // ---------------------------------------------

    print("<pre>");
    print_r($imagick_type_quantum_range);
    print("</pre>");

?>

Output:

Array
(
    [quantumRangeLong] => 65535
    [quantumRangeString] => 65535
)

The getQuantumDepth works much like the other "Depth" functions and much like the getQuantumRange function.  Like the Depth functions, it returns a value indicating the number of bits to store the unique color values -- 16-bit = 2^16 unique colors, which is 65,535 (counting starts at '0' in binary, as opposed to '1' in decimal).  Like the getQuantumRange function, it returns an array with two values, one being a long integer ('quantumDepthLong') and the other being a string ('quantumDepthLong').  The only difference, besides the variable types, is that the string version has a 'Q' prefixed to it.

Here's some sample code and the results, given a color, BMP file photograph that is 600x450 pixels...

<?php

            // Author: holdoffhunger@gmail.com
   
        // Imagick Type
        // ---------------------------------------------

    $imagick_type = new Imagick();
   
        // Open File
        // ---------------------------------------------
       
    $file_to_grab = "image_workshop_directory/test.bmp";
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
   
        // Grab File
        // ---------------------------------------------

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);
   
        // Get Quantum Depth
        // ---------------------------------------------
       
    $imagick_type_quantum_depth = $imagick_type->getQuantumDepth();
   
        // Print Results
        // ---------------------------------------------

    print("<pre>");
    print_r($imagick_type_quantum_depth);
    print("</pre>");

?>

Output:

Array
(
    [quantumDepthLong] => 16
    [quantumDepthString] => Q16
)

Official Function Page: http://www.php.net/manual/en/imagick.getquantumrange.php and http://www.php.net/manual/en/imagick.getquantumdepth.php

// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted.  http://www.hacktivismo.com/about/hessla.php