Esempio n. 1
0
 /**
  * @param \Carrooi\ImagesManager\ImagesManager $manager
  * @param string $namespace
  * @param string $name
  * @param int|string|null $size
  * @param string|null $resizeFlag
  * @return string
  */
 public static function getUrl(ImagesManager $manager, $namespace, $name, $size = null, $resizeFlag = null)
 {
     $width = $height = null;
     if ($size !== null) {
         if (!is_int($size) && !preg_match('/^(\\d+)x(\\d+)$/', $size)) {
             // probably preset
             $config = $manager->getConfiguration();
             if (!$config->hasPreset($namespace, $size)) {
                 throw new PresetNotExistsException('Preset ' . $size . ' does not exists.');
             }
             $preset = $config->getPreset($namespace, $size);
             $width = $preset['width'];
             $height = $preset['height'];
             $resizeFlag = $preset['resizeFlag'];
         } else {
             $size = UtilsHelpers::parseSize($size);
             $width = $size[0];
             $height = $size[1];
         }
     }
     if ($resizeFlag !== null && is_string($resizeFlag)) {
         $resizeFlag = UtilsHelpers::parseResizeFlags($resizeFlag);
     }
     if (!($image = $manager->findImage($namespace, $name))) {
         $dummyImageUrl = $manager->getDummyImageUrl($namespace, $width, $height);
         return $dummyImageUrl ? $dummyImageUrl : '';
     }
     if ($width || $height) {
         $manager->tryStoreThumbnail($image, $width, $height, $resizeFlag);
     }
     return $manager->getUrl($image, $width, $height, $resizeFlag);
 }
 /**
  * @param \Nette\DI\ServiceDefinition $configuration
  * @param string $namespace
  * @param array $config
  */
 private function applyNamespaceConfiguration(ServiceDefinition $configuration, $namespace, array $config)
 {
     $builder = $this->getContainerBuilder();
     $safeNamespace = str_replace('-', '_', Strings::webalize($namespace));
     if ($namespace === Configuration::DEFAULT_NAMESPACE_NAME || $config['nameResolver']['class']) {
         if (!class_exists($config['nameResolver']['class'])) {
             throw new ConfigurationException('Name resolver ' . $config['nameResolver']['class'] . ' for namespace ' . $namespace . ' does not exists.');
         }
         $nameResolver = $builder->addDefinition($this->prefix('nameResolver.' . $safeNamespace))->setClass($config['nameResolver']['class']);
         $configuration->addSetup('setNameResolver', [$namespace, $nameResolver]);
     }
     if ($config['default'] !== null) {
         $configuration->addSetup('setDefaultImage', [$namespace, $config['default']]);
     }
     if ($config['resizeFlag'] !== null) {
         $resizeFlag = Helpers::parseResizeFlags($config['resizeFlag']);
         $configuration->addSetup('setResizeFlag', [$namespace, $resizeFlag]);
     }
     if ($config['quality'] !== null) {
         $configuration->addSetup('setQuality', [$namespace, $config['quality']]);
     }
     if (isset($config['presets']) && count($config['presets'])) {
         foreach ($config['presets'] as $name => $preset) {
             if ($preset instanceof Statement) {
                 $size = Helpers::parseSize($preset->getEntity());
                 $resizeFlags = Helpers::parseResizeFlags($preset->arguments);
             } else {
                 $size = Helpers::parseSize($preset);
                 $resizeFlags = null;
             }
             $configuration->addSetup('addPreset', [$namespace, $name, $size[0], $size[1], $resizeFlags]);
         }
     }
     if ($config['dummy']['enabled'] !== null) {
         $configuration->addSetup('enableDummy', [$namespace, $config['dummy']['enabled']]);
     }
     if ($config['dummy']['class'] !== null) {
         if (!class_exists($config['dummy']['class'])) {
             throw new ConfigurationException('Dummy provider class ' . $config['dummy']['class'] . ' for namespace ' . $namespace . ' does not exists.');
         }
         $provider = $builder->addDefinition($this->prefix('dummy.' . $safeNamespace))->setClass($config['dummy']['class']);
         $configuration->addSetup('setDummyProvider', [$namespace, $provider]);
     }
     if ($config['dummy']['category'] !== null) {
         $configuration->addSetup('setDummyCategory', [$namespace, $config['dummy']['category']]);
     }
     if ($config['dummy']['fallbackSize'] !== null) {
         $size = $config['dummy']['fallbackSize'];
         $configuration->addSetup('setDummyFallbackSize', [$namespace, $size[0], $size[1]]);
     }
     if ($config['dummy']['chance'] !== null) {
         $configuration->addSetup('setDummyDisplayChance', [$namespace, $config['dummy']['chance']]);
     }
 }