使用 imagemagick 的格式字符串和宽度/高度计算新图像大小
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用下面的 ImageMagick
convert
命令可以得到你想要的。这应该可以轻松转换为您选择的任何 ImageMagick 语言 API。Using the following ImageMagick
convert
command gives what you want. This should easily translate to whichever ImageMagick language API you choose.