Ruby on rails 调整大小时,取图像的中心部分

Ruby on rails 调整大小时,取图像的中心部分,ruby-on-rails,ruby-on-rails-4,imagemagick,minimagick,Ruby On Rails,Ruby On Rails 4,Imagemagick,Minimagick,我在使用Carrierwave MiniMagick ImageMagick调整图像大小时遇到问题 我编写了自定义的调整大小方法,因为我需要将两个图像合并在一起并对它们进行一些处理,所以MiniMagick的标准process方法是不够的。问题在于调整大小的方法。我需要取图像的中心部分,但它会返回顶部 def merge manipulate! do |img| img.resize '180x140^' # problem is here ... img e

我在使用Carrierwave MiniMagick ImageMagick调整图像大小时遇到问题

我编写了自定义的调整大小方法,因为我需要将两个图像合并在一起并对它们进行一些处理,所以MiniMagick的标准
process
方法是不够的。问题在于调整大小的方法。我需要取图像的中心部分,但它会返回顶部

def merge
  manipulate! do |img|
    img.resize '180x140^' # problem is here

    ...

    img
  end
end


谢谢你的帮助

您应该使用
裁剪
而不是
调整大小

请在此处查看crop ImageMagick crop命令的说明:


MiniMagick只是ImageMagick的一个包装,所以所有参数都是相同的。

您应该使用
crop
而不是
resize

请在此处查看crop ImageMagick crop命令的说明:


MiniMagick只是ImageMagick的一个包装,所以所有参数都是相同的。

您应该使用
crop
而不是
resize

请在此处查看crop ImageMagick crop命令的说明:


MiniMagick只是ImageMagick的一个包装,所以所有参数都是相同的。

您应该使用
crop
而不是
resize

请在此处查看crop ImageMagick crop命令的说明:


MiniMagick只是ImageMagick的一个包装,所以所有参数都是相同的。

我将按如下方式处理此问题:

  • 将图像大小调整为180x180正方形
  • 从顶部拆下(180-140)/2
  • 从底部拆下(180-140)/2
像这样的东西应该可以做到:

def merge
  manipulate! do |img|
    img.resize '180x180' # resize to 180px square
    img.shave '0x20' # Removes 20px from top and bottom edges

    img # Returned image should be 180x140, cropped from the centre
  end
end
当然,这假设您的输入图像始终是正方形。如果它不是方形的,并且你的心脏设定在180x140的比例上,你可以这样做:

def merge
  manipulate! do |img|
    if img[:width] <= img[:height]
      # Image is tall ...
      img.resize '180' # resize to 180px wide
      pixels_to_remove = ((img[:height] - 140)/2).round # calculate amount to remove
      img.shave "0x#{pixels_to_remove}" # shave off the top and bottom
    else
      # Image is wide
      img.resize 'x140' # resize to 140px high
      pixels_to_remove = ((img[:width] - 180)/2).round # calculate amount to remove
      img.shave "#{pixels_to_remove}x0" # shave off the sides
    end

    img # Returned image should be 180x140, cropped from the centre
  end
end
def合并
操纵do | img|

如果img[:width]I将按如下方式进行处理:

  • 将图像大小调整为180x180正方形
  • 从顶部拆下(180-140)/2
  • 从底部拆下(180-140)/2
像这样的东西应该可以做到:

def merge
  manipulate! do |img|
    img.resize '180x180' # resize to 180px square
    img.shave '0x20' # Removes 20px from top and bottom edges

    img # Returned image should be 180x140, cropped from the centre
  end
end
当然,这假设您的输入图像始终是正方形。如果它不是方形的,并且你的心脏设定在180x140的比例上,你可以这样做:

def merge
  manipulate! do |img|
    if img[:width] <= img[:height]
      # Image is tall ...
      img.resize '180' # resize to 180px wide
      pixels_to_remove = ((img[:height] - 140)/2).round # calculate amount to remove
      img.shave "0x#{pixels_to_remove}" # shave off the top and bottom
    else
      # Image is wide
      img.resize 'x140' # resize to 140px high
      pixels_to_remove = ((img[:width] - 180)/2).round # calculate amount to remove
      img.shave "#{pixels_to_remove}x0" # shave off the sides
    end

    img # Returned image should be 180x140, cropped from the centre
  end
end
def合并
操纵do | img|

如果img[:width]I将按如下方式进行处理:

  • 将图像大小调整为180x180正方形
  • 从顶部拆下(180-140)/2
  • 从底部拆下(180-140)/2
像这样的东西应该可以做到:

def merge
  manipulate! do |img|
    img.resize '180x180' # resize to 180px square
    img.shave '0x20' # Removes 20px from top and bottom edges

    img # Returned image should be 180x140, cropped from the centre
  end
end
当然,这假设您的输入图像始终是正方形。如果它不是方形的,并且你的心脏设定在180x140的比例上,你可以这样做:

