包的选择和配置

想用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. QRadioButton

    #include "dialog.h" #include "ui_dialog.h" #include <QtCore> #include < ...

  2. 怎么清除file控件的文件路径

    还记得上次做一个文件上传,后来测试告诉我说,如果我要是不选择文件了呢?该怎么办?我说:简单啊,做一个取消按钮不就完事了吗!然后我就想一个file空间做一个取消是多么简单的事,用js处理可是想怎么样就怎 ...

  3. A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list.

    图解: 此题过程分为三个阶段,分别是 1.负责后面一个节点,并且将这个节点插入到原来链表中  2.复制后面一个节点的random指针. 3 拆分组合链表为两部分. 第一部分代码: while(curr ...

  4. 详解OpenGL中的各种变换(投影变换,模型变换,视图变换)(完)——法线变换

    前面两节内容已经说完了所有的三种变换.也就是说我们现在程序里面既不需要glLookAt(),也不需要gluPerspective(),这些矩阵我们都可以自己写.然后,再用glMultMatrix()来 ...

  5. Android shape的使用(圆角矩形)

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  6. 关于OC语法的公开和私有的讨论

    关于OC语法的公开和私有的讨论 OC语法中,类的.h文件向外面暴露类的功能/方法接口,其中定义的属性/方法/协议/类别/类扩展都属于公开的,但实例变量要看限定词(@protected/@public/ ...

  7. Android --通知栏Notification

    参考博客:Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它) //创建一个通知栏的Builder构造类 (Create a Notification Bui ...

  8. API文档的阅读

    API ——Application Programming Interface(应用程序编程接口) API是应用程序接口的意思,API是Java提供的基本编程接口,当使用Java语言进行编程时,不可能 ...

  9. shell中引用其他脚本的方法

    在Shell中引用其他脚本的方法是source   filename.sh 或    .    filename.sh 注意:      .   和   filename.sh之间必须有空格

  10. SQLSERVER:大容量导入数据时保留标识值 (SQL Server)

    从MSDN上看到实现大容量导入数据时保留标识值得方法包含三种: MSDN链接地址为:https://msdn.microsoft.com/zh-cn/library/ms178129.aspx 感觉M ...