Php Opencart 3仅调整图像宽度

Php Opencart 3仅调整图像宽度,php,resize,opencart,Php,Resize,Opencart,属于opencart的图像大小调整代码。图像采用宽度和高度值。我不希望它取高度值。我也不希望背景颜色是。因为当图片获得宽度时,高度会自动调整 我应该如何组织这个 public function resize(int $width = 0, int $height = 0, $default = '') { if (!$this->width || !$this->height) { return; } $xpos = 0; $ypos

属于opencart的图像大小调整代码。图像采用宽度和高度值。我不希望它取高度值。我也不希望背景颜色是。因为当图片获得宽度时,高度会自动调整

我应该如何组织这个

public function resize(int $width = 0, int $height = 0, $default = '') {
    if (!$this->width || !$this->height) {
        return;
    }

    $xpos = 0;
    $ypos = 0;
    $scale = 1;

    $scale_w = $width / $this->width;
    $scale_h = $height / $this->height;

    if ($default == 'w') {
        $scale = $scale_w;
    } elseif ($default == 'h') {
        $scale = $scale_h;
    } else {
        $scale = min($scale_w, $scale_h);
    }

    if ($scale == 1 && $scale_h == $scale_w && ($this->mime != 'image/png' && $this->mime != 'image/webp')) {
        return;
    }

    $new_width = (int)($this->width * $scale);
    $new_height = (int)($this->height * $scale);
    $xpos = (int)(($width - $new_width) / 2);
    $ypos = (int)(($height - $new_height) / 2);

    $image_old = $this->image;
    $this->image = imagecreatetruecolor($width, $height);

    if ($this->mime == 'image/png') {
        imagealphablending($this->image, false);
        imagesavealpha($this->image, true);

        $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);

        imagecolortransparent($this->image, $background);

    } else if ($this->mime == 'image/webp') {
        imagealphablending($this->image, false);
        imagesavealpha($this->image, true);

        $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);

        imagecolortransparent($this->image, $background);
    } else {
        $background = imagecolorallocate($this->image, 255, 255, 255);
    }

    imagefilledrectangle($this->image, 0, 0, $width, $height, $background);

    imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->width, $this->height);
    imagedestroy($image_old);

    $this->width = $width;
    $this->height = $height;
}