diff --git a/Imagine/Filter/FilterManager.php b/Imagine/Filter/FilterManager.php index 51e873837..be7c942f0 100644 --- a/Imagine/Filter/FilterManager.php +++ b/Imagine/Filter/FilterManager.php @@ -109,7 +109,14 @@ public function apply(BinaryInterface $binary, array $config) )); } + $prevImage = $image; $image = $this->loaders[$eachFilter]->load($image, $eachOptions); + + // If the filter returns a different image object destruct the old one because imagick keeps consuming memory if we don't + // See https://github.com/liip/LiipImagineBundle/pull/682 + if ($prevImage !== $image && method_exists($prevImage, '__destruct')) { + $prevImage->__destruct(); + } } $options = array( @@ -134,6 +141,12 @@ public function apply(BinaryInterface $binary, array $config) $filteredContent = $image->get($filteredFormat, $options); $filteredMimeType = $filteredFormat === $binary->getFormat() ? $binary->getMimeType() : $this->mimeTypeGuesser->guess($filteredContent); + // We are done with the image object so we can destruct the this because imagick keeps consuming memory if we don't + // See https://github.com/liip/LiipImagineBundle/pull/682 + if (method_exists($image, '__destruct')) { + $image->__destruct(); + } + return $this->applyPostProcessors(new Binary($filteredContent, $filteredMimeType, $filteredFormat), $config); }