I am trying to remove transparent areas of an image with php using imagick.
Image Magick provides the trim method: Imagick::trimImage
trim
Have you tried specifying your background color as a hex code, rather than instantiating an ImagickPixel for it?
$im->setImageBackgroundColor( #D5D5D5 );