Skip to content

Commit f532ee4

Browse files
committed
Round scaled values before integer conversion
1 parent c98ce06 commit f532ee4

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

lib/ImageResize.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,12 @@ public function resizeToShortSide($max_short, $allow_enlarge = false)
465465
{
466466
if ($this->getSourceHeight() < $this->getSourceWidth()) {
467467
$ratio = $max_short / $this->getSourceHeight();
468-
$long = (int) ($this->getSourceWidth() * $ratio);
468+
$long = (int) round($this->getSourceWidth() * $ratio);
469469

470470
$this->resize($long, $max_short, $allow_enlarge);
471471
} else {
472472
$ratio = $max_short / $this->getSourceWidth();
473-
$long = (int) ($this->getSourceHeight() * $ratio);
473+
$long = (int) round($this->getSourceHeight() * $ratio);
474474

475475
$this->resize($max_short, $long, $allow_enlarge);
476476
}
@@ -489,12 +489,12 @@ public function resizeToLongSide($max_long, $allow_enlarge = false)
489489
{
490490
if ($this->getSourceHeight() > $this->getSourceWidth()) {
491491
$ratio = $max_long / $this->getSourceHeight();
492-
$short = (int) ($this->getSourceWidth() * $ratio);
492+
$short = (int) round($this->getSourceWidth() * $ratio);
493493

494494
$this->resize($short, $max_long, $allow_enlarge);
495495
} else {
496496
$ratio = $max_long / $this->getSourceWidth();
497-
$short = (int) ($this->getSourceHeight() * $ratio);
497+
$short = (int) round($this->getSourceHeight() * $ratio);
498498

499499
$this->resize($max_long, $short, $allow_enlarge);
500500
}
@@ -512,7 +512,7 @@ public function resizeToLongSide($max_long, $allow_enlarge = false)
512512
public function resizeToHeight($height, $allow_enlarge = false)
513513
{
514514
$ratio = $height / $this->getSourceHeight();
515-
$width = (int) ($this->getSourceWidth() * $ratio);
515+
$width = (int) round($this->getSourceWidth() * $ratio);
516516

517517
$this->resize($width, $height, $allow_enlarge);
518518

@@ -529,7 +529,7 @@ public function resizeToHeight($height, $allow_enlarge = false)
529529
public function resizeToWidth($width, $allow_enlarge = false)
530530
{
531531
$ratio = $width / $this->getSourceWidth();
532-
$height = (int) ($this->getSourceHeight() * $ratio);
532+
$height = (int) round($this->getSourceHeight() * $ratio);
533533

534534
$this->resize($width, $height, $allow_enlarge);
535535

@@ -552,11 +552,11 @@ public function resizeToBestFit($max_width, $max_height, $allow_enlarge = false)
552552

553553
$ratio = $this->getSourceHeight() / $this->getSourceWidth();
554554
$width = $max_width;
555-
$height = (int) ($width * $ratio);
555+
$height = (int) round($width * $ratio);
556556

557557
if ($height > $max_height) {
558558
$height = $max_height;
559-
$width = (int) ($height / $ratio);
559+
$width = (int) round($height / $ratio);
560560
}
561561

562562
return $this->resize($width, $height, $allow_enlarge);
@@ -570,8 +570,8 @@ public function resizeToBestFit($max_width, $max_height, $allow_enlarge = false)
570570
*/
571571
public function scale($scale)
572572
{
573-
$width = (int) ($this->getSourceWidth() * $scale / 100);
574-
$height = (int) ($this->getSourceHeight() * $scale / 100);
573+
$width = (int) round($this->getSourceWidth() * $scale / 100);
574+
$height = (int) round($this->getSourceHeight() * $scale / 100);
575575

576576
$this->resize($width, $height, true);
577577

@@ -642,7 +642,7 @@ public function crop($width, $height, $allow_enlarge = false, $position = self::
642642
if ($ratio_dest < $ratio_source) {
643643
$this->resizeToHeight($height, $allow_enlarge);
644644

645-
$excess_width = (int) (($this->getDestWidth() - $width) * $this->getSourceWidth() / $this->getDestWidth());
645+
$excess_width = (int) round(($this->getDestWidth() - $width) * $this->getSourceWidth() / $this->getDestWidth());
646646

647647
$this->source_w = $this->getSourceWidth() - $excess_width;
648648
$this->source_x = $this->getCropPosition($excess_width, $position);
@@ -651,7 +651,7 @@ public function crop($width, $height, $allow_enlarge = false, $position = self::
651651
} else {
652652
$this->resizeToWidth($width, $allow_enlarge);
653653

654-
$excess_height = (int) (($this->getDestHeight() - $height) * $this->getSourceHeight() / $this->getDestHeight());
654+
$excess_height = (int) round(($this->getDestHeight() - $height) * $this->getSourceHeight() / $this->getDestHeight());
655655

656656
$this->source_h = $this->getSourceHeight() - $excess_height;
657657
$this->source_y = $this->getCropPosition($excess_height, $position);
@@ -758,7 +758,7 @@ protected function getCropPosition($expectedSize, $position = self::CROPCENTER)
758758
$size = $expectedSize / 4;
759759
break;
760760
}
761-
return (int) $size;
761+
return (int) round($size);
762762
}
763763

764764
/**

0 commit comments

Comments
 (0)