【Ruby】fastimageインストール、gifの大きさを取得(ついでに容量も)
fastimageのインストール
rubyのfastimageで簡単に画像サイズを取得する方法 - Hive Color
$ gem install fastimage Fetching: addressable-2.3.6.gem (100%) Successfully installed addressable-2.3.6 Fetching: fastimage-1.6.3.gem (100%) Successfully installed fastimage-1.6.3 Parsing documentation for addressable-2.3.6 Installing ri documentation for addressable-2.3.6 Parsing documentation for fastimage-1.6.3 Installing ri documentation for fastimage-1.6.3 Done installing documentation for addressable, fastimage after 3 seconds 2 gems installed
gifのURL開いてファイルサイズを取得、容量も測る
# coding: utf-8 require 'open-uri' require 'fastimage' gif_path = "http://forgifs.com/gallery/d/221122-2/Truck-spanking-show-off.gif" # gifの幅と高さ取得 gifSize = FastImage.size(gif_path) puts gifSize #gifの容量を調べる open(gif_path) do |file| fileSize = File.size(file) if fileSize > 1750000 puts "size is over #{fileSize}" else puts "size is ok #{fileSize}" end end
250 # width 266 # height size is over 2085283
ローカルじゃなくてURLで指定できる点はいいなと。
RMagickでもサイズや容量を測ることはできるのでしょうけど、fastimageは速いですね。
ちなみに画像はこんなん。
これだけのためにわざわざDLしたとか・・・。