/**
  * Forces image caching and returns path to cached image.
  *
  * @param string  $basePath Deprecated parameter
  * @param string  $path
  * @param string  $filter
  * @param boolean $force
  * @param string  $saveAs
  *
  * @return string|null
  *
  * @throws RuntimeException
  */
 public function cacheImage($basePath, $path, $filter, $force = false, $saveAs = null)
 {
     $path = '/' . ltrim($path, '/');
     $saveAs = $saveAs ? '/' . ltrim($saveAs, '/') : $path;
     // if cache path cannot be determined, return 404
     if (!($cachedPath = $this->cachePathResolver->getCachedPath($saveAs, $filter))) {
         return null;
     }
     // if the file has already been cached, just return path
     if (!$force && is_file($cachedPath)) {
         return $cachedPath;
     }
     if (!($sourcePath = $this->cachePathResolver->getRealPath($path, $filter))) {
         return null;
     }
     $this->ensureDirectoryExists($cachedPath);
     try {
         $image = $this->imagine->open($sourcePath);
     } catch (RuntimeException $e) {
         try {
             // Make sure source path is an image
             new ImageFile($sourcePath, false);
             // Do not pollute the space (don't copy anything; symlink is just fine)
             $this->filesystem->symlink($sourcePath, $cachedPath);
         } catch (RuntimeException $e) {
             return null;
         } catch (IOException $e) {
             // In case we were not able to create symlink we should return source path.
             // This means we'll be back here, but at least we'll not be polluting space with useless copies.
             return $sourcePath;
         }
         return $cachedPath;
     }
     $options = ['quality' => $this->filterManager->getOption($filter, 'quality', $this->defaultQuality), 'format' => $this->filterManager->getOption($filter, 'format', null)];
     // Important! optipng filter returns an instance of ImageAssetWrapper.
     /** @var ImageInterface|ImageAssetWrapper $image */
     $image = $this->filterManager->getFilter($filter)->apply($image);
     /** @var resource $context */
     if ($context = $this->findStreamContext($cachedPath, $filter)) {
         $tmpPath = tempnam(sys_get_temp_dir(), 'avalanche-cache-manager-proxy-');
         $image->save($tmpPath, $options);
         copy($tmpPath, $cachedPath, $context);
         unlink($tmpPath);
     } else {
         $image->save($cachedPath, $options);
     }
     $this->ensureFilePermissions($cachedPath);
     return $cachedPath;
 }