As nick said, the function Imagick::flattenImages() is deprecated. Replacing it by Imagick::mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN) partially worked. Images with transparency (ex: .png) get black background even if the image background is set to white.
Old working code:
$im->setImageBackgroundColor('white');
$im = $im->flattenImages();
New working code:
$im->setImageBackgroundColor('white');
$im->setImageAlphaChannel(imagick::ALPHACHANNEL_REMOVE);
$im->mergeImageLayers(imagick::LAYERMETHOD_FLATTEN);
imagick::ALPHACHANNEL_REMOVE is not shown on the constants page but do works with Imagick 3.2.0RC1.