PartKeepr\ImageBundle\Controller\ImageController::fitWithin PHP Method

fitWithin() public method

Scales the image to fit within the given size.
public fitWithin ( UploadedFile $image, integer $width, integer $height, boolean $padding = false ) : string
$image PartKeepr\UploadedFileBundle\Entity\UploadedFile The image to scale
$width integer The width
$height integer The height
$padding boolean If true, pad the output image to the given size (transparent background).
return string The path to the scaled file
    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;
    }