Sunday, April 29, 2012

Peform Polaroid-Image Effect on Image using ImageMagick in PHP

At first, the polaroidImage function looked amazingly simple.  "It must simply give a border of a specified color, and then rotate the given image by a certain number of degrees."  It actually turns out to be a really neat function.  Not only does it create an image that looks like a Polaroid of the targetted file, but it gives it a slight bend and curl that simulates a real, physical photograph.  On top of that, it gives a minor, fuzz shadow to some of the borders that accentuate the entire "effect" of looking at a real, 3d photograph.  There are some essentials to know, though!

The first parameter, $properties, has absolutely no effect upon the image that I can tell.  The official documentation, in this point, is absurdly incorrect.  Ignore the first parameter -- it's worthless.  I suspect that this is a miswrite, and what is implied is that you need to edit the properties of the Imagick class object that you're doing the PolaroidImage to.  There are two colors: the border of the image, and the color of shadow.  Both are set with the setImageBorderColor and setImageBackgroundColor functions, conveniently.  Unfortunately, this function still seems to demand a blank value for "properties", and NULL doesn't do it apparently.

One final note of precaution: the color of the surface that the photograph lies on defaults to black for BMP files, and then to white to PNG files. (Weird, huh?)  (Update:  This appears to be in how browsers decide to render opacity in the background of the image, which I have not figured out how to set in this function.)  Others have noticed this as well, on what appears to be the best source of info on this function: http://valokuva.org/?p=37 .

The angle rotates clockwise.  If you want to rotate counter-clockwise, it's easy: just use negative numbers, which this function accepts.

Now, for a brief demonstration of this function, with some fairly decent, standard parameters that should fit anyone's needs:

<?php

        // Grab Image File Data
        // ---------------------------------------------

    $file_to_grab_with_location = "image_workshop_directory/test.png"
   
        // Create ImageMagick Object Types
        // ---------------------------------------------

    $imagick_type = new Imagick();
    $draw_type = new ImagickDraw();
   
        // Open File
        // ---------------------------------------------
   
    $file_handle_for_viewing_image_file = fopen($file_to_grab_with_location, 'a+');

    $imagick_type->readImageFile($file_handle_for_viewing_image_file);

        // Perform Function: PolaroidImage
        // ---------------------------------------------

            // Polaroid Border Color:
            // ..............................................
   
    $imagick_type->setImageBorderColor( new ImagickPixel( "#CCCCCC" ) );

            // Polaroid Shadow Color:
            // ..............................................

    $imagick_type->setImageBackgroundColor( new ImagickPixel( "#000000" ) );

            // Call Function:
            // ..............................................

    $imagick_type->polaroidImage($draw_type, -10);
   
        // Save File
        // ---------------------------------------------
       
    $file_to_save_with_location = "image_workshop_directory/test_result.png"

    $file_handle_for_saving_image_file = fopen($file_to_save_with_location, 'a+');
    $imagick_type->writeImageFile($file_handle_for_saving_image_file);

?>

The following is an example using a GoogleImage search result for the word "Ocean":




Official Function Page: http://www.php.net/manual/en/imagick.polaroidimage.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

No comments:

Post a Comment