mini_magick 4.12.0 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 20aea50f08b4b5c234dc8167e3b8f344b3bb5e279e4d28857ce38d2acbdc0772
4
- data.tar.gz: 513048f1e3315516fe592da1ecb3dec41f8e7a55e8165fe37db0720a297c5daa
3
+ metadata.gz: da1546dc65a1492ca64c0747715e0e6b9b3b641541beac9edd6689a2e9544b8b
4
+ data.tar.gz: b03c897214f43f4d106d90b7fcea2d90ae1b9395afbefcf3810fd2af21fe5095
5
5
  SHA512:
6
- metadata.gz: b5e8d72969c3649b4514a5b53fb5d228fb5db58bd9bdef0b7457838ed1289e62c8a6c0ec28cda9463fc16a159724d024a23043de5a377c2824f5bc9b148a627d
7
- data.tar.gz: 28e78716378e97ba8c57758cb684b0428d734c1e5163f626a53b38f52c8f133db7fe0f041727c3a5ee200d241cdbd9409986caacc49746d361905c76a4bfed38
6
+ metadata.gz: 666e1178b7a89dedcae4de7220c1a41eb812f8a5a1b14143f669a1337deb26271a94ff7cc6698258617b2584dc93129e021b763365039939d83b7b85b47aa8f5
7
+ data.tar.gz: 6db3e4657d4e1db3c368b435ef256ba33becf7b164bea29154faeb209bd168f7c370b2ec915920c2ef1e1f61fde0da43b1753cacc6b829a9365039364b262902
data/README.md ADDED
@@ -0,0 +1,558 @@
1
+ # MiniMagick
2
+ [![Gem Version](https://img.shields.io/gem/v/mini_magick.svg)](http://rubygems.org/gems/mini_magick)
3
+ [![Gem Downloads](https://img.shields.io/gem/dt/mini_magick.svg)](http://rubygems.org/gems/mini_magick)
4
+ [![CI](https://github.com/minimagick/minimagick/actions/workflows/ci.yml/badge.svg)](https://github.com/minimagick/minimagick/actions/workflows/ci.yml)
5
+ [![Code Climate](https://codeclimate.com/github/minimagick/minimagick/badges/gpa.svg)](https://codeclimate.com/github/minimagick/minimagick)
6
+
7
+ A ruby wrapper for [ImageMagick](http://imagemagick.org/) command line.
8
+
9
+ ## Why?
10
+
11
+ I was using [RMagick](https://github.com/rmagick/rmagick) and loving it, but it
12
+ was eating up huge amounts of memory. Even a simple script would use over 100MB
13
+ of RAM. On my local machine this wasn't a problem, but on my hosting server the
14
+ ruby apps would crash because of their 100MB memory limit.
15
+
16
+ ## Solution!
17
+
18
+ Using MiniMagick the ruby processes memory remains small (it spawns
19
+ ImageMagick's command line program mogrify which takes up some memory as well,
20
+ but is much smaller compared to RMagick). See [Thinking of switching from
21
+ RMagick?](#thinking-of-switching-from-rmagick) below.
22
+
23
+ MiniMagick gives you access to all the command line options ImageMagick has
24
+ (found [here](http://www.imagemagick.org/script/command-line-options.php)).
25
+
26
+ ## Requirements
27
+
28
+ ImageMagick command-line tool has to be installed. You can check if you have it
29
+ installed by running
30
+
31
+ ```sh
32
+ $ magick -version
33
+ Version: ImageMagick 7.1.1-33 Q16-HDRI aarch64 22263 https://imagemagick.org
34
+ Copyright: (C) 1999 ImageMagick Studio LLC
35
+ License: https://imagemagick.org/script/license.php
36
+ Features: Cipher DPC HDRI Modules OpenMP(5.0)
37
+ Delegates (built-in): bzlib fontconfig freetype gslib heic jng jp2 jpeg jxl lcms lqr ltdl lzma openexr png ps raw tiff webp xml zlib zstd
38
+ Compiler: gcc (4.2)
39
+ ```
40
+
41
+ ## Installation
42
+
43
+ Add the gem to your Gemfile:
44
+
45
+ ```sh
46
+ $ bundle add mini_magick
47
+ ```
48
+
49
+ ## Information
50
+
51
+ * [API documentation](https://rubydoc.info/gems/mini_magick)
52
+
53
+ ## Usage
54
+
55
+ Let's first see a basic example of resizing an image.
56
+
57
+ ```rb
58
+ require "mini_magick"
59
+
60
+ image = MiniMagick::Image.open("input.jpg")
61
+ image.path #=> "/var/folders/k7/6zx6dx6x7ys3rv3srh0nyfj00000gn/T/magick20140921-75881-1yho3zc.jpg"
62
+ image.resize "100x100"
63
+ image.format "png"
64
+ image.write "output.png"
65
+ ```
66
+
67
+ `MiniMagick::Image.open` makes a copy of the image, and further methods modify
68
+ that copy (the original stays untouched). We then
69
+ [resize](http://www.imagemagick.org/script/command-line-options.php#resize)
70
+ the image, and write it to a file. The writing part is necessary because
71
+ the copy is just temporary, it gets garbage collected when we lose reference
72
+ to the image.
73
+
74
+ `MiniMagick::Image.open` also accepts URLs, and options passed in will be
75
+ forwarded to [open-uri](https://github.com/ruby/open-uri).
76
+
77
+ ```rb
78
+ image = MiniMagick::Image.open("http://example.com/image.jpg")
79
+ image.contrast
80
+ image.write("from_internets.jpg")
81
+ ```
82
+
83
+ On the other hand, if we want the original image to actually *get* modified,
84
+ we can use `MiniMagick::Image.new`.
85
+
86
+ ```rb
87
+ image = MiniMagick::Image.new("input.jpg")
88
+ image.path #=> "input.jpg"
89
+ image.resize "100x100"
90
+ # Not calling #write, because it's not a copy
91
+ ```
92
+
93
+ ### Combine options
94
+
95
+ While using methods like `#resize` directly is convenient, if we use more
96
+ methods in this way, it quickly becomes inefficient, because it calls the
97
+ command on each methods call. `MiniMagick::Image#combine_options` takes
98
+ multiple options and from them builds one single command.
99
+
100
+ ```rb
101
+ image.combine_options do |b|
102
+ b.resize "250x200>"
103
+ b.rotate "-90"
104
+ b.flip
105
+ end # the command gets executed
106
+ ```
107
+
108
+ As a handy shortcut, `MiniMagick::Image.new` also accepts an optional block
109
+ which is used to `combine_options`.
110
+
111
+ ```rb
112
+ image = MiniMagick::Image.new("input.jpg") do |b|
113
+ b.resize "250x200>"
114
+ b.rotate "-90"
115
+ b.flip
116
+ end # the command gets executed
117
+ ```
118
+
119
+ The yielded builder is an instance of `MiniMagick::Tool`. To learn more
120
+ about its interface, see [Tools](#tools) below.
121
+
122
+ ### Attributes
123
+
124
+ A `MiniMagick::Image` has various handy attributes.
125
+
126
+ ```rb
127
+ image.type #=> "JPEG"
128
+ image.width #=> 250
129
+ image.height #=> 300
130
+ image.dimensions #=> [250, 300]
131
+ image.size #=> 3451 (in bytes)
132
+ image.colorspace #=> "DirectClass sRGB"
133
+ image.exif #=> {"DateTimeOriginal" => "2013:09:04 08:03:39", ...}
134
+ image.resolution #=> [75, 75]
135
+ image.signature #=> "60a7848c4ca6e36b8e2c5dea632ecdc29e9637791d2c59ebf7a54c0c6a74ef7e"
136
+ ```
137
+
138
+ If you need more control, you can also access [raw image
139
+ attributes](http://www.imagemagick.org/script/escape.php):
140
+
141
+ ```rb
142
+ image["%[gamma]"] # "0.9"
143
+ ```
144
+
145
+ To get the all information about the image, MiniMagick gives you a handy method
146
+ which returns the output from `magick input.jpg json:`:
147
+
148
+ ```rb
149
+ image.data #=>
150
+ # {
151
+ # "format": "JPEG",
152
+ # "mimeType": "image/jpeg",
153
+ # "class": "DirectClass",
154
+ # "geometry": {
155
+ # "width": 200,
156
+ # "height": 276,
157
+ # "x": 0,
158
+ # "y": 0
159
+ # },
160
+ # "resolution": {
161
+ # "x": "300",
162
+ # "y": "300"
163
+ # },
164
+ # "colorspace": "sRGB",
165
+ # "channelDepth": {
166
+ # "red": 8,
167
+ # "green": 8,
168
+ # "blue": 8
169
+ # },
170
+ # "quality": 92,
171
+ # "properties": {
172
+ # "date:create": "2016-07-11T19:17:53+08:00",
173
+ # "date:modify": "2016-07-11T19:17:53+08:00",
174
+ # "exif:ColorSpace": "1",
175
+ # "exif:ExifImageLength": "276",
176
+ # "exif:ExifImageWidth": "200",
177
+ # "exif:ExifOffset": "90",
178
+ # "exif:Orientation": "1",
179
+ # "exif:ResolutionUnit": "2",
180
+ # "exif:XResolution": "300/1",
181
+ # "exif:YResolution": "300/1",
182
+ # "icc:copyright": "Copyright (c) 1998 Hewlett-Packard Company",
183
+ # "icc:description": "sRGB IEC61966-2.1",
184
+ # "icc:manufacturer": "IEC http://www.iec.ch",
185
+ # "icc:model": "IEC 61966-2.1 Default RGB colour space - sRGB",
186
+ # "jpeg:colorspace": "2",
187
+ # "jpeg:sampling-factor": "1x1,1x1,1x1",
188
+ # "signature": "1b2336f023e5be4a9f357848df9803527afacd4987ecc18c4295a272403e52c1"
189
+ # },
190
+ # ...
191
+ # }
192
+ ```
193
+
194
+ ### Pixels
195
+
196
+ With MiniMagick you can retrieve a matrix of image pixels, where each member of
197
+ the matrix is a 3-element array of numbers between 0-255, one for each range of
198
+ the RGB color channels.
199
+
200
+ ```rb
201
+ image = MiniMagick::Image.open("image.jpg")
202
+ pixels = image.get_pixels
203
+ pixels[3][2][1] # the green channel value from the 4th-row, 3rd-column pixel
204
+ ```
205
+
206
+ It can also be called after applying transformations:
207
+
208
+ ```rb
209
+ image = MiniMagick::Image.open("image.jpg")
210
+ image.crop "20x30+10+5"
211
+ image.colorspace "Gray"
212
+ pixels = image.get_pixels
213
+ ```
214
+
215
+ ### Pixels To Image
216
+
217
+ Sometimes when you have pixels and want to create image from pixels, you can do this to form an image:
218
+
219
+ ```rb
220
+ image = MiniMagick::Image.open('/Users/rabin/input.jpg')
221
+ pixels = image.get_pixels
222
+ depth = 8
223
+ dimension = [image.width, image.height]
224
+ map = 'rgb'
225
+ image = MiniMagick::Image.get_image_from_pixels(pixels, dimension, map, depth ,'jpg')
226
+ image.write('/Users/rabin/output.jpg')
227
+ ```
228
+
229
+ In this example, the returned pixels should now have equal R, G, and B values.
230
+
231
+ ### Configuration
232
+
233
+ Here are the available configuration options with their default values:
234
+
235
+ ```rb
236
+ MiniMagick.configure do |config|
237
+ config.timeout = nil # number of seconds IM commands may take
238
+ config.errors = true # raise errors non nonzero exit status
239
+ config.warnings = true # forward warnings to standard error
240
+ config.tmdir = Dir.tmpdir # alternative directory for tempfiles
241
+ config.logger = Logger.new($stdout) # where to log IM commands
242
+ config.cli_prefix = nil # add prefix to all IM commands
243
+ config.cli_env = {} # environment variables to set for IM commands
244
+ config.restricted_env = false # when true, block IM commands from accessing system environment variables other than those in cli_env
245
+ end
246
+ ```
247
+
248
+ For a more information, see
249
+ [Configuration](https://rubydoc.info/gems/mini_magick/MiniMagick/Configuration) API documentation.
250
+
251
+ ### Composite
252
+
253
+ MiniMagick also allows you to
254
+ [composite](http://www.imagemagick.org/script/composite.php) images:
255
+
256
+ ```rb
257
+ first_image = MiniMagick::Image.new("first.jpg")
258
+ second_image = MiniMagick::Image.new("second.jpg")
259
+ result = first_image.composite(second_image) do |c|
260
+ c.compose "Over" # OverCompositeOp
261
+ c.geometry "+20+20" # copy second_image onto first_image from (20, 20)
262
+ end
263
+ result.write "output.jpg"
264
+ ```
265
+
266
+ ### Layers/Frames/Pages
267
+
268
+ For multilayered images you can access its layers.
269
+
270
+ ```rb
271
+ gif.frames #=> [...]
272
+ pdf.pages #=> [...]
273
+ psd.layers #=> [...]
274
+
275
+ gif.frames.each_with_index do |frame, idx|
276
+ frame.write("frame#{idx}.jpg")
277
+ end
278
+ ```
279
+
280
+ ### Image validation
281
+
282
+ You can test whether an image is valid by running it through `identify`:
283
+
284
+ ```rb
285
+ image.valid?
286
+ image.validate! # raises MiniMagick::Invalid if image is invalid
287
+ ```
288
+
289
+ ### Logging
290
+
291
+ You can choose to log MiniMagick commands and their execution times:
292
+
293
+ ```rb
294
+ MiniMagick.logger.level = Logger::DEBUG
295
+ ```
296
+ ```
297
+ D, [2016-03-19T07:31:36.755338 #87191] DEBUG -- : [0.01s] identify /var/folders/k7/6zx6dx6x7ys3rv3srh0nyfj00000gn/T/mini_magick20160319-87191-1ve31n1.jpg
298
+ ```
299
+
300
+ In Rails you'll probably want to set `MiniMagick.logger = Rails.logger`.
301
+
302
+ ## Tools
303
+
304
+ If you prefer not to use the `MiniMagick::Image` abstraction, you can use ImageMagick's command-line tools directly:
305
+
306
+ ```rb
307
+ MiniMagick.convert do |convert|
308
+ convert << "input.jpg"
309
+ convert.resize("100x100")
310
+ convert.negate
311
+ convert << "output.jpg"
312
+ end #=> `magick input.jpg -resize 100x100 -negate output.jpg`
313
+
314
+ # OR
315
+
316
+ convert = MiniMagick.convert
317
+ convert << "input.jpg"
318
+ convert.resize("100x100")
319
+ convert.negate
320
+ convert << "output.jpg"
321
+ convert.call #=> `magick input.jpg -resize 100x100 -negate output.jpg`
322
+ ```
323
+
324
+ This way of using MiniMagick is highly recommended if you want to maximize performance of your image processing. There are class methods for each CLI tool: `animate`, `compare`, `composite`, `conjure`, `convert`, `display`, `identify`, `import`, `mogrify` and `stream`. The `MiniMagick.convert` method will use `magick` on ImageMagick 7 and `convert` on ImageMagick 6.
325
+
326
+ ### Appending
327
+
328
+ The most basic way of building a command is appending strings:
329
+
330
+ ```rb
331
+ MiniMagick.convert do |convert|
332
+ convert << "input.jpg"
333
+ convert.merge! ["-resize", "500x500", "-negate"]
334
+ convert << "output.jpg"
335
+ end
336
+ ```
337
+
338
+ Note that it is important that every command you would pass to the command line
339
+ has to be separated with `<<`, e.g.:
340
+
341
+ ```rb
342
+ # GOOD
343
+ convert << "-resize" << "500x500"
344
+
345
+ # BAD
346
+ convert << "-resize 500x500"
347
+ ```
348
+
349
+ Shell escaping is also handled for you. If an option has a value that has
350
+ spaces inside it, just pass it as a regular string.
351
+
352
+ ```rb
353
+ convert << "-distort"
354
+ convert << "Perspective"
355
+ convert << "0,0,0,0 0,45,0,45 69,0,60,10 69,45,60,35"
356
+ ```
357
+ ```
358
+ magick -distort Perspective '0,0,0,0 0,45,0,45 69,0,60,10 69,45,60,35'
359
+ ```
360
+
361
+ ### Methods
362
+
363
+ Instead of passing in options directly, you can use Ruby methods:
364
+
365
+ ```rb
366
+ convert.resize("500x500")
367
+ convert.rotate(90)
368
+ convert.distort("Perspective", "0,0,0,0 0,45,0,45 69,0,60,10 69,45,60,35")
369
+ ```
370
+
371
+ ### Chaining
372
+
373
+ Every method call returns `self`, so you can chain them to create logical groups.
374
+
375
+ ```rb
376
+ MiniMagick.convert do |convert|
377
+ convert << "input.jpg"
378
+ convert.clone(0).background('gray').shadow('80x5+5+5')
379
+ convert.negate
380
+ convert << "output.jpg"
381
+ end
382
+ ```
383
+
384
+ ### "Plus" options
385
+
386
+ ```rb
387
+ MiniMagick.convert do |convert|
388
+ convert << "input.jpg"
389
+ convert.repage.+
390
+ convert.distort.+("Perspective", "more args")
391
+ end
392
+ ```
393
+ ```
394
+ magick input.jpg +repage +distort Perspective 'more args'
395
+ ```
396
+
397
+ ### Stacks
398
+
399
+ ```rb
400
+ MiniMagick.convert do |convert|
401
+ convert << "wand.gif"
402
+
403
+ convert.stack do |stack|
404
+ stack << "wand.gif"
405
+ stack.rotate(30)
406
+ stack.foo("bar", "baz")
407
+ end
408
+ # or
409
+ convert.stack("wand.gif", { rotate: 30, foo: ["bar", "baz"] })
410
+
411
+ convert << "images.gif"
412
+ end
413
+ ```
414
+ ```
415
+ magick wand.gif \( wand.gif -rotate 90 -foo bar baz \) images.gif
416
+ ```
417
+
418
+ ### STDIN and STDOUT
419
+
420
+ If you want to pass something to standard input, you can pass the `:stdin`
421
+ option to `#call`:
422
+
423
+ ```rb
424
+ identify = MiniMagick.identify
425
+ identify.stdin # alias for "-"
426
+ identify.call(stdin: image_content)
427
+ ```
428
+
429
+ MiniMagick also has `#stdout` alias for "-" for outputting file contents to
430
+ standard output:
431
+
432
+ ```rb
433
+ content = MiniMagick.convert do |convert|
434
+ convert << "input.jpg"
435
+ convert.auto_orient
436
+ convert.stdout # alias for "-"
437
+ end
438
+ ```
439
+
440
+ ### Capturing STDERR
441
+
442
+ Some MiniMagick tools such as `compare` output the result of the command on
443
+ standard error, even if the command succeeded. The result of
444
+ `MiniMagick::Tool#call` is always the standard output, but if you pass it a
445
+ block, it will yield the stdout, stderr and exit status of the command:
446
+
447
+ ```rb
448
+ compare = MiniMagick.compare
449
+ # build the command
450
+ compare.call do |stdout, stderr, status|
451
+ # ...
452
+ end
453
+ ```
454
+
455
+ ## Configuring
456
+
457
+ ### GraphicsMagick
458
+
459
+ As of MiniMagick 5+, [GraphicsMagick](http://www.graphicsmagick.org/) isn't
460
+ officially supported. This means its installation won't be auto-detected, and no
461
+ attempts will be made to handle differences in GraphicsMagick API or output.
462
+
463
+ However, you can still configure MiniMagick to use it:
464
+
465
+ ```rb
466
+ MiniMagick.configure do |config|
467
+ config.graphicsmagick = true
468
+ end
469
+ ```
470
+
471
+ Some MiniMagick features won't be supported, such as global timeout,
472
+ `MiniMagick::Image#data` and `MiniMagick::Image#exif`.
473
+
474
+ ### Limiting resources
475
+
476
+ ImageMagick supports a number of [environment variables] for controlling its
477
+ resource limits. For example, you can enforce memory or execution time limits by
478
+ setting the following:
479
+
480
+ ```rb
481
+ MiniMagick.configure do |config|
482
+ config.cli_env = {
483
+ "MAGICK_MEMORY_LIMIT" => "128MiB",
484
+ "MAGICK_MAP_LIMIT" => "64MiB",
485
+ "MAGICK_TIME_LIMIT" => "30"
486
+ }
487
+ end
488
+ ```
489
+
490
+ For time limit you can also use the `timeout` configuration:
491
+
492
+ ```rb
493
+ MiniMagick.configure do |config|
494
+ config.timeout = 30 # 30 seconds
495
+ end
496
+ ```
497
+
498
+ ### Changing temporary directory
499
+
500
+ ImageMagick allows you to change the temporary directory to process the image file:
501
+
502
+ ```rb
503
+ MiniMagick.configure do |config|
504
+ config.tmpdir = File.join(Dir.tmpdir, "/my/new/tmp_dir")
505
+ end
506
+ ```
507
+
508
+ The example directory `/my/new/tmp_dir` must exist and must be writable.
509
+
510
+ If not configured, it will default to `Dir.tmpdir`.
511
+
512
+ ### Ignoring STDERR
513
+
514
+ If you're receiving warnings from ImageMagick that you don't care about, you
515
+ can avoid them being forwarded to standard error:
516
+
517
+ ```rb
518
+ MiniMagick.configure do |config|
519
+ config.warnings = false
520
+ end
521
+ ```
522
+
523
+ ### Avoiding raising errors
524
+
525
+ This gem raises an error when ImageMagick returns a nonzero exit code.
526
+ Sometimes, however, ImageMagick returns nonzero exit codes when the command
527
+ actually went ok. In these cases, to avoid raising errors, you can add the
528
+ following configuration:
529
+
530
+ ```rb
531
+ MiniMagick.configure do |config|
532
+ config.errors = false
533
+ end
534
+ ```
535
+
536
+ You can also pass `errors: false` to individual commands:
537
+
538
+ ```rb
539
+ MiniMagick.identify(errors: false) do |b|
540
+ b.help
541
+ end
542
+ ```
543
+
544
+ ## Thinking of switching from RMagick?
545
+
546
+ Unlike RMagick, MiniMagick is a much thinner wrapper around ImageMagick.
547
+
548
+ * To piece together MiniMagick commands refer to the [Mogrify
549
+ Documentation](https://imagemagick.org/script/mogrify.php). For instance
550
+ you can use the `-flop` option as `image.flop`.
551
+ * Operations on a MiniMagick image tend to happen in-place as `image.trim`,
552
+ whereas RMagick has both copying and in-place methods like `image.trim` and
553
+ `image.trim!`.
554
+ * To open files with MiniMagick you use `MiniMagick::Image.open` as you would
555
+ `Magick::Image.read`. To open a file and directly edit it, use
556
+ `MiniMagick::Image.new`.
557
+
558
+ [environment variables]: https://imagemagick.org/script/resources.php#environment