Skip to content

Commit de17b0a

Browse files
committed
Merge pull request #682 from JCID/patch-1
Destruct image to cleanup memory
2 parents c2dfddf + cfe164d commit de17b0a

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Imagine/Filter/FilterManager.php

+13
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,14 @@ public function apply(BinaryInterface $binary, array $config)
109109
));
110110
}
111111

112+
$prevImage = $image;
112113
$image = $this->loaders[$eachFilter]->load($image, $eachOptions);
114+
115+
// If the filter returns a different image object destruct the old one because imagick keeps consuming memory if we don't
116+
// See https://github.com/liip/LiipImagineBundle/pull/682
117+
if ($prevImage !== $image && method_exists($prevImage, '__destruct')) {
118+
$prevImage->__destruct();
119+
}
113120
}
114121

115122
$options = array(
@@ -134,6 +141,12 @@ public function apply(BinaryInterface $binary, array $config)
134141
$filteredContent = $image->get($filteredFormat, $options);
135142
$filteredMimeType = $filteredFormat === $binary->getFormat() ? $binary->getMimeType() : $this->mimeTypeGuesser->guess($filteredContent);
136143

144+
// We are done with the image object so we can destruct the this because imagick keeps consuming memory if we don't
145+
// See https://github.com/liip/LiipImagineBundle/pull/682
146+
if (method_exists($image, '__destruct')) {
147+
$image->__destruct();
148+
}
149+
137150
return $this->applyPostProcessors(new Binary($filteredContent, $filteredMimeType, $filteredFormat), $config);
138151
}
139152

0 commit comments

Comments
 (0)