diff --git a/README.md b/README.md index 5ad7d50..0d68078 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -Calculates integer ratios for pixel-perfect image upscaling with optional aspect-ratio correction. +Calculates integer ratios and scaled-image size for pixel-perfect image upscaling with optional aspect-ratio correction. See the project [webpage](http://tanalin.com/en/projects/integer-scaling/) for details. \ No newline at end of file diff --git a/composer.json b/composer.json index f6b40ec..e73ce14 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "marat-tanalin/integer-scaling", - "version" : "1.2.3", + "version" : "1.3.0", "type": "library", "description": "Calculates integer ratios for pixel-perfect image upscaling with optional aspect-ratio correction.", "homepage": "http://tanalin.com/en/projects/integer-scaling/", diff --git a/src/IntegerScaling.php b/src/IntegerScaling.php index 0bb1b5e..7b4f387 100644 --- a/src/IntegerScaling.php +++ b/src/IntegerScaling.php @@ -4,8 +4,6 @@ namespace MaratTanalin; -require_once 'IntegerScaling/Ratios.php'; - class IntegerScaling { /** @@ -46,7 +44,7 @@ public static function calculateRatio(int $areaWidth, int $areaHeight, int $imag * @param int $imageHeight * @param float $aspectX * @param float $aspectY - * @return array(string => int) + * @return IntegerScaling\Ratios */ public static function calculateRatios(int $areaWidth, int $areaHeight, int $imageWidth, int $imageHeight, float $aspectX = 0.0, float $aspectY = 0.0) { if ($imageWidth * $aspectY === $imageHeight * $aspectX) { @@ -130,4 +128,90 @@ public static function calculateRatios(int $areaWidth, int $areaHeight, int $ima return new IntegerScaling\Ratios($ratioX, $ratioY); } + + /** + * Calculates size (width and height) of scaled image + * without aspect-ratio correction (square pixels). + * + * @param int $areaWidth + * @param int $areaHeight + * @param int $imageWidth + * @param int $imageHeight + * @return IntegerScaling\Size + */ + public static function calculateSize(int $areaWidth, int $areaHeight, int $imageWidth, int $imageHeight) { + $ratio = self::calculateRatio($areaWidth, $areaHeight, $imageWidth, $imageHeight); + + return new IntegerScaling\Size( + $imageWidth * $ratio, + $imageHeight * $ratio + ); + } + + /** + * Calculates size (width and height) of scaled image + * with aspect-ratio correction (rectangular pixels). + * + * @param int $areaWidth + * @param int $areaHeight + * @param int $imageWidth + * @param int $imageHeight + * @param float $aspectX + * @param float $aspectY + * @return IntegerScaling\Size + */ + public static function calculateSizeCorrected(int $areaWidth, int $areaHeight, + int $imageWidth, int $imageHeight, float $aspectX = 0.0, float $aspectY = 0.0) + { + $ratios = self::calculateRatios($areaWidth, $areaHeight, $imageWidth, $imageHeight, $aspectX, $aspectY); + + return new IntegerScaling\Size( + $imageWidth * $ratios->x, + $imageHeight * $ratios->y + ); + } + + /** + * Calculates size (width and height) of scaled image with aspect-ratio + * correction with integer vertical scaling ratio, but fractional horizontal + * scaling ratio for the purpose of achieving precise aspect ratio while + * still having integer vertical scaling e.g. for uniform scanlines. + * + * @param int $areaWidth + * @param int $areaHeight + * @param int $imageWidth + * @param int $imageHeight + * @param float $aspectX + * @param float $aspectY + * @return IntegerScaling\Size + */ + public static function calculateSizeCorrectedPerfectY(int $areaWidth, int $areaHeight, int $imageHeight, float $aspectX, float $aspectY) { + $imageWidth = $imageHeight * $aspectX / $aspectY; + + if ($areaHeight * $imageWidth < $areaWidth * $imageHeight) { + $areaSize = $areaHeight; + $imageSize = $imageHeight; + } + else { + $areaSize = $areaWidth; + $imageSize = $imageWidth; + } + + $ratio = floor($areaSize / $imageSize); + + if ($ratio < 1) { + $ratio = 1; + } + + $width = round($imageWidth * $ratio); + + if ($width > $areaWidth) { + $width--; + } + + return new IntegerScaling\Size( + $width, + $imageHeight * $ratio + ); + } } \ No newline at end of file diff --git a/src/IntegerScaling/Size.php b/src/IntegerScaling/Size.php new file mode 100644 index 0000000..f2e088a --- /dev/null +++ b/src/IntegerScaling/Size.php @@ -0,0 +1,15 @@ +width = $width; + $this->height = $height; + } +} \ No newline at end of file