有没有办法动态定义图像在页面或特定布局中的显示方式?

发布于 2024-09-01 10:45:02 字数 358 浏览 1 评论 0原文

长期以来,我在管理高度或宽度异常长的图像时遇到了问题。

如果我固定它们的高度和宽度,它们会显得拉长吗?

如果我固定它们的宽度,并且图像的高度很长,那么它也会弄乱整个网站。

如果我固定它们的高度,并且图像的宽度很长,那么它也会弄乱整个网站。

我保存在本地驱动器中的图像是否保持比例保存? 假设用户决定上传图像 1(高度)* 32(宽度)。 当他上传这张图片时,脚本将用户上传的图片大小调整为高度:1000px(只是一个例子),

所以生成的图片为1000px(高度)* 32000(宽度),你看到现在图像异常大。

现在,当在 1000px * 1000px 的框中显示此图像时,显示此图像的最佳方式是什么?

For so many time, I have encountered problems with managing image having abnormally long height or width.

If I fixed their height and widht, they will appear streched?

If I fixed their width, and if the height of the image is very long then also it will mess up the overall website.

If I fixed their height, and if the width of the image is very long then also it will mess up the overall website.

The images I save in the local drive are saved maintaining the ratio?
Let say user decides to upload image 1(height)*32(width).
When he uploads this image, the script is made to resize the user uploaded image to height:1000px(just an example)

So the resulting image in 1000px(height)*32000(widht), you see now the image is abnormally large.

Now while displaying this image in a box of 1000px * 1000px, what is the best way to display this image?

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

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

发布评论

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

评论(5

爱她像谁 2024-09-08 10:45:02

如果您尝试调整 32x1 图像的大小以使其适合 1000x1000 的框,则不应将高度调整为 1000。相反,您应该将宽度调整为 1000,将高度调整为 1000/32。

一些示例代码可以是:

define(MAX_WIDTH, 1000);
define(MAX_HEIGHT, 1000);

if ($cur_width / MAX_WIDTH > $cur_height / MAX_HEIGHT)
{
   $new_width = MAX_WIDTH;
   $new_height = (int) ($cur_height * MAX_WIDTH / $cur_width);
}
else
{
   $new_width = (int) ($cur_width * MAX_HEIGHT / $cur_height);
   $new_height = MAX_HEIGHT;
}

这检查哪个尺寸相对于盒子的尺寸“更大”,并适当地设置 $new_width 和 $new_height 。确保处理其中一个维度四舍五入为 0 的奇怪情况(即 10000x1 图像可能最终为 1000x0)。

If you're trying to resize the 32x1 image so it fits within a 1000x1000 box, you shouldn't be resizing the height to be 1000. Instead, you should resize the width to 1000 and the height to 1000/32.

Some example code could be:

define(MAX_WIDTH, 1000);
define(MAX_HEIGHT, 1000);

if ($cur_width / MAX_WIDTH > $cur_height / MAX_HEIGHT)
{
   $new_width = MAX_WIDTH;
   $new_height = (int) ($cur_height * MAX_WIDTH / $cur_width);
}
else
{
   $new_width = (int) ($cur_width * MAX_HEIGHT / $cur_height);
   $new_height = MAX_HEIGHT;
}

This checks which of the dimensions is "bigger" relative to the dimensions of the box and sets $new_width and $new_height appropriately. Make sure to handle weird cases where one of the dimensions gets rounded down to 0 (ie. a 10000x1 image would probably end up as 1000x0).

别忘他 2024-09-08 10:45:02

如果我理解正确的话你想保持这个比例。那么为什么不使用一些图像库来缩放图像的高度或宽度并保持比例。

If I understand correctly you want to keep the ratio. So why don't you use some image library to scale the image on either height or width and keep ratio.

黑凤梨 2024-09-08 10:45:02

如果我固定它们的高度和宽度,它们会显得拉伸吗?

如果改变比例,它们就会扭曲;如果改变大小,它们就会拉伸。

如果我固定它们的宽度,并且图像的高度很长,那么它也会弄乱整个网站。

图像将缩放,保持其原始 xy 比例。这是否“搞乱网站”取决于上下文。

如果我固定它们的高度,并且如果图像的宽度很长,那么它也会弄乱整个网站。

同上

解决这个问题的最佳方法是什么?

这取决于许多因素,其中最重要的是图像的目的是什么。是的,我们又回到了上下文。

如果我对您想要实现的目标做出一些假设,您基本上有两个选择:

  • 裁剪图像(可能在缩放后)
  • 计算图像的 xy 比率,将其与您想要放置的空间的 xy 比率进行比较在中,使用它来决定将其缩放(同时保持纵横比)到空间的宽度或空间的高度。

If I fixed their height and widht, they will appear streched?

They'll be distorted if you change the ratio, and stretched if you change the size.

If I fixed their width, and if the height of the image is very long then also it will mess up the overall website.

The image will scale, keeping its original x-y ratio. Whether or not this "messes up the website" depends on the context.

If I fixed their height, and if the width of the image is very long then also it will mess up the overall website.

Ditto

How is the best way to fix this?

That depends on a number of factors, not least of which is what the purpose of the images is. Yes, we're back to context again.

If I make some assumptions about what you are trying to achieve, you essentially have two options:

  • Crop the image (possibly after scaling it)
  • Calculate the x-y ratio of the image, compare it to the x-y ratio of the space you want to put it in, use that to make a decision about scaling it (while maintaining aspect ratio) to the width of the space or the height of the space.
月隐月明月朦胧 2024-09-08 10:45:02

尝试将输出图像适合(保持纵横比)在特定的矩形中怎么样?

What about trying to fit (keeping the aspect ratio) the output image in a specific rectangle?

轻拂→两袖风尘 2024-09-08 10:45:02

您可以将 divoverflow:hidden; 一起使用。

You can use a div with overflow: hidden;.

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