使用 GD 调整图像大小问题

发布于 2025-01-05 01:50:59 字数 1215 浏览 1 评论 0原文

我已经使用 GD 库成功调整了图像大小。我将任何图像的大小调整为 350 x 250,问题是有些图片在调整大小时看起来不太好(拉伸),因为我将它们调整为固定大小。我有一个 350 x 250 的空间,需要调整图片大小,我不介意图片尺寸是否小于 350 x 250,只要它不拉伸即可。我该如何解决这个问题?

                      $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) ;

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) ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二货你真萌 2025-01-12 01:50:59

尝试使用我不久前编写的这个函数:

    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; 
    }

如果你说不拉伸图像,它会计算一个新的尺寸,使其适合你的新尺寸。

将其用作:

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

现在使用 imagepng() 将 $resized 存储在磁盘上。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文