Newscoop\Image\ImageService::upload PHP Метод

upload() публичный Метод

Upload image and create entity
public upload ( Symfony\Component\HttpFoundation\File\UploadedFile $file, array $attributes, Newscoop\Image\ImageInterface $image = null, $keepRatio = true ) : LocalImage
$file Symfony\Component\HttpFoundation\File\UploadedFile
$attributes array
$image Newscoop\Image\ImageInterface
Результат LocalImage
    public function upload(UploadedFile $file, array $attributes, ImageInterface $image = null, $keepRatio = true)
    {
        $filesystem = new Filesystem();
        $imagine = self::getImagine();
        $mimeType = $file->getClientMimeType();
        if (!in_array($mimeType, $this->supportedTypes)) {
            throw new \InvalidArgumentException('Unsupported image type ' . $mimeType . '.');
        }
        if (!file_exists($this->config['image_path']) || !is_writable($this->config['image_path'])) {
            throw new FileException('Directory ' . $this->config['image_path'] . ' is not writable');
        }
        if (!file_exists($this->config['thumbnail_path']) || !is_writable($this->config['thumbnail_path'])) {
            throw new FileException('Directory ' . $this->config['thumbnail_path'] . ' is not writable');
        }
        $attributes = array_merge(array('content_type' => $mimeType), $attributes);
        if (!is_null($image)) {
            if (file_exists($image->getPath())) {
                $filesystem->remove($image->getPath());
            }
            if (file_exists($image->getThumbnailPath())) {
                unlink($this->config['thumbnail_path'] . $image->getThumbnailPath(true));
            }
        } else {
            $image = new LocalImage($file->getClientOriginalName());
            $image->setCreated(new \DateTime());
            $this->orm->persist($image);
        }
        list($width, $height) = getimagesize($file->getRealPath());
        $image->setWidth($width);
        $image->setHeight($height);
        $this->fillImage($image, $attributes);
        $this->orm->flush();
        $imagePath = $this->generateImagePath($image->getId(), $file->getClientOriginalExtension());
        $thumbnailPath = $this->generateThumbnailPath($image->getId(), $file->getClientOriginalExtension());
        $image->setBasename($this->generateImagePath($image->getId(), $file->getClientOriginalExtension(), true));
        $image->setThumbnailPath($this->generateThumbnailPath($image->getId(), $file->getClientOriginalExtension(), true));
        $this->orm->flush();
        try {
            $file->move($this->config['image_path'], $this->generateImagePath($image->getId(), $file->getClientOriginalExtension(), true));
            $filesystem->chmod($imagePath, 0644);
            if ($keepRatio) {
                $ratioOrig = $width / $height;
                $ratioNew = $this->config['thumbnail_max_size'] / $this->config['thumbnail_max_size'];
                if ($ratioNew > $ratioOrig) {
                    $newImageWidth = $this->config['thumbnail_max_size'] * $ratioOrig;
                    $newImageHeight = $this->config['thumbnail_max_size'];
                } else {
                    $newImageWidth = $this->config['thumbnail_max_size'];
                    $newImageHeight = $this->config['thumbnail_max_size'] / $ratioOrig;
                }
            } else {
                $newImageWidth = $this->config['thumbnail_max_size'];
                $newImageHeight = $this->config['thumbnail_max_size'];
            }
            $imagine->open($imagePath)->resize(new Box($newImageWidth, $newImageHeight))->save($thumbnailPath, array('quality' => 90));
            $filesystem->chmod($thumbnailPath, 0644);
        } catch (\Exceptiom $e) {
            $filesystem->remove($imagePath);
            $filesystem->remove($thumbnailPath);
            $this->orm->remove($image);
            $this->orm->flush();
            throw new \Exception($e->getMessage(), $e->getCode());
        }
        $this->cacheService->clearNamespace('image');
        return $image;
    }