最近blog换了皮肤,以前是最大宽度,现在宽度限制死了,有的图片超过了宽度,很丑陋,于是用minimagick对所有的图片统一缩放了下,这下图片的大小刚刚好,缩放比resize的效果要好,图片不会扭曲难看,
minimagick和rmagick都是调用imagemagick的ruby接口,使用起来很方便。。。。
1,缩放 (也就是我用来处理我blog里图片的脚本)
require 'rubygems' require 'mini_magick' # path = "E:/Rubyproject/blog.wxianfeng.com/public/files/" # windows路径 path = "/usr/local/system/www/blog.wxianfeng.com/shared/public/files/" files = Dir.open(path).to_a.select{|x| x != '.' && x!= '..' && x != '.svn' && x != 'Thumbs.db'} imgs = files.select { |f| f !~ /^(thumb_|middle_)/ } imgs.each do |ele| p ele img_path = path + ele img = MiniMagick::Image.from_file(img_path) w,h = img[:width],img[:height] percent = ((480/w.to_f) * 100).to_i img.combine_options do |c| c.sample "#{percent}%" # 缩放 end img.write(img_path) end
2,resize
image = MiniMagick::Image.from_file("input.jpg") # or MiniMagick::Image.new("input.jpg") image.resize "100x100" # or image.thumbnail "100x100" image.write("output.jpg")
3,裁剪
require ‘mini_magick’ img = MiniMagick::Image.from_file “1.jpg” #取得宽度和高度 w,h = img[:width],img[:height] #=> [2048, 1536] shaved_off = ((w-h)/2).round #=> 256 img.shave “#{shaved_off}x0″ #此处表示宽度上左右各截取256个像素,高度上截取0像素 img.write “2.jpg”
4,旋转
image = MiniMagick::Image.from_file("input.jpg") image.combine_options do |c| c.rotate "-90>" # 旋转 90 度 end image.write("input.jpg") # 同名 替换掉原来的
SEE:
http://github.com/probablycorey/mini_magick
http://www.blogkid.net/archives/2154.html