1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-01 11:30:16 +02:00

fixed bug when resizing exceeds given values

This commit is contained in:
Oliver Vogel
2014-05-24 20:34:28 +02:00
parent 6638929324
commit 055d579403
2 changed files with 124 additions and 25 deletions

View File

@@ -109,6 +109,71 @@ class Size
);
}
// new size with dominant width
$dominant_w_size = clone $this;
$dominant_w_size->resizeHeight($height, $callback);
$dominant_w_size->resizeWidth($width, $callback);
// new size with dominant height
$dominant_h_size = clone $this;
$dominant_h_size->resizeWidth($width, $callback);
$dominant_h_size->resizeHeight($height, $callback);
// decide which size to use
if ($dominant_h_size->fitsInto(new self($width, $height))) {
$this->set($dominant_h_size->width, $dominant_h_size->height);
} else {
$this->set($dominant_w_size->width, $dominant_w_size->height);
}
return $this;
}
/**
* Scale size according to given constraints
*
* @param integer $width
* @param Closure $callback
* @return Size
*/
private function resizeWidth($width, Closure $callback = null)
{
$constraint = $this->getConstraint($callback);
if ($constraint->isFixed(Constraint::UPSIZE)) {
$max_width = $constraint->getSize()->getWidth();
$max_height = $constraint->getSize()->getHeight();
}
if (is_numeric($width)) {
if ($constraint->isFixed(Constraint::UPSIZE)) {
$this->width = ($width > $max_width) ? $max_width : $width;
} else {
$this->width = $width;
}
if ($constraint->isFixed(Constraint::ASPECTRATIO)) {
$h = intval(round($this->width / $constraint->getSize()->getRatio()));
if ($constraint->isFixed(Constraint::UPSIZE)) {
$this->height = ($h > $max_height) ? $max_height : $h;
} else {
$this->height = $h;
}
}
}
}
/**
* Scale size according to given constraints
*
* @param integer $width
* @param Closure $callback
* @return Size
*/
private function resizeHeight($height, Closure $callback = null)
{
$constraint = $this->getConstraint($callback);
if ($constraint->isFixed(Constraint::UPSIZE)) {
@@ -134,27 +199,6 @@ class Size
}
}
}
if (is_numeric($width)) {
if ($constraint->isFixed(Constraint::UPSIZE)) {
$this->width = ($width > $max_width) ? $max_width : $width;
} else {
$this->width = $width;
}
if ($constraint->isFixed(Constraint::ASPECTRATIO)) {
$h = intval(round($this->width / $constraint->getSize()->getRatio()));
if ($constraint->isFixed(Constraint::UPSIZE)) {
$this->height = ($h > $max_height) ? $max_height : $h;
} else {
$this->height = $h;
}
}
}
return $this;
}
/**

View File

@@ -77,6 +77,11 @@ class SizeTest extends PHPUnit_Framework_TestCase
$this->assertEquals(1000, $size->width);
$this->assertEquals(750, $size->height);
$size = new Size(800, 600);
$size->resize(2000, 1000, function ($c) { $c->aspectRatio(); });
$this->assertEquals(1333, $size->width);
$this->assertEquals(1000, $size->height);
$size = new Size(800, 600);
$size->resize(null, 3000, function ($c) { $c->aspectRatio(); });
$this->assertEquals(4000, $size->width);
@@ -92,6 +97,11 @@ class SizeTest extends PHPUnit_Framework_TestCase
$this->assertEquals(100, $size->width);
$this->assertEquals(75, $size->height);
$size = new Size(800, 600);
$size->resize(400, 100, function ($c) { $c->aspectRatio(); });
$this->assertEquals(133, $size->width);
$this->assertEquals(100, $size->height);
$size = new Size(800, 600);
$size->resize(null, 300, function ($c) { $c->aspectRatio(); });
$this->assertEquals(400, $size->width);
@@ -111,6 +121,21 @@ class SizeTest extends PHPUnit_Framework_TestCase
$size->resize(223, null, function ($c) { $c->aspectRatio(); });
$this->assertEquals(223, $size->width);
$this->assertEquals(167, $size->height);
$size = new Size(600, 800);
$size->resize(300, 300, function ($c) { $c->aspectRatio(); });
$this->assertEquals(225, $size->width);
$this->assertEquals(300, $size->height);
$size = new Size(800, 600);
$size->resize(400, 10, function ($c) { $c->aspectRatio(); });
$this->assertEquals(13, $size->width);
$this->assertEquals(10, $size->height);
$size = new Size(800, 600);
$size->resize(1000, 1200, function ($c) { $c->aspectRatio(); });
$this->assertEquals(1000, $size->width);
$this->assertEquals(750, $size->height);
}
public function testResizeWithCallbackUpsize()
@@ -125,6 +150,11 @@ class SizeTest extends PHPUnit_Framework_TestCase
$this->assertEquals(400, $size->width);
$this->assertEquals(600, $size->height);
$size = new Size(800, 600);
$size->resize(1000, 400, function ($c) { $c->upsize(); });
$this->assertEquals(800, $size->width);
$this->assertEquals(400, $size->height);
$size = new Size(800, 600);
$size->resize(400, 300, function ($c) { $c->upsize(); });
$this->assertEquals(400, $size->width);
@@ -155,8 +185,13 @@ class SizeTest extends PHPUnit_Framework_TestCase
$size = new Size(800, 600);
$size->resize(1000, 300, function ($c) { $c->aspectRatio(); $c->upsize(); });
$this->assertEquals(800, $size->width);
$this->assertEquals(600, $size->height);
$this->assertEquals(400, $size->width);
$this->assertEquals(300, $size->height);
$size = new Size(800, 600);
$size->resize(400, 1000, function ($c) { $c->aspectRatio(); $c->upsize(); });
$this->assertEquals(400, $size->width);
$this->assertEquals(300, $size->height);
$size = new Size(800, 600);
$size->resize(400, null, function ($c) { $c->aspectRatio(); $c->upsize(); });
@@ -168,6 +203,16 @@ class SizeTest extends PHPUnit_Framework_TestCase
$this->assertEquals(400, $size->width);
$this->assertEquals(300, $size->height);
$size = new Size(800, 600);
$size->resize(1000, null, function ($c) { $c->aspectRatio(); $c->upsize(); });
$this->assertEquals(800, $size->width);
$this->assertEquals(600, $size->height);
$size = new Size(800, 600);
$size->resize(null, 1000, function ($c) { $c->aspectRatio(); $c->upsize(); });
$this->assertEquals(800, $size->width);
$this->assertEquals(600, $size->height);
$size = new Size(800, 600);
$size->resize(100, 100, function ($c) { $c->aspectRatio(); $c->upsize(); });
$this->assertEquals(100, $size->width);
@@ -175,8 +220,18 @@ class SizeTest extends PHPUnit_Framework_TestCase
$size = new Size(800, 600);
$size->resize(300, 200, function ($c) { $c->aspectRatio(); $c->upsize(); });
$this->assertEquals(300, $size->width);
$this->assertEquals(225, $size->height);
$this->assertEquals(267, $size->width);
$this->assertEquals(200, $size->height);
$size = new Size(600, 800);
$size->resize(300, 300, function ($c) { $c->aspectRatio(); $c->upsize(); });
$this->assertEquals(225, $size->width);
$this->assertEquals(300, $size->height);
$size = new Size(800, 600);
$size->resize(400, 10, function ($c) { $c->aspectRatio(); $c->upsize(); });
$this->assertEquals(13, $size->width);
$this->assertEquals(10, $size->height);
}
public function testRelativePosition()