Vijay Boyapati
Vijay Boyapati

Reputation: 7728

Tilt shifting an Image using ImageMagick in PHP

I have a command that I can run on the command line to tilt-shift an image using Imagemagick:

convert \( myimage.jpg -gamma 0.75 -modulate 100,130 -contrast \) \( +clone -sparse-color Barycentric "0,0 black 0,%h white" -function polynomial 4,-4,1 -level 0,50% \) -compose blur -set option:compose:args 5 -composite myimage.jpg

I'd like to be able to reproduce the effect of this command using PHP's imagemagick library. The first parts of the command are easy to reproduce, but I'm having trouble figuring out sparse-color and the arguments after it. So far I have:

$image = new imagick("myimage.jpg")
$image->gammaImage(0.75);
$image->modulateImage(100,130,100);
$image->contrastImage(1);

Thanks!

Upvotes: 2

Views: 1713

Answers (3)

tbjers
tbjers

Reputation: 572

I am assuming that you are referring to the examples found here:

http://www.imagemagick.org/Usage/photos/

With that said, I have been trying to accomplish the very same thing with PHP Imagick. I am using PHP Imagick 3.0.1 and ImageMagick 6.7.4-4 2012-01-09 Q16 in Linux.

It appears that the blur composite mode is undocumented. I found it by looking through the ImageMagick source under magick/composite.h on line 88.

This code accomplishes what you are looking for:

$im = new Imagick();
$im->readImageBlob(file_get_contents($src));
$qr = $im->getQuantumRange();
$qr = $qr['quantumRangeLong'];
$im->sigmoidalContrastImage(true, 10, $qr / 2, Imagick::CHANNEL_ALL);
$blurmap = new Imagick();
$blurmap->setOption('compose:args', '5');
$blurmap->newPseudoImage($im->getImageWidth(), $im->getImageHeight(), 'gradient:black-white');
$blurmap->functionImage(Imagick::FUNCTION_POLYNOMIAL, array(4.5, -4.5, 1));
$blurmap->levelImage(0, 1, $qr / 2);
$im->compositeImage($blurmap, 57, 0, 0);
$blurmap->destroy();

If you require more blur, simply change compose:args to 10 or something. One thing I figured out was that I had to set the option before I created/loaded anything into the wand itself.

I could not get the SparseColorImage() function to behave as it does on the command line, although I am sure that someone else could contribute that part if they figure it out. The above is enough for my needs.

EDIT: Upon further inspection of the images generated I noticed that even the black parts of the blur map were being blurred. After some searching it appears that this stems from a bug introduced in ImageMagick. I switched to version 6.7.8-1 2012-07-05 Q16 and the blur map now appears to be working. If you notice unwanted blurring with the blur composite I suggest you upgrade your ImageMagick.

Upvotes: 0

Bonzo
Bonzo

Reputation: 5299

Best of luck to you Vijay - I recomend you stick with exec()

Anyway just had a try and my code is below; you can see some changes I made and gave up with an error on the line: $new->functionImage ( FUNCTION_POLYNOMIAL, $functionImagearray );

// Did not like the %h
// $sparseColorarray = array( 0, 0, black, 0, %h, white );
$sparseColorarray = array( 0, 0, black, 0, 20, white );
$functionImagearray = array( 4, -4, 1 );
$image = new imagick("output3.jpg");
$image->gammaImage(0.75);
$image->modulateImage(100,130,100);
$image->contrastImage(1);
//$new = $image->clone(); 
$new = clone $image; 
$new->sparseColorImage( Imagick::SPARSECOLORMETHOD_BARYCENTRIC, $sparseColorarray );
$new->functionImage ( Imagick::FUNCTION_POLYNOMIAL, $functionImagearray );
// Did not like the %
// $new->levelImage( 0, 50% );
$new->levelImage( 0, 50 );
// Can not find any options for this blur
$image->setImageCompose ( BLUR );
$image->setOption( args, 5);
$image->compositeImage( $new, COMPOSITE_BLEND, 0, 0 );
$image->writeImage( "tilt.jpg" );
$image->destroy();

Reading the documentation and following your example code this is what I came up with. I could also not find anyware the options for $image->setImageCompose ( BLUR );

It would be interesting to see if you ever get it working.

Just found out my Imagick version is to old for functionImage

Changed $new->functionImage line

Upvotes: 2

MasonWinsauer
MasonWinsauer

Reputation: 673

I don't have vast experience with ImageMagick, but I believe that there is a function equivalent to sparse-color Barycentric that can be found in documentation here.

It should look something like:

$image -> sparseColorImage(int SPARSECOLORMETHOD_BARYCENTRIC (integer), 
                           array $arguments [, int $channel = CHANNEL_DEFAULT ])

FUNCTION_POLYNOMIAL and other Method Constants/Channel Constants can be found here for future reference!

Gaussian Blurring is handled by adding:

$image -> gaussianBlurImage ( float $radius , 
                              float $sigma [, int $channel = CHANNEL_ALL ] )

Of course, these are all very generic, but you can mess around to get the look and feel you want.

Hope this helps!

Mason

Upvotes: 1

Related Questions