/** * {@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); }
/** * 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(), ''); }
/** * Tests the executeResize hook. */ public function testExecuteResizeHook() { $GLOBALS['TL_HOOKS'] = ['executeResize' => [[get_class($this), 'executeResizeHookCallback']]]; $file = new \File('dummy.jpg'); $imageObj = new Image($file); $imageObj->setTargetWidth(100)->setTargetHeight(100); $imageObj->setTargetPath('target.jpg'); $imageObj->executeResize(); $this->assertSame('assets/dummy.jpg%26executeResize_100_100_crop_target.jpg_Contao-Image.jpg', $imageObj->getResizedPath()); $imageObj = new Image($file); $imageObj->setTargetWidth($file->width)->setTargetHeight($file->height); $imageObj->executeResize(); $this->assertSame('assets/dummy.jpg%26executeResize_200_200_crop__Contao-Image.jpg', $imageObj->getResizedPath()); $imageObj = new Image($file); $imageObj->setTargetWidth($file->width)->setTargetHeight($file->height); file_put_contents(self::$rootDir . '/target.jpg', ''); $imageObj->setTargetPath('target.jpg'); $imageObj->executeResize(); $this->assertSame('assets/dummy.jpg%26executeResize_200_200_crop_target.jpg_Contao-Image.jpg', $imageObj->getResizedPath()); unset($GLOBALS['TL_HOOKS']); }