setImportantPart() public method

Set the important part settings
public setImportantPart ( array $importantPart = null )
$importantPart array The settings array
Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function resize(ImageInterface $image, ResizeConfigurationInterface $config, ResizeOptionsInterface $options)
 {
     if (!empty($GLOBALS['TL_HOOKS']['executeResize']) && is_array($GLOBALS['TL_HOOKS']['executeResize']) || !empty($GLOBALS['TL_HOOKS']['getImage']) && is_array($GLOBALS['TL_HOOKS']['getImage'])) {
         @trigger_error('Using the executeResize and getImage hooks has been deprecated and will no longer work in Contao 5.0. Replace the contao.image.resizer service instead.', E_USER_DEPRECATED);
         $this->legacyImage = null;
         $legacyPath = $image->getPath();
         if (0 === strpos($legacyPath, TL_ROOT . '/') || 0 === strpos($legacyPath, TL_ROOT . '\\')) {
             $legacyPath = substr($legacyPath, strlen(TL_ROOT) + 1);
             $this->legacyImage = new LegacyImage(new File($legacyPath));
             $this->legacyImage->setTargetWidth($config->getWidth());
             $this->legacyImage->setTargetHeight($config->getHeight());
             $this->legacyImage->setResizeMode($config->getMode());
             $this->legacyImage->setZoomLevel($config->getZoomLevel());
             if ($options->getTargetPath() && (0 === strpos($options->getTargetPath(), TL_ROOT . '/') || 0 === strpos($options->getTargetPath(), TL_ROOT . '\\'))) {
                 $this->legacyImage->setTargetPath(substr($options->getTargetPath(), strlen(TL_ROOT) + 1));
             }
             $importantPart = $image->getImportantPart();
             $this->legacyImage->setImportantPart(['x' => $importantPart->getPosition()->getX(), 'y' => $importantPart->getPosition()->getY(), 'width' => $importantPart->getSize()->getWidth(), 'height' => $importantPart->getSize()->getHeight()]);
         }
     }
     if (isset($GLOBALS['TL_HOOKS']['executeResize']) && is_array($GLOBALS['TL_HOOKS']['executeResize']) && $this->legacyImage) {
         foreach ($GLOBALS['TL_HOOKS']['executeResize'] as $callback) {
             $return = System::importStatic($callback[0])->{$callback[1]}($this->legacyImage);
             if (is_string($return)) {
                 return $this->createImage($image, TL_ROOT . '/' . $return);
             }
         }
     }
     return parent::resize($image, $config, $options);
 }
Exemplo n.º 2
0
 /**
  * Get the config for one picture source element
  *
  * @param Model|object $imageSize The image size or image size item model
  *
  * @return PictureConfigurationItem
  */
 protected function getConfigurationItem($imageSize)
 {
     $configItem = new PictureConfigurationItem();
     $resizeConfig = new ResizeConfiguration();
     $mode = $imageSize->resizeMode;
     if (substr_count($mode, '_') === 1) {
         $importantPart = $this->image->setImportantPart(null)->getImportantPart();
         $mode = explode('_', $mode);
         if ($mode[0] === 'left') {
             $importantPart['width'] = 1;
         } elseif ($mode[0] === 'right') {
             $importantPart['x'] = $importantPart['width'] - 1;
             $importantPart['width'] = 1;
         }
         if ($mode[1] === 'top') {
             $importantPart['height'] = 1;
         } elseif ($mode[1] === 'bottom') {
             $importantPart['y'] = $importantPart['height'] - 1;
             $importantPart['height'] = 1;
         }
         $this->image->setImportantPart($importantPart);
         $mode = ResizeConfiguration::MODE_CROP;
     }
     $resizeConfig->setWidth($imageSize->width)->setHeight($imageSize->height)->setZoomLevel($imageSize->zoom);
     if ($mode) {
         $resizeConfig->setMode($mode);
     }
     $configItem->setResizeConfig($resizeConfig);
     if (isset($imageSize->sizes)) {
         $configItem->setSizes($imageSize->sizes);
     }
     if (isset($imageSize->densities)) {
         $configItem->setDensities($imageSize->densities);
     }
     if (isset($imageSize->media)) {
         $configItem->setMedia($imageSize->media);
     }
     return $configItem;
 }
Exemplo n.º 3
0
 /**
  * Tests the getCacheName() method.
  *
  * @param array  $arguments
  * @param string $expectedCacheName
  *
  * @dataProvider getCacheName
  */
 public function testGetCacheName($arguments, $expectedCacheName)
 {
     /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileMock */
     $fileMock = $this->getMockBuilder('Contao\\File')->setMethods(['__get', 'exists'])->setConstructorArgs(['dummy.jpg'])->getMock();
     $fileMock->expects($this->any())->method('exists')->will($this->returnValue(true));
     $fileMock->expects($this->any())->method('__get')->will($this->returnCallback(function ($key) use($arguments) {
         switch ($key) {
             case 'extension':
                 return 'jpg';
             case 'path':
                 return $arguments[2];
             case 'filename':
                 return $arguments[2];
             case 'mtime':
                 return $arguments[5];
             case 'width':
             case 'viewWidth':
                 return 200;
             case 'height':
             case 'viewHeight':
                 return 200;
             default:
                 return null;
         }
     }));
     $imageObj = new Image($fileMock);
     $imageObj->setTargetWidth($arguments[0]);
     $imageObj->setTargetHeight($arguments[1]);
     $imageObj->setResizeMode($arguments[3]);
     $imageObj->setZoomLevel($arguments[4]);
     $imageObj->setImportantPart($arguments[6]);
     $this->assertSame($imageObj->getCacheName(), $expectedCacheName);
 }