/** * Get the picture element definition array * * @return array The picture element definition */ public function getTemplateData() { $image = \System::getContainer()->get('contao.image.image_factory')->create(TL_ROOT . '/' . $this->image->getOriginalPath()); $config = new PictureConfiguration(); $config->setSize($this->getConfigurationItem($this->imageSize)); $sizeItems = array(); foreach ($this->imageSizeItems as $imageSizeItem) { $sizeItems[] = $this->getConfigurationItem($imageSizeItem); } $config->setSizeItems($sizeItems); $importantPart = $this->image->getImportantPart(); $image->setImportantPart(new ImportantPart(new Point($importantPart['x'], $importantPart['y']), new Box($importantPart['width'], $importantPart['height']))); $container = \System::getContainer(); $picture = $container->get('contao.image.picture_generator')->generate($image, $config, (new ResizeOptions())->setImagineOptions($container->getParameter('contao.image.imagine_options'))->setBypassCache($container->getParameter('contao.image.bypass_cache'))); return array('img' => $picture->getImg(TL_ROOT), 'sources' => $picture->getSources(TL_ROOT)); }
/** * Tests the setters and getters. */ public function testSettersAndGetters() { /** @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) { switch ($key) { case 'extension': return 'jpg'; case 'path': return 'dummy.jpg'; case 'width': case 'viewWidth': return 100; case 'height': case 'viewHeight': return 100; default: return null; } })); $imageObj = new Image($fileMock); $this->assertFalse($imageObj->getForceOverride()); $imageObj->setForceOverride(true); $this->assertTrue($imageObj->getForceOverride()); $this->assertSame($imageObj->getImportantPart(), ['x' => 0, 'y' => 0, 'width' => 100, 'height' => 100]); $imageObj->setImportantPart(['x' => 20, 'y' => 40, 'width' => 80, 'height' => 60]); $this->assertSame($imageObj->getImportantPart(), ['x' => 20, 'y' => 40, 'width' => 80, 'height' => 60]); $imageObj->setImportantPart(['x' => -20, 'y' => 40.1, 'width' => '80', 'height' => 120]); $this->assertSame($imageObj->getImportantPart(), ['x' => 0, 'y' => 40, 'width' => 80, 'height' => 60]); $imageObj->setImportantPart(['x' => 200, 'y' => 200, 'width' => 200, 'height' => 200]); $this->assertSame($imageObj->getImportantPart(), ['x' => 99, 'y' => 99, 'width' => 1, 'height' => 1]); $imageObj->setImportantPart(null); $this->assertSame($imageObj->getImportantPart(), ['x' => 0, 'y' => 0, 'width' => 100, 'height' => 100]); $this->assertSame($imageObj->getTargetHeight(), 0); $imageObj->setTargetHeight(20); $this->assertSame($imageObj->getTargetHeight(), 20); $imageObj->setTargetHeight(50.125); $this->assertSame($imageObj->getTargetHeight(), 50); $this->assertSame($imageObj->getTargetWidth(), 0); $imageObj->setTargetWidth(20); $this->assertSame($imageObj->getTargetWidth(), 20); $imageObj->setTargetWidth(50.125); $this->assertSame($imageObj->getTargetWidth(), 50); $this->assertSame($imageObj->getTargetPath(), ''); $imageObj->setTargetPath('foobar'); $this->assertSame($imageObj->getTargetPath(), 'foobar'); $this->assertSame($imageObj->getZoomLevel(), 0); $imageObj->setZoomLevel(54); $this->assertSame($imageObj->getZoomLevel(), 54); $this->assertSame($imageObj->getResizeMode(), 'crop'); $imageObj->setResizeMode('foobar'); $this->assertSame($imageObj->getResizeMode(), 'foobar'); $this->assertSame($imageObj->getOriginalPath(), 'dummy.jpg'); $this->assertSame($imageObj->getResizedPath(), ''); }