Generates thumb for non-image elements.

@param MediaInterface $media
@return null|string
966960
private function generateFileThumb(MediaInterface $media)
    {
        $extension   = $media->guessExtension();
        $thumbPath   = sprintf('/%s/%s.jpg', $this->thumbsDirectory, $extension);
        $destination = $this->webRootDirectory . $thumbPath;

        if (file_exists($destination)) {
            return $thumbPath;
        }

        $backgroundColor = MediaTypes::getColor($media->getType());

        $iconPath = sprintf('%s/%s.png', $this->iconsSourcePath, $extension);
        if (! file_exists($iconPath)) {
            $iconPath = $this->iconsSourcePath.'/default.png';
        }

        $this->checkDir(dirname($destination));
        try {
            $palette = new RGB();
            $thumb = $this->imagine->create(new Box(120, 90), $palette->color($backgroundColor));

            $icon = $this->imagine->open($iconPath);
            $iconSize = $icon->getSize();
            $start = new Point(120/2 - $iconSize->getWidth()/2, 90/2 - $iconSize->getHeight()/2);

            $thumb->paste($icon, $start);
            $thumb->save($destination);

            return $thumbPath;
        } catch (ImagineException $e) {
            // Image thumb generation failed
        }

        return null;
    }
1182287
public function renderMedia(MediaInterface $media, array $options)
    {
        $options = $this->validateOptions($options, __METHOD__);

        return $this->render(
            'OpenOrchestraMediaBundle:RenderMedia:image.html.twig',
            array(
                'media_url' => $this->getMediaFormatUrl($media, $options['format']),
                'media_alt' => $options['alt'],
                'id'        => $options['id'],
                'class'     => $options['class'],
                'style'     => $options['style'],
                'legend'    => $options['legend']
            )
        );
    }
685037
public function getOriginalFile(Media $media)
    {
        return $this->fileSystem->get($this->getFilePath($media), true);
    }
488834
public function generateThumbnail(MediaInterface $media)
    {
        $fileName = $media->getFilesystemName();
        $thumbnailName = '';

        $extractedImagePath = $this->imageManager->extractImageFromPdf(
            $this->tmpDir . DIRECTORY_SEPARATOR . $fileName
        );

        $thumbnailPath = $this->imageManager->generateAlternative($extractedImagePath, $this->thumbnailFormat);

        if ($thumbnailPath != '') {
            $thumbnailName = self::THUMBNAIL_PREFIX . '-' . str_replace('.pdf', '.jpg', $fileName);
            $this->mediaStorageManager->uploadFile($thumbnailName, $thumbnailPath);
        }

        if (trim($extractedImagePath, DIRECTORY_SEPARATOR) != trim($this->tmpDir, DIRECTORY_SEPARATOR)) {
            $this->fileSystem->remove(array($extractedImagePath));
        }

        $media->setThumbnail($thumbnailName);
    }
1345314
public function addMedia(MediaInterface $media)
    {
        if (!in_array($media->getKey(), $this->keys)) {
            throw new \RuntimeException("Key {$media->getKey()} is not selected.");
        }

        foreach ($this->medias as $m) {
            if ($m->getKey() == $media->getKey()) {
                return $this;
            }
        }

        $media->setFolder($this->folder);
        $this->medias[] = $media;

        return $this;
    }
904479
public function getThumbnails()
	{
		$thumbs = array();

		foreach ($this->xml->children('media', true)->group->thumbnail as $item)
		{
			$url = (string) $item->attributes()->url;
			$width = (int) $item->attributes()->width;
			$height = (int) $item->attributes()->height;
			$thumbs[$width] = array('url' => $url, 'w' => $width, 'h' => $height);
		}

		return $thumbs;
	}
584563
public function thumbnail()
    {
        if (is_null($thumbnailURL = $this->entity->getImagePath()))
        {
            return $this->iconThumbnail('document');
        }

        return $this->wrapThumbnail(
            $this->filteredImageWith('rthumb', 'document-thumbnail__image')
        );
    }
45860
public function setData(Media $media)
    {
        $this->id = $media->getId();
        $this->title = $media->getTitle();
        $this->setFormats($media->getFormats());
        $this->url = $media->getUrl();
        $this->copyright = $media->getCopyright();

        return $this;
    }
