包的选择和配置

想用RMagick,但据说内存泄露的问题比较厉害,作为替代品MiniMagick不存在内存泄露的问题。而二者都是使用ImageMagick的,所以需要下载并安装ImageMagick

下面安装ImageMagick:

sudo apt-get install imagemagick

安装gem··「mini_magick」

gem  install mini_magick --no-ri --no-rdoc

测试和使用mini_magick

引入gem MiniMagick :

require "mini_magick"

MiniMagick中的Image对象

MiniMagick::Image

from_file(file, ext = nil)
img=MiniMagick::Image.open("./pic1.jpg")

[](value)

A rather low-level way to interact with the “identify” command. No nice API here, just the crazy stuff you find in ImageMagick. See the examples listed!

@example

image["format"]      #=> "TIFF"
image["height"] #=> 41 (pixels)
image["width"] #=> 50 (pixels)
image["dimensions"] #=> [50, 41]
image["size"] #=> 2050 (bits)
image["original_at"] #=> 2005-02-23 23:17:24 +0000 (Read from Exif data)
image["EXIF:ExifVersion"] #=> "0220" (Can read anything from Exif)

@param format [String] A format for the “identify” command @see For reference seewww.imagemagick.org/script/command-line-options.php#format @return [String, Numeric, Array, Time, Object] Depends on the method called! Defaults to String for unknown commands

write(output_to)

Writes the temporary file out to either a file location (by passing in a String) or by passing in a Stream that you can write(chunk) to repeatedly

@param output_to [IOStream, String] Some kind of stream object that needs to be read or a file path as a String @return [IOStream, Boolean] If you pass in a file location [String] then you get a success boolean. If its a stream, you get it back. Writes the temporary image that we are using for processing to the output path

shave("宽x高")

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”
#!/usr/bin/ruby

require "mini_magick"

def  resize_and_crop(image, square_size)
if image[:width]<image[:height]
shave_off=((image[:height]-image[:width])/2).round
image.shave("0x#{shave_off}")
elsif image[:width]>image[:height]
shave_off=((image[:width]-image[:height])/2).round
end
geometry=to_geometry(square_size,square_size)
image.resize(geometry
return image
end new_image=resize_and_crop(MiniMagick::Image.from_file("1.jpg"))

「ruby/MiniMagick」用MiniMagick处理图片的更多相关文章

  1. 「Ruby && Sqlite3」How to install sqlite3 for ruby? (solve: sqlite-ruby no such file...)

    error message:           no such file .... 安装 gem install sqlite3-ruby -- --with-sqlite3-dir=/usr/lo ...

  2. 前端构建工具之gulp(一)「图片压缩」

    前端构建工具之gulp(一)「图片压缩」 已经很久没有写过博客了,现下终于事情少了,开始写博吧 今天网站要做一些优化:图片压缩,资源合并等 以前一直使用百度的FIS工具,但是FIS还没有提供图片压缩的 ...

  3. fir.im Weekly - 如何打造 Github 「爆款」开源项目

    最近 Android 转用 Swift 的传闻甚嚣尘上,Swift 的 Github 主页上已经有了一次 merge>>「Port to Android」,让我们对 Swift 的想象又多 ...

  4. 更新日志 - fir.im「高级统计」功能上线

    距离 2016 年到来只剩 10 个日夜,fir.im 也准备了一些新鲜的东西,比如「高级统计」功能和「跳转应用商店」功能,帮助你更好地管理.优化应用,欢迎大家试用反馈:) 新增高级统计功能 这次更新 ...

  5. Notepad++ 开启「切分窗口」同时检视、比对两份文件

    Notepad++ 是个相当好用的免费纯文本编辑器,除了内建的功能相当多之外,也支持外挂模块的方式扩充各方面的应用.以前我都用 UltraEdit 跟 Emeditor,后来都改用免费的 Notepa ...

  6. 「zigbee - 1」工欲善其事必先利其器 - IAR for 8051 IDE customization

    最近在实验室做一些 Zigbee 相关的事情,然而一直没在博客上记录啥东西,也不像原来在公司有动力在 Confluence wiki 上扯东扯西.直到前些阵子,跑到 feibit 论坛上(国内较大的一 ...

  7. 「C语言」文件的概念与简单数据流的读写函数

    写完「C语言」单链表/双向链表的建立/遍历/插入/删除 后,如何将内存中的链表信息及时的保存到文件中,又能够及时的从文件中读取出来进行处理,便需要用到”文件“的相关知识点进行文件的输入.输出. 其实, ...

  8. 「C语言」Windows+EclipseCDT下的C语言开发环境准备

    之前写过一篇 「C语言」在Windows平台搭建C语言开发环境的多种方式 ,讨论了如何在Windows下用DEV C++.EclipseCDT.VisualStudio.Sublime Test.Cl ...

  9. 如何对抗 WhatsApp「蓝色双勾」-- 3 个方法让你偷偷看讯息

    WhatsApp 强制推出新功能「蓝色双勾 (✔✔)」 ,让对方知道你已经看过讯息.一众用户反应极大,因为以后不能再藉口说未看到讯息而不回覆.究竟以后 WhatsApp 是否真的「更难用」? 幸好还有 ...

随机推荐

  1. [LeetCode]题解(python):089 Gray Code

    题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...

  2. nodejs技术面试问题整理

    1.meteor 是如何解决多回调的问题 参考 http://www.cnblogs.com/meteorcn/p/MeteorJS_Async_Fiber_Future_Wrap.html 2.一个 ...

  3. Maven集成Sonar

    Sonar对maven提供了简单可配的支持,要做的事情很简单--在maven/conf下settings.xml <profiles></profiles>标签之间添加如下内容 ...

  4. VS2013的virtualpath在当前应用程序根的外部

    将项目升级到VS2013环境时,运行报"virtualpath 在当前应用程序根的外部"错误,如下图 搜寻解决方法如下: 1.禁用VS2013 IDE的浏览链接视图功能 2.在We ...

  5. iOS: 悬浮的条件筛选下拉框的使用

    1.介绍 app中条件筛选视图是很常用的功能,一般它搭配着tableView的表头悬浮滚动使用,点击按钮时,就会弹出下拉框显示条件,选择一个条件后,下拉框自动隐藏. 2.效果图如下 从中间点击弹出,然 ...

  6. saltstack之(九)配置管理源码部署Nginx

    场景:rpm包安装的nginx服务,无法满足定制模块的需求,故线上环境使用nginx源码进行安装.本片文章详细介绍如何使用saltstack的配置管理功能实现nginx软件的源码安装. 下载源码:pc ...

  7. 打补丁patch 命令使用

    打补丁patch 命令使用 http://www.cnblogs.com/huanghuang/archive/2011/07/14/2106402.html patch 命令用于打补丁,补丁文件是使 ...

  8. centos python 2.7 安装

    一开始有这个需求,是因为用 YaH3C 替代 iNode 进行校园网认证时,一直编译错误,提示找不到 Python 的某个模块,百度了一下,此模块是在 Python2.7 以上才有的,但是系统的自带的 ...

  9. Python基础 (yield生成器)

    如果在一个函数中使用了yield,那么这个函数实际上生成的是一个生成器函数 ,返回的是一个generator object.生成器是实现迭代的一种方式 特点: 其实返回的就是可以的迭代对象 和迭代的方 ...

  10. makefile中引用其他makefile方法

    在Makefile中引用其他Makefile文件的方法是,使用inclue   filename.mk