Contao\Image::computeResize PHP Method

computeResize() public method

Calculate the resize coordinates
public computeResize ( ) : array
return array The resize coordinates (width, height, target_x, target_y, target_width, target_height)
    public function computeResize()
    {
        $resizeCoordinates = \System::getContainer()->get('contao.image.resize_calculator')->calculate($this->prepareResizeConfig(), new ImageDimensions(new Box($this->fileObj->viewWidth, $this->fileObj->viewHeight), $this->fileObj->viewWidth !== $this->fileObj->width), $this->prepareImportantPart());
        return array('width' => $resizeCoordinates->getCropSize()->getWidth(), 'height' => $resizeCoordinates->getCropSize()->getHeight(), 'target_x' => -$resizeCoordinates->getCropStart()->getX(), 'target_y' => -$resizeCoordinates->getCropStart()->getY(), 'target_width' => $resizeCoordinates->getSize()->getWidth(), 'target_height' => $resizeCoordinates->getSize()->getHeight());
    }

Usage Example

Esempio n. 1
0
 /**
  * Tests resizing with an important part.
  *
  * @param array $arguments
  * @param array $expectedResult
  *
  * @dataProvider getComputeResizeDataWithImportantPart
  */
 public function testComputeResizeWithImportantPart($arguments, $expectedResult)
 {
     /** @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 'dummy.jpg';
             case 'width':
             case 'viewWidth':
                 return $arguments[2];
             case 'height':
             case 'viewHeight':
                 return $arguments[3];
             default:
                 return null;
         }
     }));
     $imageObj = new Image($fileMock);
     $imageObj->setTargetWidth($arguments[0]);
     $imageObj->setTargetHeight($arguments[1]);
     $imageObj->setResizeMode($arguments[4]);
     $imageObj->setZoomLevel($arguments[5]);
     $imageObj->setImportantPart($arguments[6]);
     $this->assertEquals($expectedResult, $imageObj->computeResize());
 }