streamio-magick 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +51 -0
- data/lib/magick/image.rb +50 -0
- data/lib/magick/version.rb +3 -0
- data/lib/streamio-magick.rb +4 -0
- metadata +88 -0
data/README.rdoc
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
= Streamio Magick
|
2
|
+
|
3
|
+
Simple yet powerful wrapper around imagemagick cli tools for reading metadata and transcoding images.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
(sudo) gem install streamio-magick
|
8
|
+
|
9
|
+
You also need to make sure to have imagemagick installed and that the commands identify and convert are available to the ruby process.
|
10
|
+
|
11
|
+
This version is written against an imagemagick build from august 2010. So no guaranteed compatability with much earlier (or much later) versions.
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
|
15
|
+
=== Require the gem
|
16
|
+
|
17
|
+
require 'rubygems'
|
18
|
+
require 'streamio-magick'
|
19
|
+
|
20
|
+
=== Reading Metadata
|
21
|
+
|
22
|
+
image = Magick::Image.new("path/to/image.png")
|
23
|
+
|
24
|
+
image.width # 640 (width of the image in pixels)
|
25
|
+
image.height # 480 (height of the image in pixels)
|
26
|
+
image.size # 455546 (filesize in bytes)
|
27
|
+
image.codec # "PNG"
|
28
|
+
|
29
|
+
image.valid? # true (would be false if identify fails to read the image)
|
30
|
+
|
31
|
+
=== Transcoding
|
32
|
+
|
33
|
+
First argument is the output file path.
|
34
|
+
|
35
|
+
image.transcode("image.jpg") # Transcode to jpg format
|
36
|
+
|
37
|
+
Give custom command line options for the convert command with a string.
|
38
|
+
|
39
|
+
image.transcode("image.jpg", "-resize 320x240")
|
40
|
+
|
41
|
+
The transcode function returns an Image object for the encoded file.
|
42
|
+
|
43
|
+
transcoded_image = image.transcode("image.jpg", "-resize 320x240")
|
44
|
+
|
45
|
+
transcoded_image.width # 320
|
46
|
+
transcoded_image.height # 240
|
47
|
+
transcoded_image.codec # "JPEG"
|
48
|
+
|
49
|
+
== Copyright
|
50
|
+
|
51
|
+
Copyright (c) 2010 Streamio Networks AB. See LICENSE for details.
|
data/lib/magick/image.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Output example:
|
2
|
+
# file.ext JPEG 1920x1200 1920x1200+0+0 8-bit DirectClass 1.955MB 0.000u 0:00.000
|
3
|
+
# Error example:
|
4
|
+
# identify: no decode delegate for this image format `file.ext' @ error/constitute.c/ReadImage/532.
|
5
|
+
|
6
|
+
class Magick::Image
|
7
|
+
attr_reader :width, :height, :codec, :path, :size
|
8
|
+
|
9
|
+
def initialize(path)
|
10
|
+
raise Errno::ENOENT, "the file '#{path}' does not exist" unless File.exists?(path)
|
11
|
+
|
12
|
+
@path = escape(path)
|
13
|
+
@size = File.size(path)
|
14
|
+
|
15
|
+
stdin, stdout, stderr = Open3.popen3("identify '#{path}'") # Output will land in stderr
|
16
|
+
|
17
|
+
out = stdout.read
|
18
|
+
err = stderr.read
|
19
|
+
|
20
|
+
@valid = out.length > 0
|
21
|
+
|
22
|
+
if valid?
|
23
|
+
filename, @codec, resolution, etc = out.split
|
24
|
+
|
25
|
+
@width = resolution.split("x").first.to_i
|
26
|
+
@height = resolution.split("x").last.to_i
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid?
|
31
|
+
@valid
|
32
|
+
end
|
33
|
+
|
34
|
+
def transcode(output_file, parameters = "")
|
35
|
+
stdin, stdout, stderr = Open3.popen3("convert #{path} #{escape(parameters)} #{output_file}")
|
36
|
+
|
37
|
+
out = stdout.read
|
38
|
+
err = stderr.read
|
39
|
+
|
40
|
+
raise err if err.length > 0
|
41
|
+
|
42
|
+
self.class.new(output_file)
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
46
|
+
def escape(path)
|
47
|
+
map = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"' }
|
48
|
+
path.gsub(/(\\|<\/|\r\n|[\n\r"])/) { map[$1] }
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: streamio-magick
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- David Backeus
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-26 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - "="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 27
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
version: 1.3.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Simple yet powerful wrapper around imagemagick cli tools for reading metadata and transcoding images.
|
38
|
+
email:
|
39
|
+
- david@streamio.se
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- lib/magick/image.rb
|
48
|
+
- lib/magick/version.rb
|
49
|
+
- lib/streamio-magick.rb
|
50
|
+
- README.rdoc
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/streamio/streamio-magick
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 23
|
75
|
+
segments:
|
76
|
+
- 1
|
77
|
+
- 3
|
78
|
+
- 6
|
79
|
+
version: 1.3.6
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.7
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Reads metadata and transcodes images.
|
87
|
+
test_files: []
|
88
|
+
|