代码之家  ›  专栏  ›  技术社区  ›  Fitzchak Yitzchaki

使用ImageMagick检测图片方向

  •  0
  • Fitzchak Yitzchaki  · 技术社区  · 15 年前

    我想使用一个参数将图片大小调整为新的大小:宽度。

    如果图片是水平的,新的大小将是:宽度=宽度,高度=与宽度成比例。

    如果图片是垂直的,新的大小将是:高度=宽度,宽度=高度成比例。

    知道如何实现这个吗?

    我用的是带有magicknet包装的imagemagick。

    2 回复  |  直到 11 年前
        1
  •  2
  •   o.k.w    15 年前

    从处的用法参考 http://www.imagemagick.org/Usage/resize/

    convert org.jpg    -resize widthxwidth  final.jpg
    

    例如,宽x宽可以是256x256

    纵横比将保持不变,并在256 x 256像素平方的边界内调整大小。

    引自上页:

    调整大小将使图像适合 请求的大小。它不填充 要求的盒子尺寸。

        2
  •  1
  •   nickf    15 年前

    我不太清楚你在这里的意思。你说你只想定义宽度,但是在“垂直”的情况下,你把高度设置为宽度?无论如何,如果您只想使用宽度来调整某些内容的大小,请使用以下伪代码:

    ratio = width / height
    newWidth = <the new width>
    newHeight = newWidth / ratio
    

    如果要将最长大小调整为给定值,请尝试以下操作:

    ratio = width / height
    
    if ratio > 1   // wider than it is tall
        newWidth = <theValue>
        newHeight = newWidth / ratio
    
    else           // taller than it is wide
        newHeight = <theValue>
        newWidth = newHeight * ratio
    
    推荐文章