rita
rita

Reputation: 545

Change imagemagick command line into php imagick

For performance reasons (exec in via php is just incredibly slow at times) I am converting most of my imagemagick commands to php's imagick.

I read the documentation about -channel and -level here: http://www.imagemagick.org/script/command-line-options.php#channel and http://www.imagemagick.org/script/command-line-options.php#level

especially in regards to the black, white and gamma values:

Given one, two or three values delimited with commas: black-point, white-point, gamma (for example: 10,250,1.0 or 2%,98%,0.5). The black and white points range from 0 to QuantumRange, or from 0 to 100%; if the white point is omitted it is set to (QuantumRange - black_point), so as to center contrast changes. If a % sign is present anywhere in the string, both black and white points are percentages of the full color range. Gamma will do a -gamma adjustment of the values. If it is omitted, the default of 1.0 (no gamma correction) is assumed.

In normal usage (-level) the image values are stretched so that the given 'black_point' value in the original image is set to zero (or black), while the given 'white_point' value is set to QuantumRange (or white). This provides you with direct contrast adjustments to the image. The 'gamma' of the resulting image will then be adjusted.

Based on this I tried to change

convert a.jpg -channel R -level 33% -channel G -level 33% b.jpg

to

$quantumRange = $image->getQuantumRange();
//get the 33%
$blackPoint = $quantumRange ['quantumRangeLong']/100*33;
//to get the middle ground between black and white - also tried with $quantumRange['quantumRangeLong']
$whitePoint = $quantumRange ['quantumRangeLong'] - $blackPoint;
$gamma = 1;

$image->levelImage ( $blackPoint , $gamma , $whitePoint , Imagick::CHANNEL_RED);
$image->levelImage ( $blackPoint , $gamma , $whitePoint , Imagick::CHANNEL_GREEN);

but the result does look quite different...any experts out there that can point out how imagick performs its channel levels?

Upvotes: 1

Views: 1623

Answers (1)

dank.game
dank.game

Reputation: 4719

I tried it on my computer and the PHP and command line versions are very similar:

original
original

command line
command line

enter image description here
PHP

I don't see anything wrong with your script, but you can try mine to see if it works differently.

$image = new Imagick();
$quantum_range = $image->getQuantumRange();
$max_quantum = $quantum_range["quantumRangeLong"];
$image->readImage("rose.jpg");
$black_point = $max_quantum * .33;
$white_point = $max_quantum - $black_point;
$gamma = 1.0;
$image->levelImage($black_point, $gamma, $white_point, Imagick::CHANNEL_RED);
$image->levelImage($black_point, $gamma, $white_point, Imagick::CHANNEL_GREEN);
header("Content-type: image/jpeg");
echo $image;
$image->writeImage("php.jpg");

In case it's still not working, here are my versions of ImageMagick and PHP's ImageMagick module:

$ convert -version
Version: ImageMagick 6.7.9-10 2012-10-10 Q16 http://www.imagemagick.org
Array ( [versionNumber] => 1632
        [versionString] => ImageMagick 6.6.0-4 2012-05-03 Q16
                           http://www.imagemagick.org )

Upvotes: 3

Related Questions