public function fitWithin(UploadedFile $image, $width, $height, $padding = false)
{
$this->ensureCacheDirExists();
if ($padding) {
$mode = 'fwp';
} else {
$mode = 'fw';
}
$outputFile = $this->getImageCacheFilename($image, $width, $height, $mode);
if ($this->hasCacheFile($image, $width, $height, $mode) && file_exists($outputFile)) {
return $outputFile;
}
$imagine = new Imagine();
$localCacheFile = $this->getImageCacheDirectory() . $image->getFullFilename();
$storage = $this->get('partkeepr_uploadedfile_service')->getStorage($image);
file_put_contents($localCacheFile, $storage->read($image->getFullFilename()));
$imagine->open($localCacheFile)->thumbnail(new Box($width, $height))->save($outputFile);
$cachedImage = new CachedImage($image, $outputFile);
$this->getDoctrine()->getManager()->persist($cachedImage);
return $outputFile;
}