新手问题 可以用 MiniMagick 和 carrierwave 可以实现填充图片背景色功能吗

QueXuQ · December 15, 2013 · Last by snull replied at July 27, 2015 · 2085 hits

例如我想要图片均为正方形为 400*400px,但是如果我上传的是 400*300 图片,就帮我把上下扩展为 400*400 的图片,并且增加的地方填充为白色的背景?可以实现的了的吗?

#1 楼 @yesmeck 呃,请问用什么方法呢?不好意思,不知道如何 google 这个问题呢。

process :resize_and_pad => [400, 400]

image = MiniMagick::Image.open(image_path) crop_params = "#{w}x#{h}+#{x}+#{y}" image.crop(crop_params) image.resize_and_pad '640x366' image.write(image_path) 这样写会报错!求指点应该怎么改写!

#3 楼 @yesmeck image = MiniMagick::Image.open(image_path) crop_params = "#{w}x#{h}+#{x}+#{y}" image.crop(crop_params) image.resize_and_pad '640x366' image.write(image_path)

#3 楼 @yesmeck 自己解决了 image.combine_options do |cmd| cmd.thumbnail "#{640}x#{366}>" cmd.background "#FFFFFF" cmd.gravity "center" cmd.extent "#{640}x#{366}" end

You need to Sign in before reply, if you don't have an account, please Sign up first.