包的选择和配置

想用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. Ajax无刷新提交

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Android 开发-Intent传递普通字符串

    假设A传递id到B中 ActivityA: Intent intent=new Intent();    intent.setClass(ActivityA.this,ActivityB.class) ...

  3. Interview Return Products of All Other Elements in Array

    这是一道面试亚马逊时的题目,要求Time O(n). 我刚开始想的是算出所有的数的总product,再去除以对应位置的元素,但这种做法的问题是若该位置为0,就会报错. 到网上搜了下,才知道,原来有这种 ...

  4. ubuntu14.04安装Docker

    Ubuntu在14.02开始就已经集成了Docker,要安装很简单:   1 2 3 4 sudo apt-get update sudo apt-get install docker.io sudo ...

  5. Nginx负载均衡和LVS负载均衡的比较分析

    LVS和Nginx都可以用作多机负载的方案,它们各有优缺,在生产环境中需要好好分析实际情况并加以利用. 首先提醒,做技术切不可人云亦云,我云即你云:同时也不可太趋向保守,过于相信旧有方式而等别人来帮你 ...

  6. 【公开课】《奥威Power-BI基于微软示例库(MSSQL)快速制作管理驾驶舱》文字记录与反馈

        本期分享的内容: <奥威Power-BI基于微软示例库(MSSQL)快速制作管理驾驶舱> 时间:2016年11月02日 课程主讲人:叶锡文 从事商业智能行业,有丰富的实施经验,擅长 ...

  7. Intellij IDEA 创建控制台项目,断点调试

    在idea 2016中创建一个控制台项目(经常会忘) 打开创建界面 注意,什么都不要选,点击next(最坑的地方,经常忘) 再次点击next ============================= ...

  8. canvas学习之制作动画

    html部分 ...... <body> <canvas id="myCanvas" width="400" height="400 ...

  9. AWR--导出AWR数据

    SQL> create or replace directory expdp_dir as '/home/oracle/dump'; Directory created. SQL> @$O ...

  10. oc 正则图片<img /> 标签

    -(NSString *)getImageAttributeValue:(NSString *)content attributeKey:(NSString *)key { NSString *reg ...