包的选择和配置

想用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/rubyrequire "mini_magick"def  resize_and_crop(image, square_size) if image[:width]<image[:height]shave_off=((image[:height]-image[:width])/2).roundimage.shave("0x#{shave_off}")elsif image[:width]>image[:height]shave_off=((image[:width]-image[:height])/2).roundendgeometry=to_geometry(square_size,square_size)image.resize(geometryreturn image
endnew_image=resize_and_crop(MiniMagick::Image.from_file("1.jpg"))

转载于:https://www.cnblogs.com/lizunicon/p/3674605.html

「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. TIOBE 1月编程语言排行榜:C语言再度「C 位」出道,Python惜败

    整理 | 屠敏 来源 | CSDN(ID:CSDNnews) [导读]在 2020 年初雪来临之际,TIOBE 官方在最新发布的 1 月编程语言榜单中为我们最终揭开了「 2019 年度编程语言」的神秘 ...

  3. 编程语言发展70年,用50种不同语言输出「Hello World」

    「免费学习 60+ 节公开课,投票页面,点击讲师头像」 作者 | Sylvain Saurel 译者 | 风车云马 责编 | 屠敏 [导读]历经 70 年,不断出现的编程语言为开发者解决了哪些难题?其 ...

  4. 「代码家」的学习过程和学习经验分享【转】

    图灵丛书的一句话说的很好,Standing on the shoulders of giants,是的,我们一直站在巨人的肩上,我们起步都在沿着他们的轨迹前行,之后慢慢的在前人的开发基础或者规范上写出 ...

  5. rust主播排行_「Rust日报」2019每周精选 • 第四期

    前言: 从2018年开始,我每天会花1个小时关注Rust社区动态,并且在Rust.CC论坛.tg channel.Steemit.GitHub.语雀订阅都开通了Rust日报,分享我每天的见闻,偶尔也夹 ...

  6. macOS 的「预览」有几种用法

    如果要评选 macOS 最低调的 App ,「预览」可能就会位列其中之一.「预览」和 macOS 结合异常紧密,甚至让人都会遗忘它还是个 App.毕竟大多数的时候,提到「预览」就会想到操作系统的一部分 ...

  7. 拒绝「技术栈」选择恐惧症

    所谓最小化可行产品(Minimum Viable Product,MVP),就是将产品快速推向客户,从客户反馈中不断进行迭代.更重要的是,MVP 也是研发团队进一步完善产品的基础. 但是,在正式代码之 ...

  8. 转: 拒绝「技术栈」选择恐惧症

    所谓最小化可行产品(Minimum Viable Product,MVP),就是将产品快速推向客户,从客户反馈中不断进行迭代.更重要的是,MVP 也是研发团队进一步完善产品的基础. 但是,在正式代码之 ...

  9. 「第一章」安全世界观

    批注[--] 表示他人.自己.网络批注参考资料来源于* 书中批注* CSDN* GitHub* Google* 维基百科* YouTube* MDN Web Docs由于编写过程中无法记录所有的URL ...

最新文章

  1. Linux 运维常用命令 find、awk、sed、grep、vi、ps、lsof、rpm
  2. 用Perl发送邮件小例子
  3. 管理和配置客户端访问服务器
  4. python36中文手册_python_36_文件操作4
  5. 操作系统习题3—进程的互斥与同步
  6. 英语语法---形容词短语详解
  7. Java19-day10【标准输入输出流、字节字符打印流、对象序列化-反序列化流、serialVersionUIDtransient、Properties】
  8. iPhone 越狱开发相关
  9. 视频加速方案的最优解 - Xilinx硬件加速技术专场(深圳站)
  10. Java中lombok @Builder注解使用详解
  11. 架构设计-数据访问层简述
  12. 前端重构实践(一) —— 性能优化
  13. php生成一个500错误_Hyperf 发布 v2.0.1 版本 | 企业级的 PHP 微服务云原生协程框架...
  14. 最近升级了win10系统,以前一直用的RDPWrap-1.6版本无法使用,解决方案
  15. GlobalMapper小白学习日记
  16. 网页登陆界面,如何测试?
  17. 使用window小娜实现文本转语音
  18. 一般纳税人什么意思(为什么一般纳税人认定500万)
  19. 印象笔记:网页版与mac客户端工具栏都不一样
  20. win7使用U盘重装系统

热门文章

  1. 算法小结——qsort函数
  2. 007 - VUE + hexo blog
  3. 数据库索引的使用及优化
  4. faker假数据php,Faker首页、文档和下载 - 生成假数据的 PHP 库
  5. python怎样切换到上一行_如何返回到python中的前一行?
  6. OJ、ACM提交常见错误类型
  7. 一加10 Pro的性能配置还是非常不错的
  8. 公司没有与员工签订劳动合同,也没有给员工购买社保,现在员工被公司解雇,该如何要求赔偿?
  9. 微信朋友圈内容能删除吗?
  10. 赤小豆和红小豆有什么区别,哪种和薏米煮粥更好些?