Contao\Image::executeResize PHP Method

executeResize() public method

Resize the image
public executeResize ( )
    public function executeResize()
    {
        $image = $this->prepareImage();
        $resizeConfig = $this->prepareResizeConfig();
        if (!System::getContainer()->getParameter('contao.image.bypass_cache') && $this->getTargetPath() && !$this->getForceOverride() && file_exists(TL_ROOT . '/' . $this->getTargetPath()) && $this->fileObj->mtime <= filemtime(TL_ROOT . '/' . $this->getTargetPath())) {
            // HOOK: add custom logic
            if (isset($GLOBALS['TL_HOOKS']['executeResize']) && is_array($GLOBALS['TL_HOOKS']['executeResize'])) {
                foreach ($GLOBALS['TL_HOOKS']['executeResize'] as $callback) {
                    $return = \System::importStatic($callback[0])->{$callback[1]}($this);
                    if (is_string($return)) {
                        $this->resizedPath = \System::urlEncode($return);
                        return $this;
                    }
                }
            }
            $this->resizedPath = \System::urlEncode($this->getTargetPath());
            return $this;
        }
        $image = \System::getContainer()->get('contao.image.resizer')->resize($image, $resizeConfig, (new ResizeOptions())->setImagineOptions(\System::getContainer()->getParameter('contao.image.imagine_options'))->setTargetPath($this->targetPath ? TL_ROOT . '/' . $this->targetPath : null)->setBypassCache(\System::getContainer()->getParameter('contao.image.bypass_cache')));
        $this->resizedPath = $image->getPath();
        if (strpos($this->resizedPath, TL_ROOT . '/') === 0 || strpos($this->resizedPath, TL_ROOT . '\\') === 0) {
            $this->resizedPath = substr($this->resizedPath, strlen(TL_ROOT) + 1);
        }
        $this->resizedPath = \System::urlEncode($this->resizedPath);
        return $this;
    }

Usage Example

示例#1
0
 /**
  * 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');
 }
All Usage Examples Of Contao\Image::executeResize