• 26-05-2022, 14:38:50
    #1
    Selamlar, buraya bir konu açmak istedim konu hakkında bilgisi olanların fikir verebileceğini düşünerek. Aşağıdaki kod ile görselleri panelden verdiğim width ve height değeri ile yeniden oluşturuyorum. Fakat görseller şu an orantısız şekilde oluşuyor ve yalnızca width değerine göre olmuyor. Width değerini verdiğimde height değerinin oran korunarak otomatik oluşmasını nasıl sağlayabilirim. Width ve height değerini sabit olarak verirsem bazı görsel boyutları yüklerken farklı olduğu için sağında solunda beyaz boşluk çıkıyor. Beyaz rengi saydam yapabilirim ama o da front tarafındaki kötü görünüş yüzünden işime gelmiyor.

    Ö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;
        }
  • 26-05-2022, 14:42:23
    #2
    width yüzde kaç azalıyor ona bakın height ı hesaplatın örneğin şuanki boyut 1000x500 ise yeni width 500 ise oran 1000/500 den 2 dir yani height 500/2 den 250 olmalı
  • 26-05-2022, 15:17:13
    #3
    Aeknasd145 adlı üyeden alıntı: mesajı görüntüle
    width yüzde kaç azalıyor ona bakın height ı hesaplatın örneğin şuanki boyut 1000x500 ise yeni width 500 ise oran 1000/500 den 2 dir yani height 500/2 den 250 olmalı
    Cevabınız için teşekkür ederim bu mantık ile deneyeceğim halledersem ilk mesajımı güncelleyeceğim.
  • 26-05-2022, 20:35:00
    #4
    Hunper adlı üyeden alıntı: mesajı görüntüle
    Cevabınız için teşekkür ederim bu mantık ile deneyeceğim halledersem ilk mesajımı güncelleyeceğim.
    asıl ben teşekkür ederim, ben de aynı şey için fonksiyon yazcaktım benim de diğer kısımları işime yaramış oldu