/** * {@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 getImage hook. */ public function testExecuteResizeHook() { $GLOBALS['TL_HOOKS']['getImage'][] = [get_class($this), 'getImageHookCallback']; $file = new \File('dummy.jpg'); $imageObj = new Image($file); $imageObj->setTargetWidth(100)->setTargetHeight(100); $imageObj->executeResize(); $this->assertSame($imageObj->getResizedPath(), 'dummy.jpg%3B100%3B100%3Bcrop%3BContao%5CFile%3B%3BContao%5CImage'); }
/** * Tests the getImage hook. */ public function testGetImageHook() { $file = new \File('dummy.jpg'); // Build cache before adding the hook $imageObj = new Image($file); $imageObj->setTargetWidth(50)->setTargetHeight(50); $imageObj->executeResize(); $GLOBALS['TL_HOOKS'] = ['getImage' => [[get_class($this), 'getImageHookCallback']]]; $imageObj = new Image($file); $imageObj->setTargetWidth(100)->setTargetHeight(100); $imageObj->executeResize(); $this->assertSame('assets/dummy.jpg%26getImage_100_100_crop_Contao-File__Contao-Image.jpg', $imageObj->getResizedPath()); $imageObj = new Image($file); $imageObj->setTargetWidth(50)->setTargetHeight(50); $imageObj->executeResize(); $this->assertRegExp('(^assets/images/.*dummy.*.jpg$)', $imageObj->getResizedPath(), 'Hook should not get called for cached images'); $imageObj = new Image($file); $imageObj->setTargetWidth($file->width)->setTargetHeight($file->height); $imageObj->executeResize(); $this->assertSame('dummy.jpg', $imageObj->getResizedPath(), 'Hook should not get called if no resize is necessary'); unset($GLOBALS['TL_HOOKS']); }