根据长宽比调整图像大小php

发布于 2024-11-08 03:00:59 字数 152 浏览 0 评论 0原文

我想要图像需要调整大小的新高度和宽度。有两个条件:

  1. 宽度需要在 180px(170-180) 左右,但 <; 180px(上传的图像始终> 180)
  2. 高度应最大为180px(上传的图像可能会或可能不会> 180)

I want the new height and width to which the image needs to be resized. There are two conditions

  1. Width needs to be around 180px(170-180) but < 180px (uploaded image is always > 180)
  2. Height should be max 180px (uploaded image may or may not be > 180)

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

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

发布评论

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

评论(2

只有影子陪我不离不弃 2024-11-15 03:00:59

如果您正在为 Linux 编写程序,我建议使用 ImageMagick。与任何基于 PHP 的方法相比,它的内存效率更高,并且可能更快。几乎所有服务器都安装了它。下面的代码就可以解决问题。

function resizeTo($source, $dest, $w=180, $h=180) {
    system("convert $source -resize {$w}x{$h} $dest");
}

它会介意纵横比。

编辑:

抱歉造成混乱。我认为以下内容应该满足您的需求。它未经测试,可能需要一点调试,如果您遇到麻烦,我可以尝试再次发布。

//accepts and returns point object (having ->x and ->y)
function resizeTo($current, $max) {
   if($current->x <= $max->x && $current->y <= $max->y) //you will not need this but
       return $current;                                 // still its good to have

   if( ($current->y / $max->y) > ($current->x / $max->x) ) { //y axis needs more trimming
       $r=$current->y / $max->y;
       $current->y = $max->y;
       $current->x = $current->x / $r;
   } else {
       $r=$current->x / $max->x;
       $current->x = $max->x;
       $current->y = $current->y / $r;
   }

   return $current;
}

If you are writing program for Linux I would recommend using ImageMagick. It is more memory efficient and probably faster than any PHP based method. Almost all servers have it installed. Following code will do the trick.

function resizeTo($source, $dest, $w=180, $h=180) {
    system("convert $source -resize {$w}x{$h} $dest");
}

It will mind the aspect ratio.

Edit:

Sorry for the confusion. I think the following should do what you are looking for. It is not tested and might need a little bit debugging, if you run into trouble I can try and post again.

//accepts and returns point object (having ->x and ->y)
function resizeTo($current, $max) {
   if($current->x <= $max->x && $current->y <= $max->y) //you will not need this but
       return $current;                                 // still its good to have

   if( ($current->y / $max->y) > ($current->x / $max->x) ) { //y axis needs more trimming
       $r=$current->y / $max->y;
       $current->y = $max->y;
       $current->x = $current->x / $r;
   } else {
       $r=$current->x / $max->x;
       $current->x = $max->x;
       $current->y = $current->y / $r;
   }

   return $current;
}
羁客 2024-11-15 03:00:59

您只需要几个步骤:

1. scale = imageWidth / 180;
2. scale = (imageHeight/scale>180) ? imageHeight/180 : scale;

第一个将设置宽度为 180 所需的比例因子(根据您的评论,它总是大于 180)

第二个将检查高度是否大于 180规模。如果是,则比例将为高度/180。如果不是,则您已经达到了最大高度。

然后你还需要步骤来获取实际的宽度和高度:

width = imageWidth/scale;
height = imageHeight/scale;

考虑到你想让 imageWidth 在 170 和 180 之间,我想裁剪图像也是一种可能。如果是这种情况,您需要进行额外检查

if (width<170) {
  width = 170;
  height = imageHeigh / (imageWidth/170);
  //resize image to width and height
  //crop image to height = 180
}

You just need a few steps:

1. scale = imageWidth / 180;
2. scale = (imageHeight/scale>180) ? imageHeight/180 : scale;

The first one will set the scale factor you need to make the width 180 (based on your comment that it is ALWAYS larger then 180)

The second one will check if the height will be larger then 180 with that scale. If it is, then the scale will be height/180. If its not, you already have the max height.

Then you also need steps to get the actual width and height:

width = imageWidth/scale;
height = imageHeight/scale;

Considering you want to make the imageWidth between 170 and 180 I guess cropping the image is also a possibility. If that is the case you need an additional check

if (width<170) {
  width = 170;
  height = imageHeigh / (imageWidth/170);
  //resize image to width and height
  //crop image to height = 180
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文