1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-22 13:32:56 +02:00

Rewrite resize function. Fixes #1

The resize function is now extended so it supports more functionality. Parameter passing with arrays has been removed.

New functionality included:
- Resizing of a single image side
- Preserve image ratio when both width and height have been set (treated as maximum values)
- Prevent image from being upsized

Signed-off-by: Dries Vints <dries.vints@gmail.com>
This commit is contained in:
Dries Vints
2013-02-24 16:30:38 +01:00
parent 3ab0b923a2
commit b2ed4f4db7

View File

@@ -186,40 +186,92 @@ class Image
/**
* Resize current image based on given width/height
*
* @param mixed width|height width and height are optional, the not given
* parameter is calculated based on the given
* Width and height are optional, the not given parameter is calculated
* 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
*/
public function resize()
public function resize($width = null, $height = null, $ratio = false, $upsize = true)
{
$args = func_get_args();
if (array_key_exists(0, $args) && is_array($args[0])) {
// extract 'width' and 'height'
extract(array_only($args[0], array('width', 'height')));
// Evaluate passed parameters.
$width = isset($width) ? intval($width) : null;
$height = isset($height) ? intval($height) : null;
$height = $max_height = isset($height) ? intval($height) : null;
$ratio = $ratio ? true : false;
$upsize = $upsize ? true : false;
if ( ! is_null($width) OR ! is_null($height)) {
// if width or height are not set, define values automatically
// If the ratio needs to be kept.
if ($ratio) {
// If both width and hight have been passed along, the width and
// height parameters are maximum values.
if ( ! is_null($width) && ! is_null($height)) {
// First, calculate the height.
$height = intval($width / $this->width * $this->height);
// If the height is too large, set it to the maximum
// height and calculate the width.
if ($height > $max_height) {
$height = $max_height;
$width = intval($height / $this->height * $this->width);
}
}
// If only one of width or height has been provided.
else if ($ratio && ( ! is_null($width) OR ! is_null($height))) {
$width = is_null($width) ? intval($height / $this->height * $this->width) : $width;
$height = is_null($height) ? intval($width / $this->width * $this->height) : $height;
} else {
// width or height not defined (resume with original values)
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])) {
$width = intval($args[0]);
$height = intval($args[1]);
// 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 (is_null($width) OR is_null($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');
}
// 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);
}