0

I need to convert some command line imagick code into the php classes. I can't run command line scripts on our production box so I need to be able to use the classes, unfortunately there's no documentation on converting between the two.

Does anyone know how I could do this using the imagick class?

$color_mask ="convert -size $dimensions xc:#$color -fill white";

/* create the silver mask */
$silver_mask ="convert -size $dimensions xc:#e6e7e8 -fill white";

/* resize the image to the $larger_dim before cropping it to the dimensions to get that "zoomed in" effect */
$thumb = "convert images/$im -thumbnail $larger_dim^ ". 
" -gravity center -extent $dimensions PNG:-";

/* screen the resized thumbnail and the color mask */
$masked = "convert $thumb $color_mask -compose Screen -gravity center -composite PNG:-";

/* multiply the masked thumb with the silver mask */
$final = "convert $masked $silver_mask -compose Multiply -gravity center -composite PNG:-"; 

/* Output the image*/
header("Content-Type: image/png");
passthru($final, $retval);

I'd also be happy to do the same in GD instead, I've just had issues getting the quality right in GD.

TIA

emcconville
  • 23,800
  • 4
  • 50
  • 66
kurtfoster
  • 353
  • 3
  • 14

1 Answers1

2

Ok, this took quite a bit of figuring out as the docs on php.net aren't great and there isn't anywhere that explains the equivalent functions between the ImageMagick command line and the functions in the Imagick php class.

Here's is my function which performs the above:

public static function getColorImg($img, $color, $filename) {

if (class_exists('Imagick')) {

  // Create the blue overlay
  $blue = new Imagick();

  // Create a plain colour image in the selected color
  $blue->newImage($dimensions['width'], $dimensions['height'], new ImagickPixel($color));
  $blue->setImageFormat('png');

  // Create the plain grey image
  $grey  = new Imagick();
  $grey->newImage($dimensions['width'], $dimensions['height'], new ImagickPixel($silver));
  $grey->setImageFormat('png');

  // Now grab the actual image and change it's size to the larger image
  $image = new Imagick($img);
  $image->setImageFormat('png');
  $image->thumbnailImage($larger_dim['width'], $larger_dim['height'], TRUE);
  // now zoom it in
  $image->cropThumbnailImage($dimensions['width'], $dimensions['height']);
  // Screen takes multiple commands in the php class. Negate the two images
  $image->negateImage(false);
  $blue->negateImage(false);
  // Then multipy them.
  $image->compositeImage($blue, imagick::COMPOSITE_MULTIPLY, 0, 0);
  // Re nagate the image so that it looks back to normal.
  $image->negateImage(false);
  // Now multiply it with the silver image
  $image->compositeImage($grey, imagick::COMPOSITE_MULTIPLY, 0, 0);
  // Write out the file as original file name with the prefix
  if ($image->writeImage($filename)) {
    $return = $filename;
  }
  else {
    $return = FALSE;
  }
}
else {
  $return = FALSE;
}

return $return;
}
kurtfoster
  • 353
  • 3
  • 14