在 (PHP/GD) 中调整图像大小

发布于 2024-08-13 07:50:11 字数 338 浏览 5 评论 0原文

我正在寻求帮助/建议,以找到最有效的方法,使用 PHP/GD 将图像大小调整为尽可能小,同时保留原始图像的纵横比,但确保调整后的图像大于定义的最小宽度&高度。

例如,调整大小后的图像必须具有 宽度 >= 400 和高度 >= 300,但应尽可能接近这些尺寸,同时保持原始宽高比。

这样,“风景”图像的理想高度为 300 或稍大,宽度 >= 400,“肖像”图像的理想高度为 >宽度为 400 或稍大,高度 >= 300

I'm looking for help/suggestions in finding the most efficient way to resize an image to be as small as possible using PHP/GD while preserving the aspect ratio of the original image but ensuring the the resized image is bigger than a defined minimum width & height.

For example, the resized image must have a width >= 400 and a height >= 300 but should be as close to those dimensions as possible while maintaining the original aspect ratio.

Such that a "landscape" image would have an ideal height of 300 or slightly larger with a width >= 400 and a "portrait" image would have an ideal width of 400 or slightly larger with a height >= 300.

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

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

发布评论

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

评论(2

彩虹直至黑白 2024-08-20 07:50:11

我相信这就是您正在寻找的;具体来说,中间一列的图片:

当源图像更宽时
当源图像较高时
当宽高比相同时

以下代码源自 使用 ASP/PHP 裁剪以适合图像

list(
  $source_image_width,
  $source_image_height
) = getimagesize( '/path/to/image' );

$target_image_width  = 400;
$target_image_height = 300;

$source_aspect_ratio = $source_image_width / $source_image_height;
$target_aspect_ratio = $target_image_width / $target_image_height;

if ( $target_aspect_ratio > $source_aspect_ratio )
{
  // if target is wider compared to source then
  // we retain ideal width and constrain height
  $target_image_height = ( int ) ( $target_image_width / $source_aspect_ratio );
}
else
{
  // if target is taller (or has same aspect-ratio) compared to source then
  // we retain ideal height and constrain width
  $target_image_width = ( int ) ( $target_image_height * $source_aspect_ratio );
}

// from here, use GD library functions to resize the image

I believe this is what you're looking for; specifically, the pictures in the middle column:

When source image is wider
When source image is taller
When aspect ratios are same

Following code is derived from Crop-To-Fit an Image Using ASP/PHP:

list(
  $source_image_width,
  $source_image_height
) = getimagesize( '/path/to/image' );

$target_image_width  = 400;
$target_image_height = 300;

$source_aspect_ratio = $source_image_width / $source_image_height;
$target_aspect_ratio = $target_image_width / $target_image_height;

if ( $target_aspect_ratio > $source_aspect_ratio )
{
  // if target is wider compared to source then
  // we retain ideal width and constrain height
  $target_image_height = ( int ) ( $target_image_width / $source_aspect_ratio );
}
else
{
  // if target is taller (or has same aspect-ratio) compared to source then
  // we retain ideal height and constrain width
  $target_image_width = ( int ) ( $target_image_height * $source_aspect_ratio );
}

// from here, use GD library functions to resize the image
贵在坚持 2024-08-20 07:50:11

这似乎完成了工作......

    $data = @getimagesize($image);

    $o_w = $data[0];
    $o_h = $data[1];
    $m_w = 400;
    $m_h = 300;

    $c_w = $m_w / $o_w;
    $c_h = $m_h / $o_h;

    if($o_w == $o_h){
            $n_w = ($n_w >= $n_h)?$n_w:$n_h;
            $n_h = $n_w;
    } else {
            if( $c_w > $c_h ) {
                $n_w = $m_w;
                $n_h = $o_h * ($n_w/$o_w);
            } else {
                $n_h = $m_h;
                $n_w = $o_w * ($n_h/$o_h);
            }
    }

This seems to get the job done...

    $data = @getimagesize($image);

    $o_w = $data[0];
    $o_h = $data[1];
    $m_w = 400;
    $m_h = 300;

    $c_w = $m_w / $o_w;
    $c_h = $m_h / $o_h;

    if($o_w == $o_h){
            $n_w = ($n_w >= $n_h)?$n_w:$n_h;
            $n_h = $n_w;
    } else {
            if( $c_w > $c_h ) {
                $n_w = $m_w;
                $n_h = $o_h * ($n_w/$o_w);
            } else {
                $n_h = $m_h;
                $n_w = $o_w * ($n_h/$o_h);
            }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文