N°3124 - Refactorize ResizeImageToFit methods (#734)

This commit is contained in:
Timmy38
2025-08-08 10:43:35 +02:00
committed by GitHub
parent 5b9e0a1d4f
commit 71386198cf
7 changed files with 227 additions and 190 deletions

View File

@@ -139,4 +139,115 @@ class ormDocumentTest extends ItopDataTestCase
],
];
}
public function testResizeImageToFitShouldResizeImageWhenImageIsTooBig()
{
$sImageData = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAoAAAAICAIAAABPmPnhAAAAe0lEQVQI132OMQoCMRRE3/9Z3M126V0kB9BCvICnziXs7QIWlttqpWMRFQT1VcMbGMb4xPoQ18uWL4eTxxglSaq1Au8OwM1TSi3nnLGnzxKA4fM8N1VKQVyPZ6Br6s4Xhj7st9OwcNy61yUsGEK3Nmu+mUawcbfiN85fHsBoHdXt5HATAAAAAElFTkSuQmCC');
$sMimeType = 'image/png';
$sFileName = 'MyImage.png';
$oDoc = new ormDocument($sImageData, $sMimeType, $sFileName);
$iMawWidth = 6;
$iMaxHeight = 5;
$oResult = $oDoc->ResizeImageToFit($iMawWidth, $iMaxHeight, $aDimensions);
$aRealDimensions = \utils::GetImageSize($oResult->GetData());
$aActualDimensions = [
'width' => $aRealDimensions[0],
'height' => $aRealDimensions[1],
];
$this->assertNotSame( $oDoc, $oResult, 'ResizeImageToFit should return a new object when there have been some modifications');
$this->assertIsArray( $aDimensions, 'ResizeImageToFit should fill aDimension with the dimensions of the new image when there are no issues');
$this->assertEquals( $aDimensions, $aActualDimensions, 'The returned dimensions should match the real dimensions of the image');
$this->assertLessThanOrEqual($iMawWidth, $aActualDimensions['width'], 'The new width should be less than or equal to max width');
$this->assertLessThanOrEqual($iMaxHeight, $aActualDimensions['height'], 'The new height should be less than or equal to max height');
}
public function testResizeImageToFitShouldDoNothingWhenImageIsAlreadySmallEnough()
{
$sImageData = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAoAAAAICAIAAABPmPnhAAAAe0lEQVQI132OMQoCMRRE3/9Z3M126V0kB9BCvICnziXs7QIWlttqpWMRFQT1VcMbGMb4xPoQ18uWL4eTxxglSaq1Au8OwM1TSi3nnLGnzxKA4fM8N1VKQVyPZ6Br6s4Xhj7st9OwcNy61yUsGEK3Nmu+mUawcbfiN85fHsBoHdXt5HATAAAAAElFTkSuQmCC');
$sMimeType = 'image/png';
$sFileName = 'MyImage.png';
$oDoc = new ormDocument($sImageData, $sMimeType, $sFileName);
$iMawWidth = 10;
$iMaxHeight = 8;
$oResult = $oDoc->ResizeImageToFit($iMawWidth, $iMaxHeight, $aDimensions);
$this->assertSame( $oDoc, $oResult, 'ResizeImageToFit should return the same object when there have been no modifications');
$this->assertIsArray( $aDimensions, 'ResizeImageToFit should fill aDimension with the dimensions of the image when there are no issues');
}
public function testResizeImageToFitShouldDoNothingWhenItCannotReadTheImage()
{
$sImageData = 'garbagedata';
$sMimeType = 'image/png';
$sFileName = 'MyImage.png';
$oDoc = new ormDocument($sImageData, $sMimeType, $sFileName);
$iMawWidth = 10;
$iMaxHeight = 8;
$oResult = $oDoc->ResizeImageToFit($iMawWidth, $iMaxHeight, $aDimensions);
$this->assertSame( $oDoc, $oResult, 'ResizeImageToFit should return the same object when there have been no modifications');
$this->assertNull( $aDimensions, 'ResizeImageToFit should fill aDimension with null when there are issues');
}
public function testResizeImageToFitShouldDoNothingWhenItDoesNotHandleTheMimeType()
{
$sImageData = base64_decode('Qk3mAAAAAAAAAEYAAAA4AAAACgAAAAgAAAABABAAAwAAAKAAAAAjLgAAIy4AAAAAAAAAAAAAAHwAAOADAAAfAAAAAAAAAMQExATEBMQExATEBMQExATEBMQExATEBMQExATEBMQExATEBMQExAQAAAAAAAAAAAAAAAAgBMQgxATEBAAAAAAAAAAAAAAAACAExCAgBAAAIQT/f/9/1loAACAAxATEGMQEAABjDP9//3//fwAAxATEBMQUxAQAACEE/3//f3tvAADEBMQExATEBAAAAAAAAAAAAAAAACAAxATEBMQEAAA=');
$sMimeType = 'image/bmp';
$sFileName = 'MyImage.bmp';
$oDoc = new ormDocument($sImageData, $sMimeType, $sFileName);
$iMawWidth = 5;
$iMaxHeight = 5;
$oResult = $oDoc->ResizeImageToFit($iMawWidth, $iMaxHeight, $aDimensions);
$this->assertSame( $oDoc, $oResult, 'ResizeImageToFit should return the same object when there have been no modifications');
$this->assertNull( $aDimensions, 'ResizeImageToFit should fill aDimension with null when there are issues');
}
public function testResizeImageToFitShouldNotResizeWhenMaximumIs0()
{
$sImageData = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAoAAAAICAIAAABPmPnhAAAAe0lEQVQI132OMQoCMRRE3/9Z3M126V0kB9BCvICnziXs7QIWlttqpWMRFQT1VcMbGMb4xPoQ18uWL4eTxxglSaq1Au8OwM1TSi3nnLGnzxKA4fM8N1VKQVyPZ6Br6s4Xhj7st9OwcNy61yUsGEK3Nmu+mUawcbfiN85fHsBoHdXt5HATAAAAAElFTkSuQmCC');
$sMimeType = 'image/png';
$sFileName = 'MyImage.png';
$oDoc = new ormDocument($sImageData, $sMimeType, $sFileName);
$iMawWidth = 0;
$iMaxHeight = 0;
$oResult = $oDoc->ResizeImageToFit($iMawWidth, $iMaxHeight, $aDimensions);
$this->assertSame( $oDoc, $oResult, 'ResizeImageToFit should return the same object when there have been no modifications');
$this->assertIsArray( $aDimensions, 'ResizeImageToFit should fill aDimension with the dimensions of the image when there are no issues');
}
public function testResizeImageToFitShouldIgnoreMaximum0Axis()
{
$sImageData = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAoAAAAICAIAAABPmPnhAAAAe0lEQVQI132OMQoCMRRE3/9Z3M126V0kB9BCvICnziXs7QIWlttqpWMRFQT1VcMbGMb4xPoQ18uWL4eTxxglSaq1Au8OwM1TSi3nnLGnzxKA4fM8N1VKQVyPZ6Br6s4Xhj7st9OwcNy61yUsGEK3Nmu+mUawcbfiN85fHsBoHdXt5HATAAAAAElFTkSuQmCC');
$sMimeType = 'image/png';
$sFileName = 'MyImage.png';
$oDoc = new ormDocument($sImageData, $sMimeType, $sFileName);
$iMawWidth = 5;
$iMaxHeight = 0;
$oResult = $oDoc->ResizeImageToFit($iMawWidth, $iMaxHeight, $aDimensions);
$aRealDimensions = \utils::GetImageSize($oResult->GetData());
$aActualDimensions = [
'width' => $aRealDimensions[0],
'height' => $aRealDimensions[1],
];
$this->assertNotSame( $oDoc, $oResult, 'ResizeImageToFit should return a new object when there have been some modifications');
$this->assertIsArray( $aDimensions, 'ResizeImageToFit should fill aDimension with the dimensions of the new image when there are no issues');
$this->assertEquals( $aDimensions, $aActualDimensions, 'The returned dimensions should match the real dimensions of the image');
$this->assertEquals($iMawWidth, $aActualDimensions['width'], 'The new width should be exactly the max width');
$this->assertGreaterThanOrEqual($iMaxHeight, $aActualDimensions['height'], 'The new height should not be 0');
}
}