GD 缩略图生成器出现问题

发布于 2024-07-09 06:56:07 字数 587 浏览 4 评论 0原文

我正在使用 PHP 生成缩略图。 问题是我有一个缩略图需要设定的宽度和高度,并且图像经常被拉伸。

我想要的是图像保持相同的比例,并且左侧和右侧只有黑色填充物(或任何颜色)。 适合高图像或顶部和顶部图像 底部用于宽幅图像。

这是我当前正在使用的代码:(为了可读性而简化了一点)

$temp_image_file = imagecreatefromjpeg("http://www.example.com/image.jpg");

list($width,$height) = getimagesize("http://www.example.com/image.jpg");

$image_file = imagecreatetruecolor(166,103);

imagecopyresampled($image_file,$temp_image_file,0,0,0,0,166,103,$width,$height);
imagejpeg($image_file,"thumbnails/thumbnail.jpg",50);

imagedestroy($temp_image_file);
imagedestroy($image_file);

I'm using PHP to generate thumbnails. The problem is that I have a set width and height the thumbnails need to be and often times the images are stretched.

What I'd like is the image to remain at the same proportions and just have black filler (or any color) either on the left & right for tall images or top & bottom for wide images.

Here is the code I'm currently using: (dumbed down a bit for readability)

$temp_image_file = imagecreatefromjpeg("http://www.example.com/image.jpg");

list($width,$height) = getimagesize("http://www.example.com/image.jpg");

$image_file = imagecreatetruecolor(166,103);

imagecopyresampled($image_file,$temp_image_file,0,0,0,0,166,103,$width,$height);
imagejpeg($image_file,"thumbnails/thumbnail.jpg",50);

imagedestroy($temp_image_file);
imagedestroy($image_file);

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

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

发布评论

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

评论(5

没有伤那来痛 2024-07-16 06:56:08

好吧,它工作了,这就是我最终所做的:

$filename = "http://www.example.com/image.jpg";

list($width,$height) = getimagesize($filename);

$width_ratio = 166 / $width;
if ($height * $width_ratio <= 103)
{
    $adjusted_width = 166;
    $adjusted_height = $height * $width_ratio;
}
else
{
    $height_ratio = 103 / $height;
    $adjusted_width = $width * $height_ratio;
    $adjusted_height = 103;
}

$image_p = imagecreatetruecolor(166,103);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p,$image,ceil((166 - $adjusted_width) / 2),ceil((103 - $adjusted_height) / 2),0,0,ceil($adjusted_width),ceil($adjusted_height),$width,$height);

imagejpeg($image_p,"thumbnails/thumbnail.jpg",50);

Okay got it working, here's what I ended up doing:

$filename = "http://www.example.com/image.jpg";

list($width,$height) = getimagesize($filename);

$width_ratio = 166 / $width;
if ($height * $width_ratio <= 103)
{
    $adjusted_width = 166;
    $adjusted_height = $height * $width_ratio;
}
else
{
    $height_ratio = 103 / $height;
    $adjusted_width = $width * $height_ratio;
    $adjusted_height = 103;
}

$image_p = imagecreatetruecolor(166,103);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p,$image,ceil((166 - $adjusted_width) / 2),ceil((103 - $adjusted_height) / 2),0,0,ceil($adjusted_width),ceil($adjusted_height),$width,$height);

imagejpeg($image_p,"thumbnails/thumbnail.jpg",50);
小矜持 2024-07-16 06:56:08

这是我编写的一个函数,它接受 GD 图像对象、最大宽度、最大高度,并在这些参数内重新缩放:

function resized($im, $mx, $my) {
  $x = $nx = imagesx($im);
  $y = $ny = imagesy($im);
  $ar = $x / $y;
  while ($nx > $mx || $ny > $my) {
    if ($nx > $mx) {
      $nx = $mx;
      $ny = $nx / $ar;
    }
    if ($ny > $my) {
      $ny = $my;
      $nx = $ny * $ar;
    }
  }
  if ($nx != $x) {
    $im2 = imagecreatetruecolor($nx, $ny);
    imagecopyresampled($im2, $im, 0, 0, 0, 0, $nx, $ny, $x, $y);
    return $im2;
  } else {
    return $im;
  }
}

如果生成的图像不需要重新缩放,则它只返回原始图像。

Here's a function I wrote that takes a GD image object, max width, max height, and rescales within those parameters:

function resized($im, $mx, $my) {
  $x = $nx = imagesx($im);
  $y = $ny = imagesy($im);
  $ar = $x / $y;
  while ($nx > $mx || $ny > $my) {
    if ($nx > $mx) {
      $nx = $mx;
      $ny = $nx / $ar;
    }
    if ($ny > $my) {
      $ny = $my;
      $nx = $ny * $ar;
    }
  }
  if ($nx != $x) {
    $im2 = imagecreatetruecolor($nx, $ny);
    imagecopyresampled($im2, $im, 0, 0, 0, 0, $nx, $ny, $x, $y);
    return $im2;
  } else {
    return $im;
  }
}

If the resulting image doesn't need rescaling then it just returns the original image.

一桥轻雨一伞开 2024-07-16 06:56:08

看看这个上传类。 它可以满足您的需求,而且还可以做更多的事情。

Have a look at this upload class. It does what you want and a lot more besides.

雨后彩虹 2024-07-16 06:56:08

这会将图像重新缩放为宽度和高度的较大尺寸,然后将其裁剪为正确的尺寸。

// Crete an image forced to width and height
    function createFixedImage($img, $id=0, $preFix=false, $mw='100', $mh='100', $quality=90){

        // Fix path
        $filename = '../'.$img;

        // Check for file
        if(file_exists($filename))
        {           
            // Set a maximum height and width
            $width = $mw;
            $height = $mh;

            // Get new dimensions
            list($width_orig, $height_orig) = getimagesize($filename);

            $ratio_orig = $width_orig/$height_orig;

            if ($width/$height < $ratio_orig) {
                   $width = $height*$ratio_orig;
            }else{
                   $height = $width/$ratio_orig;
                }

            // Resample
            $image_p = imagecreatetruecolor($mw, $mh);
            $image = imagecreatefromjpeg($filename);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

            // Output
            imagejpeg($image_p, "../images/stories/catalog/{$preFix}{$id}.jpg", $quality);
        }
    }

This rescales the image to the larger size of the width and height and then crops it to the correct size.

// Crete an image forced to width and height
    function createFixedImage($img, $id=0, $preFix=false, $mw='100', $mh='100', $quality=90){

        // Fix path
        $filename = '../'.$img;

        // Check for file
        if(file_exists($filename))
        {           
            // Set a maximum height and width
            $width = $mw;
            $height = $mh;

            // Get new dimensions
            list($width_orig, $height_orig) = getimagesize($filename);

            $ratio_orig = $width_orig/$height_orig;

            if ($width/$height < $ratio_orig) {
                   $width = $height*$ratio_orig;
            }else{
                   $height = $width/$ratio_orig;
                }

            // Resample
            $image_p = imagecreatetruecolor($mw, $mh);
            $image = imagecreatefromjpeg($filename);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

            // Output
            imagejpeg($image_p, "../images/stories/catalog/{$preFix}{$id}.jpg", $quality);
        }
    }
似梦非梦 2024-07-16 06:56:07

您需要计算新的宽度和宽度。 高度以保持图像比例。 查看本页上的示例 2:

https://www.php.net/imagecopyresampled

You'll need to calculate the new width & height to keep the image proportionat. Check out example 2 on this page:

https://www.php.net/imagecopyresampled

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