Özetle görseller oluşturulurken width değerinin verdiğim boyutta, height değerinin ise oran korunarak otomatik sağlanmasını istiyorum. Bu konuda yorum yazabilirseniz çok sevinirim. Buradan sonuç çıkmazsa ücretli destek için başka kategoride konu açacağım.
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 = 1;
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);
$xpos = 0;
$ypos = 0;
$image_old = $this->image;
$this->image = imagecreatetruecolor($width, $new_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, $new_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 = $new_height;
}EDİT Düzenlenmiş Hali: Width değerinin scale oranını height değeri için kullandım ve sorun çözüldü. Aeknasd145 yardımı için teşekkür ederim.
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 = $width / $this->width;
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 = 0;
$ypos = 0;
$image_old = $this->image;
$this->image = imagecreatetruecolor($width, $new_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, $new_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 = $new_height;
}