mirror of
https://github.com/Intervention/image.git
synced 2025-08-06 22:06:36 +02:00
Merge pull request #2 from driesvints/dv/extend-resize-function
Extend resize function. Fixes #1
This commit is contained in:
@@ -186,40 +186,92 @@ class Image
|
|||||||
/**
|
/**
|
||||||
* Resize current image based on given width/height
|
* Resize current image based on given width/height
|
||||||
*
|
*
|
||||||
* @param mixed width|height width and height are optional, the not given
|
* Width and height are optional, the not given parameter is calculated
|
||||||
* parameter is calculated based on the given
|
* based on the given. The ratio boolean decides whether the resizing
|
||||||
|
* should keep the image ratio. You can also pass along a boolean to
|
||||||
|
* prevent the image from being upsized.
|
||||||
|
*
|
||||||
|
* @param integer $width The target width for the image
|
||||||
|
* @param integer $height The target height for the image
|
||||||
|
* @param boolean $ratio Determines if the image ratio should be preserved
|
||||||
|
* @param boolean $upsize Determines whether the image can be upsized
|
||||||
|
*
|
||||||
* @return Image
|
* @return Image
|
||||||
*/
|
*/
|
||||||
public function resize()
|
public function resize($width = null, $height = null, $ratio = false, $upsize = true)
|
||||||
{
|
{
|
||||||
$args = func_get_args();
|
// Evaluate passed parameters.
|
||||||
|
$width = isset($width) ? intval($width) : null;
|
||||||
|
$height = $max_height = isset($height) ? intval($height) : null;
|
||||||
|
$ratio = $ratio ? true : false;
|
||||||
|
$upsize = $upsize ? true : false;
|
||||||
|
|
||||||
if (array_key_exists(0, $args) && is_array($args[0])) {
|
// If the ratio needs to be kept.
|
||||||
|
if ($ratio) {
|
||||||
// extract 'width' and 'height'
|
// If both width and hight have been passed along, the width and
|
||||||
extract(array_only($args[0], array('width', 'height')));
|
// height parameters are maximum values.
|
||||||
$width = isset($width) ? intval($width) : null;
|
if ( ! is_null($width) && ! is_null($height)) {
|
||||||
$height = isset($height) ? intval($height) : null;
|
// First, calculate the height.
|
||||||
|
$height = intval($width / $this->width * $this->height);
|
||||||
if ( ! is_null($width) OR ! is_null($height)) {
|
|
||||||
// if width or height are not set, define values automatically
|
// If the height is too large, set it to the maximum
|
||||||
$width = is_null($width) ? intval($height / $this->height * $this->width) : $width;
|
// height and calculate the width.
|
||||||
$height = is_null($height) ? intval($width / $this->width * $this->height) : $height;
|
if ($height > $max_height) {
|
||||||
} else {
|
$height = $max_height;
|
||||||
// width or height not defined (resume with original values)
|
$width = intval($height / $this->height * $this->width);
|
||||||
throw new Exception('Width or Height needs to be defined as keys in paramater array');
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} elseif (array_key_exists(0, $args) && array_key_exists(1, $args) && is_numeric($args[0]) && is_numeric($args[1])) {
|
// If only one of width or height has been provided.
|
||||||
$width = intval($args[0]);
|
else if ($ratio && ( ! is_null($width) OR ! is_null($height))) {
|
||||||
$height = intval($args[1]);
|
$width = is_null($width) ? intval($height / $this->height * $this->width) : $width;
|
||||||
|
$height = is_null($height) ? intval($width / $this->width * $this->height) : $height;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_null($width) OR is_null($height)) {
|
// If the image can't be upsized, check if the given width and/or
|
||||||
|
// height are too large.
|
||||||
|
if ( ! $upsize) {
|
||||||
|
// If the given width is larger then the image width,
|
||||||
|
// then don't resize it.
|
||||||
|
if ( ! is_null($width) && $width > $this->width) {
|
||||||
|
$width = $this->width;
|
||||||
|
|
||||||
|
// If ratio needs to be kept, height is recalculated.
|
||||||
|
if ($ratio) {
|
||||||
|
$height = intval($width / $this->width * $this->height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the given height is larger then the image height,
|
||||||
|
// then don't resize it.
|
||||||
|
if ( ! is_null($height) && $height > $this->height) {
|
||||||
|
$height = $this->height;
|
||||||
|
|
||||||
|
// If ratio needs to be kept, width is recalculated.
|
||||||
|
if ($ratio) {
|
||||||
|
$width = intval($height / $this->height * $this->width);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If both the width and height haven't been passed along,
|
||||||
|
// throw an exception.
|
||||||
|
if (is_null($width) && is_null($height)) {
|
||||||
throw new Exception('width or height needs to be defined');
|
throw new Exception('width or height needs to be defined');
|
||||||
}
|
}
|
||||||
|
|
||||||
// create new image in new dimensions
|
// If only the width hasn't been set, keep the current width.
|
||||||
|
else if (is_null($width) ) {
|
||||||
|
$width = $this->width;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If only the height hasn't been set, keep the current height.
|
||||||
|
else if (is_null($height) ) {
|
||||||
|
$height = $this->height;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new image in new dimensions.
|
||||||
return $this->modify(0, 0, 0, 0, $width, $height, $this->width, $this->height);
|
return $this->modify(0, 0, 0, 0, $width, $height, $this->width, $this->height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -38,9 +38,29 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
|||||||
$this->assertEquals($img->width, 320);
|
$this->assertEquals($img->width, 320);
|
||||||
$this->assertEquals($img->height, 240);
|
$this->assertEquals($img->height, 240);
|
||||||
|
|
||||||
|
// Only resize the width.
|
||||||
|
$img = $this->getTestImage();
|
||||||
|
$img->resize(320);
|
||||||
|
$height = $img->height;
|
||||||
|
$this->assertInternalType('int', $img->width);
|
||||||
|
$this->assertInternalType('int', $img->height);
|
||||||
|
$this->assertEquals($img->width, 320);
|
||||||
|
// Check if the height is still the same.
|
||||||
|
$this->assertEquals($img->height, $height);
|
||||||
|
|
||||||
|
// Only resize the width.
|
||||||
|
$img = $this->getTestImage();
|
||||||
|
$img->resize(null, 240);
|
||||||
|
$width = $img->width;
|
||||||
|
$this->assertInternalType('int', $img->width);
|
||||||
|
$this->assertInternalType('int', $img->height);
|
||||||
|
// Check if the width is still the same.
|
||||||
|
$this->assertEquals($img->width, $width);
|
||||||
|
$this->assertEquals($img->height, 240);
|
||||||
|
|
||||||
// auto height
|
// auto height
|
||||||
$img = $this->getTestImage();
|
$img = $this->getTestImage();
|
||||||
$img->resize(array('width' => '320'));
|
$img->resize(320, null, true);
|
||||||
$this->assertInternalType('int', $img->width);
|
$this->assertInternalType('int', $img->width);
|
||||||
$this->assertInternalType('int', $img->height);
|
$this->assertInternalType('int', $img->height);
|
||||||
$this->assertEquals($img->width, 320);
|
$this->assertEquals($img->width, 320);
|
||||||
@@ -48,11 +68,27 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
|||||||
|
|
||||||
// auto width
|
// auto width
|
||||||
$img = $this->getTestImage();
|
$img = $this->getTestImage();
|
||||||
$img->resize(array('height' => '240'));
|
$img->resize(null, 240, true);
|
||||||
$this->assertInternalType('int', $img->width);
|
$this->assertInternalType('int', $img->width);
|
||||||
$this->assertInternalType('int', $img->height);
|
$this->assertInternalType('int', $img->height);
|
||||||
$this->assertEquals($img->width, 320);
|
$this->assertEquals($img->width, 320);
|
||||||
$this->assertEquals($img->height, 240);
|
$this->assertEquals($img->height, 240);
|
||||||
|
|
||||||
|
// Test image upsizing.
|
||||||
|
$img = $this->getTestImage();
|
||||||
|
// Keep original width and height.
|
||||||
|
$original_width = $img->width;
|
||||||
|
$original_height = $img->height;
|
||||||
|
// Increase values a bit.
|
||||||
|
$width = $original_width + 500;
|
||||||
|
$height = $original_height + 350;
|
||||||
|
// Try resizing to higher values while upsizing is set to false.
|
||||||
|
$img->resize($width, $height, false, false);
|
||||||
|
$this->assertInternalType('int', $img->width);
|
||||||
|
$this->assertInternalType('int', $img->height);
|
||||||
|
// Check if width and height are still the same.
|
||||||
|
$this->assertEquals($img->width, $original_width);
|
||||||
|
$this->assertEquals($img->height, $original_height);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGrabImage()
|
public function testGrabImage()
|
||||||
|
Reference in New Issue
Block a user