1236946
public function getDownloadStrategy(MediaInterface $media)
    {
        $context = $this->getContext($media->getContext());

        $id = $context['download']['strategy'];

        // NEXT_MAJOR: remove this line with the next major release.
        if (isset($this->downloadSecurities[$id])) {
            return $this->downloadSecurities[$id];
        }

        if (!isset($this->downloadStrategies[$id])) {
            throw new \RuntimeException('Unable to retrieve the download security : '.$id);
        }

        return $this->downloadStrategies[$id];
    }
1109059
public function preSave(MediaInterface $media)
    {
        $vimeoData = $this->getMetadata($media);

        $media->setReference($vimeoData->video_id);

        if(!$media->getName()) {
            $media->setName($vimeoData->title);
        }

        $thumb = $this->saveThumbnail($media, $vimeoData->thumbnail_url);
        $media->setThumb($thumb);

        $media->setMetadata($vimeoData);

        $media->setContentType('video/x-flv');
    }
197939
public function createReference(MediaInterface $media)
    {
        $allowed = false;
        foreach ($this->contexts as $context) {
            if (in_array($media->getExtension(), $context)) {
                $allowed = true;
                break;
            }
        }

        if ($allowed === false) {
            $this->error = array('error.extensionNotAllowed', array('%extension%' => $media->getExtension()));

            return false;
        }

        // secure extension for reference
        if (!in_array($media->getExtension(), $this->trustedExtensions)) {
            $media->setExtension($media->getExtension().'.txt');
            $media->setType($media::OTHER);
        }

        do {
            $media->setReference(sha1(microtime(true).mt_rand()).'.'.$media->getExtension());
        } while ($this->filesystemManipulator->has($this->originalDir.'/'.$media->getReference()));

        return true;
    }
707154
public function createDefaultThumbs($image = null, $app = 'default')
    {
        if(!$image) {
            $image = $this;
        }

        if($image->hasImageExtension()) {
            foreach (config('media.default_thumb_size') as $thumKey => $thumValue) {
                if ($thumKey == "default" || $thumKey == $app) { // create only the thumbs that are needed for an app
                    foreach ($thumValue as $thumbDimension) {
                        $this->createThumb($image, $thumbDimension[0], $thumbDimension[1]);
                    }
                }
            }
        }

        return $this;
    }
171450
public function generateFrontUrl(MediaInterface $media)
    {
        $path = null;

        if ($media->getType() === MediaTypes::IMAGE) {
            $path = $this->cacheManager->getBrowserPath($media->getPath(), 'media_front');
        } elseif (in_array($media->getType(), [MediaTypes::VIDEO, MediaTypes::AUDIO, MediaTypes::FLASH])) {
            $path = $this->urlGenerator->generate('ekyna_media_player', ['key' => $media->getPath()], true);
        } else {
            $path = $this->urlGenerator->generate('ekyna_media_download', ['key' => $media->getPath()], true);
        }

        return $path;
    }
600938
private function getFileVersionForMedia(MediaInterface $media)
    {
        /** @var File $file */
        $file = $media->getFiles()->get(0);
        if (!isset($file)) {
            throw new FileVersionNotFoundException($media->getId(), 'latest');
        }

        $fileVersion = $file->getLatestFileVersion();
        if (!isset($fileVersion)) {
            throw new FileVersionNotFoundException($media->getId(), 'latest');
        }

        return $fileVersion;
    }
659724
public function getAliasAndKey(MediaInterface $media, $alias = null)
    {
        $aliasManipulator = $this->aliasManipulatorFactory->setAlias($alias);

        if ($media->getType() === $media::IMAGE) {
            $alias = $aliasManipulator->getAliasName();

            if ($alias !== $this->originalDir) {
                $this->cropImage($media, $aliasManipulator);
            }
        } else {
            $alias = $this->originalDir;
        }

        return $alias.'/'.$media->getReference();
    }
38121
public function fetchPermanentThumbnail ($mediaID, $into = null)
    {
        return new Downloaded\Thumbnail($mediaID, $this->doPermanentFetchToStream($mediaID, $into));
    }
679780
public function firstImageFile(): ?MediaFile
    {
        foreach ($this->mediaFiles() as $media_file) {
            if ($media_file->isImage()) {
                return $media_file;
            }
        }

        return null;
    }