def merge
  manipulate! do |img|
    if img[:width] <= img[:height]
      # Image is tall ...
      img.resize '180' # resize to 180px wide
      pixels_to_remove = ((img[:height] - 140)/2).round # calculate amount to remove
      img.shave "0x#{pixels_to_remove}" # shave off the top and bottom
    else
      # Image is wide
      img.resize 'x140' # resize to 140px high
      pixels_to_remove = ((img[:width] - 180)/2).round # calculate amount to remove
      img.shave "#{pixels_to_remove}x0" # shave off the sides
    end

    img # Returned image should be 180x140, cropped from the centre
  end
end
def合并
操纵do | img|

如果img[:width]I将按如下方式进行处理:

  • 将图像大小调整为180x180正方形
  • 从顶部拆下(180-140)/2
  • 从底部拆下(180-140)/2
像这样的东西应该可以做到:

def merge
  manipulate! do |img|
    img.resize '180x180' # resize to 180px square
    img.shave '0x20' # Removes 20px from top and bottom edges

    img # Returned image should be 180x140, cropped from the centre
  end
end
当然,这假设您的输入图像始终是正方形。如果它不是方形的,并且你的心脏设定在180x140的比例上,你可以这样做:

def merge
  manipulate! do |img|
    if img[:width] <= img[:height]
      # Image is tall ...
      img.resize '180' # resize to 180px wide
      pixels_to_remove = ((img[:height] - 140)/2).round # calculate amount to remove
      img.shave "0x#{pixels_to_remove}" # shave off the top and bottom
    else
      # Image is wide
      img.resize 'x140' # resize to 140px high
      pixels_to_remove = ((img[:width] - 180)/2).round # calculate amount to remove
      img.shave "#{pixels_to_remove}x0" # shave off the sides
    end

    img # Returned image should be 180x140, cropped from the centre
  end
end
def合并
操纵do | img|
如果img[:width]这就是:

调整图像大小以适应指定尺寸,同时保留原始图像的纵横比。如有必要,请以较大尺寸裁剪图像

例如:

image = ImageList.new(Rails.root.join('app/assets/images/image.jpg'))
thumb = image.resize_to_fill(1200, 630)
thumb.write('thumb.jpg')
该方法采用第三个参数,即重力,但默认情况下它是
中心重力。

这就是它的作用:

调整图像大小以适应指定尺寸,同时保留原始图像的纵横比。如有必要,请以较大尺寸裁剪图像

例如:

image = ImageList.new(Rails.root.join('app/assets/images/image.jpg'))
thumb = image.resize_to_fill(1200, 630)
thumb.write('thumb.jpg')
该方法采用第三个参数,即重力,但默认情况下它是
中心重力。

这就是它的作用:

调整图像大小以适应指定尺寸,同时保留原始图像的纵横比。如有必要,请以较大尺寸裁剪图像

例如:

image = ImageList.new(Rails.root.join('app/assets/images/image.jpg'))
thumb = image.resize_to_fill(1200, 630)
thumb.write('thumb.jpg')
该方法采用第三个参数,即重力,但默认情况下它是
中心重力。

这就是它的作用:

调整图像大小以适应指定尺寸,同时保留原始图像的纵横比。如有必要,请以较大尺寸裁剪图像

例如:

image = ImageList.new(Rails.root.join('app/assets/images/image.jpg'))
thumb = image.resize_to_fill(1200, 630)
thumb.write('thumb.jpg')

该方法采用第三个参数,即重力,但默认情况下它是
CenterGravity

不幸的是,它们并不总是正方形。我想我需要在这里使用
gravity
方法和resize方法,但我不知道如何将其作为参数传递。更新了我的答案以处理任何输入图像比率/sizeGreat,它可以工作。奇怪的是,仅仅用一两种方法没有更简单的方法。我认为,这是一种非常流行的调整大小的方法。如果您首先计算所需的偏移量,您可以使用一个
crop
命令进行调整。。。然而,我认为这个解决方案在数学上比这个更复杂。这感觉更容易通过编程来支持。不幸的是,它们并不总是方形的。我想我需要在这里使用
gravity
方法和resize方法,但我不知道如何将其作为参数传递。更新了我的答案以处理任何输入图像比率/sizeGreat,它可以工作。奇怪的是,仅仅用一两种方法没有更简单的方法。我认为,这是一种非常流行的调整大小的方法。如果您首先计算所需的偏移量,您可以使用一个
crop
命令进行调整。。。然而,我认为这个解决方案在数学上比这个更复杂。这感觉更容易通过编程来支持。不幸的是,它们并不总是方形的。我想我需要在这里使用
gravity
方法和resize方法,但我不知道如何将其作为参数传递。更新了我的答案以处理任何输入图像比率/sizeGreat,它可以工作。奇怪的是,仅仅用一两种方法没有更简单的方法。我认为