使用 imagemagick 的格式字符串和宽度/高度计算新图像大小

发布于 2024-12-08 14:15:21 字数 600 浏览 4 评论 0原文

在 Web 应用程序中,最好使用 img 标签的宽度和高度属性 - 这允许浏览器更快地渲染页面。

如果我有图像的宽度和高度以及 imagemagick 格式字符串(即“300>”),如何获得该图像将调整到的大小?我希望 imagemagick 有一些钩子/函数可以让我访问这些信息。

我不想调整图像大小或将图像加载到内存中,我只想知道尺寸是多少。

本质上,我想要以下函数:

def get_image_size(width, height, format_string)
  # ... magic here
  return [new_width, new_height]
end

width, height = get_image_size(300, 400, '235x235!')
# would return 235 and 235

width, height = get_image_size(300, 400, '700x700>')
# would return 300 and 400

width, height = get_image_size(300, 400, '100x100')
# would return 75 and 100

In web apps, it's good practice to use the width and height attributes for img tags -- this allows the browser to render the page faster.

If I have the width and height of an image, and a imagemagick format string (i.e. '300>'), how can I get the size that that image would be resized to? I'm hoping that imagemagick has some hooks / functions that would allow me to get access to this information.

I don't want to resize or load the image into memory, I just want to know what the dimensions would be.

Essentially, I want the following function:

def get_image_size(width, height, format_string)
  # ... magic here
  return [new_width, new_height]
end

width, height = get_image_size(300, 400, '235x235!')
# would return 235 and 235

width, height = get_image_size(300, 400, '700x700>')
# would return 300 and 400

width, height = get_image_size(300, 400, '100x100')
# would return 75 and 100

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

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

发布评论

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

评论(1

怎会甘心 2024-12-15 14:15:21

使用下面的 ImageMagick convert 命令可以得到你想要的。这应该可以轻松转换为您选择的任何 ImageMagick 语言 API

$ convert -size 300x400 xc:white -resize '235x235!' info:- | cut -d\  -f3
235x235
$ convert -size 300x400 xc:white -resize '700x700>' info:- | cut -d\  -f3
300x400
$ convert -size 300x400 xc:white -resize '100x100' info:- | cut -d\  -f3
75x100

Using the following ImageMagick convert command gives what you want. This should easily translate to whichever ImageMagick language API you choose.

$ convert -size 300x400 xc:white -resize '235x235!' info:- | cut -d\  -f3
235x235
$ convert -size 300x400 xc:white -resize '700x700>' info:- | cut -d\  -f3
300x400
$ convert -size 300x400 xc:white -resize '100x100' info:- | cut -d\  -f3
75x100
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文