mirror of
https://github.com/Intervention/image.git
synced 2025-08-22 21:42:53 +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:
@@ -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) {
|
||||||
|
// 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);
|
||||||
|
|
||||||
// extract 'width' and 'height'
|
// If the height is too large, set it to the maximum
|
||||||
extract(array_only($args[0], array('width', 'height')));
|
// height and calculate the width.
|
||||||
$width = isset($width) ? intval($width) : null;
|
if ($height > $max_height) {
|
||||||
$height = isset($height) ? intval($height) : null;
|
$height = $max_height;
|
||||||
|
$width = intval($height / $this->height * $this->width);
|
||||||
if ( ! is_null($width) OR ! is_null($height)) {
|
}
|
||||||
// if width or height are not set, define values automatically
|
|
||||||
$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])) {
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user