zista
zista

Reputation: 197

Image resize issue using GD

I have successfully resized images using GD library. I resize any image to 350 x 250, the problem is tat some pictures don't look good (stretch) when they are resized as i am resizing them to a fixed size. I have a space of 350 x 250 where resize picture needs to be fit, I don't mind if the pic size is smaller than 350 x 250 as long as it does not stretch. How do i solve this problem?

                      $save = "$directory/" . $file_name; //This is the new file you saving
                      $file = "$directory/" . $file_name; //This is the original file

                      list($width, $height) = getimagesize($file) ; 
                      $modwidth = 350; 
                      if ($width > $height) {
                      $y = 0;
                      $x = ($width - $height) / 2;
                      $smallestSide = $height;
                    } else {
                      $x = 0;
                      $y = ($height - $width) / 2;
                      $smallestSide = $width;
                    }

                      $diff = $width / $modwidth;

                      $modheight = 250; 
                      $tn = imagecreatetruecolor($modwidth, $modheight) ; 
                      $image = imagecreatefromjpeg($file) ; 
                      imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
                      imagejpeg($tn, $save, 100) ;

Upvotes: 2

Views: 1568

Answers (1)

WoLfulus
WoLfulus

Reputation: 1977

Try using this function I've written some time ago:

    public function resize($img, $width, $height, $stretch = false)
    {
        $temp = imagecreatetruecolor($width, $height);
        imagealphablending($temp, true);
        imagesavealpha($temp, true);

        $bg = imagecolorallocatealpha($temp, 0, 0, 0, 127); // Background color
        imagefill($temp, 0, 0, $bg);

        if ($stretch)
        {
            imagecopyresampled($temp, img, 0, 0, 0, 0, $width, $height, imagesx($img), imagesy($img)); 
        }
        else
        {          
            if (imagesx($img) <= $width && imagesy($img) <= $height)
            {
                $fwidth = imagesx($img);
                $fheight = imagesy($img);
            }
            else
            {
                $wscale = $width / imagesx($img);
                $hscale = $height / imagesy($img);
                $scale = min($wscale, $hscale);
                $fwidth = $scale * imagesx($img);
                $fheight = $scale * imagesy($img);
            }
            imagecopyresampled($temp,                             
                $img,                                      
                ($width - $fwidth) / 2, ($height - $fheight) / 2,   
                0, 0,                                              
                $fwidth, $fheight,                                 
                imagesx($img), imagesy($img)               
            );
        }
        return $temp; 
    }

if you say not to stretch the image, it will calculate a new size making it fit your new size.

use it as:

...
$image = imagecreatefromjpeg($file);
$resized = resize($image, 350, 250, false); // false = don't stretch
imagejpeg($resized, $save, 100);
...

now store $resized on the disk using imagepng() for example.

Upvotes: 2

Related Questions