method resize documentation in bbn\File\Image
Resize the width and the height of the image. If is given only width or height the other dimension will be set on auto.
function($w = NULL, int $h = NULL, bool $crop = false, int $max_w = NULL, int $max_h = NULL)
{
if (!$this->test()) {
throw new Exception(X::_("The image is not valid"));
}
if (\is_array($w)) {
$max_w = $w['max_w'] ?? null;
$max_h = $w['max_h'] ?? null;
$crop = $w['crop'] ?? false;
$h = $w['h'] ?? null;
$w = $w['w'] ?? null;
}
if ($w || $h) {
if ($w && $h) {
$oratio = $this->w / $this->h;
$nratio = $w / $h;
if ($crop && ($oratio != $nratio)) {
if ($oratio < $nratio) {
$w2 = $w;
$h2 = floor(($w2 * $this->h) / $this->w);
$x = 0;
$y = floor(($h2 - $h) / 2);
}
else {
$h2 = $h;
$w2 = floor(($h2 * $this->w) / $this->h);
$y = 0;
$x = floor(($w2 - $w) / 2);
}
}
else{
$w2 = $w;
$h2 = $h;
}
}
else {
if ($w > 0) {
$w2 = $w;
$h2 = floor(($w2 * $this->h) / $this->w);
}
if ($h > 0) {
if (isset($h2)) {
if ($h2 > $h) {
$h2 = $h;
$w2 = floor(($h2 * $this->w) / $this->h);
}
}
else{
$h2 = $h;
$w2 = floor(($h2 * $this->w) / $this->h);
}
}
}
if (isset($w2, $h2)) {
if (self::isImagick()) {
$res = $this->img->resizeImage($w2, $h2, \Imagick::FILTER_LANCZOS, 1);
}
else{
$image = imagecreatetruecolor($w2,$h2);
if (in_array($this->ext, ['png', 'gif', 'svg', 'webp'])) {
imagecolorallocatealpha($image, 0, 0, 0, 127);
imagealphablending($image, false);
imagesavealpha($image, true);
}
$res = imagecopyresampled($image, $this->img, 0, 0, 0, 0, $w2, $h2, $this->w, $this->h);
$this->img = $image;
}
if ($res === true) {
if ($crop && isset($x, $y) && $this->crop($w, $h, $x, $y)) {
$this->w = $w;
$this->h = $h;
}
else {
$this->w = $w2;
$this->h = $h2;
}
}
else{
$this->error = X::_("There has been a problem while resizing");
}
}
else{
$this->error = X::_("Width and/or height is null");
}
}
return $this;
}
Resize the width and the height of the image. If is given only width or height the other dimension will be set on auto. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions