mini_magick 4.10.1 → 4.13.2

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: 29b57b7ad13279ad3a6cdef274c7b9cc6efd5c509db5f6bd826e6affe6364081
4
- data.tar.gz: e958f807b9d779f94e502786510b656948d3ecbb9f89c2539ff0aa11afd0079e
3
+ metadata.gz: 44b0562f09fa6d0cc281ad1c4317b8e9915aa60dc6175072dc0cdc872f3d8367
4
+ data.tar.gz: 96225cd5bd66e3c0c065cb169057e196becfcdd1e0c883cb7c23f746d76bcda6
5
5
  SHA512:
6
- metadata.gz: 71843702fea5e44a8ef160c03af2319b6cce43d8191201056fff1a32e9297c3045e0c2cb15a6611243c4ab0ed9f8883a4f1669ef570c8da659e1c1a17d3e922f
7
- data.tar.gz: 5cb059a1319aa29750ec2e5e46d04b5b9256b2ca4896a72f28ca94594df573a16d607f35558fc49b00f9839b4e8553df94788e24842f43b18aaa10d9ec39c7e7
6
+ metadata.gz: a39cb409419be063b9cc27fd107a361d94d3f5dbdad719ed1f472efca70650c710160494f22b18f1408df479f29836d29c2bd71262ad1988cdf22caf85a9251a
7
+ data.tar.gz: dbb5ba23b0b773a696812ec343f0d267aee1653b0ebfb61ea9560af9c4cac68352b7c936c57233913d8848a1de3c9fe02e31070a94e5f3f0d7f471e444615a55
data/README.md ADDED
@@ -0,0 +1,573 @@
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/) or
8
+ [GraphicsMagick](http://www.graphicsmagick.org/) command line.
9
+
10
+ ## Why?
11
+
12
+ I was using [RMagick](https://github.com/rmagick/rmagick) and loving it, but it
13
+ was eating up huge amounts of memory. Even a simple script would use over 100MB
14
+ of RAM. On my local machine this wasn't a problem, but on my hosting server the
15
+ ruby apps would crash because of their 100MB memory limit.
16
+
17
+ ## Solution!
18
+
19
+ Using MiniMagick the ruby processes memory remains small (it spawns
20
+ ImageMagick's command line program mogrify which takes up some memory as well,
21
+ but is much smaller compared to RMagick). See [Thinking of switching from
22
+ RMagick?](#thinking-of-switching-from-rmagick) below.
23
+
24
+ MiniMagick gives you access to all the command line options ImageMagick has
25
+ (found [here](http://www.imagemagick.org/script/command-line-options.php)).
26
+
27
+ ## Requirements
28
+
29
+ ImageMagick or GraphicsMagick command-line tool has to be installed. You can
30
+ check if you have it installed by running
31
+
32
+ ```sh
33
+ $ magick -version
34
+ Version: ImageMagick 7.1.1-33 Q16-HDRI aarch64 22263 https://imagemagick.org
35
+ Copyright: (C) 1999 ImageMagick Studio LLC
36
+ License: https://imagemagick.org/script/license.php
37
+ Features: Cipher DPC HDRI Modules OpenMP(5.0)
38
+ 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
39
+ Compiler: gcc (4.2)
40
+ ```
41
+
42
+ ## Installation
43
+
44
+ Add the gem to your Gemfile:
45
+
46
+ ```rb
47
+ gem "mini_magick"
48
+ ```
49
+
50
+ ## Information
51
+
52
+ * [API documentation](http://rubydoc.info/github/minimagick/minimagick)
53
+
54
+ ## Usage
55
+
56
+ Let's first see a basic example of resizing an image.
57
+
58
+ ```rb
59
+ require "mini_magick"
60
+
61
+ image = MiniMagick::Image.open("input.jpg")
62
+ image.path #=> "/var/folders/k7/6zx6dx6x7ys3rv3srh0nyfj00000gn/T/magick20140921-75881-1yho3zc.jpg"
63
+ image.resize "100x100"
64
+ image.format "png"
65
+ image.write "output.png"
66
+ ```
67
+
68
+ `MiniMagick::Image.open` makes a copy of the image, and further methods modify
69
+ that copy (the original stays untouched). We then
70
+ [resize](http://www.imagemagick.org/script/command-line-options.php#resize)
71
+ the image, and write it to a file. The writing part is necessary because
72
+ the copy is just temporary, it gets garbage collected when we lose reference
73
+ to the image.
74
+
75
+ `MiniMagick::Image.open` also accepts URLs, and options passed in will be
76
+ forwarded to open-uri.
77
+
78
+ ```rb
79
+ image = MiniMagick::Image.open("http://example.com/image.jpg")
80
+ image.contrast
81
+ image.write("from_internets.jpg")
82
+ ```
83
+
84
+ On the other hand, if we want the original image to actually *get* modified,
85
+ we can use `MiniMagick::Image.new`.
86
+
87
+ ```rb
88
+ image = MiniMagick::Image.new("input.jpg")
89
+ image.path #=> "input.jpg"
90
+ image.resize "100x100"
91
+ # Not calling #write, because it's not a copy
92
+ ```
93
+
94
+ ### Combine options
95
+
96
+ While using methods like `#resize` directly is convenient, if we use more
97
+ methods in this way, it quickly becomes inefficient, because it calls the
98
+ command on each methods call. `MiniMagick::Image#combine_options` takes
99
+ multiple options and from them builds one single command.
100
+
101
+ ```rb
102
+ image.combine_options do |b|
103
+ b.resize "250x200>"
104
+ b.rotate "-90"
105
+ b.flip
106
+ end # the command gets executed
107
+ ```
108
+
109
+ As a handy shortcut, `MiniMagick::Image.new` also accepts an optional block
110
+ which is used to `combine_options`.
111
+
112
+ ```rb
113
+ image = MiniMagick::Image.new("input.jpg") do |b|
114
+ b.resize "250x200>"
115
+ b.rotate "-90"
116
+ b.flip
117
+ end # the command gets executed
118
+ ```
119
+
120
+ The yielded builder is an instance of `MiniMagick::Tool::Mogrify`. To learn more
121
+ about its interface, see [Metal](#metal) below.
122
+
123
+ ### Attributes
124
+
125
+ A `MiniMagick::Image` has various handy attributes.
126
+
127
+ ```rb
128
+ image.type #=> "JPEG"
129
+ image.mime_type #=> "image/jpeg"
130
+ image.width #=> 250
131
+ image.height #=> 300
132
+ image.dimensions #=> [250, 300]
133
+ image.size #=> 3451 (in bytes)
134
+ image.colorspace #=> "DirectClass sRGB"
135
+ image.exif #=> {"DateTimeOriginal" => "2013:09:04 08:03:39", ...}
136
+ image.resolution #=> [75, 75]
137
+ image.signature #=> "60a7848c4ca6e36b8e2c5dea632ecdc29e9637791d2c59ebf7a54c0c6a74ef7e"
138
+ ```
139
+
140
+ If you need more control, you can also access [raw image
141
+ attributes](http://www.imagemagick.org/script/escape.php):
142
+
143
+ ```rb
144
+ image["%[gamma]"] # "0.9"
145
+ ```
146
+
147
+ To get the all information about the image, MiniMagick gives you a handy method
148
+ which returns the output from `identify -verbose` in hash format:
149
+
150
+ ```rb
151
+ image.data #=>
152
+ # {
153
+ # "format": "JPEG",
154
+ # "mimeType": "image/jpeg",
155
+ # "class": "DirectClass",
156
+ # "geometry": {
157
+ # "width": 200,
158
+ # "height": 276,
159
+ # "x": 0,
160
+ # "y": 0
161
+ # },
162
+ # "resolution": {
163
+ # "x": "300",
164
+ # "y": "300"
165
+ # },
166
+ # "colorspace": "sRGB",
167
+ # "channelDepth": {
168
+ # "red": 8,
169
+ # "green": 8,
170
+ # "blue": 8
171
+ # },
172
+ # "quality": 92,
173
+ # "properties": {
174
+ # "date:create": "2016-07-11T19:17:53+08:00",
175
+ # "date:modify": "2016-07-11T19:17:53+08:00",
176
+ # "exif:ColorSpace": "1",
177
+ # "exif:ExifImageLength": "276",
178
+ # "exif:ExifImageWidth": "200",
179
+ # "exif:ExifOffset": "90",
180
+ # "exif:Orientation": "1",
181
+ # "exif:ResolutionUnit": "2",
182
+ # "exif:XResolution": "300/1",
183
+ # "exif:YResolution": "300/1",
184
+ # "icc:copyright": "Copyright (c) 1998 Hewlett-Packard Company",
185
+ # "icc:description": "sRGB IEC61966-2.1",
186
+ # "icc:manufacturer": "IEC http://www.iec.ch",
187
+ # "icc:model": "IEC 61966-2.1 Default RGB colour space - sRGB",
188
+ # "jpeg:colorspace": "2",
189
+ # "jpeg:sampling-factor": "1x1,1x1,1x1",
190
+ # "signature": "1b2336f023e5be4a9f357848df9803527afacd4987ecc18c4295a272403e52c1"
191
+ # },
192
+ # ...
193
+ # }
194
+ ```
195
+
196
+ Note that `MiniMagick::Image#data` is supported only on ImageMagick 6.8.8-3 or
197
+ above, for GraphicsMagick or older versions of ImageMagick use
198
+ `MiniMagick::Image#details`.
199
+
200
+ ### Pixels
201
+
202
+ With MiniMagick you can retrieve a matrix of image pixels, where each member of
203
+ the matrix is a 3-element array of numbers between 0-255, one for each range of
204
+ the RGB color channels.
205
+
206
+ ```rb
207
+ image = MiniMagick::Image.open("image.jpg")
208
+ pixels = image.get_pixels
209
+ pixels[3][2][1] # the green channel value from the 4th-row, 3rd-column pixel
210
+ ```
211
+
212
+ It can also be called after applying transformations:
213
+
214
+ ```rb
215
+ image = MiniMagick::Image.open("image.jpg")
216
+ image.crop "20x30+10+5"
217
+ image.colorspace "Gray"
218
+ pixels = image.get_pixels
219
+ ```
220
+
221
+ ### Pixels To Image
222
+
223
+ Sometimes when you have pixels and want to create image from pixels, you can do this to form an image:
224
+ ```rb
225
+ image = MiniMagick::Image.open('/Users/rabin/input.jpg')
226
+ pixels = image.get_pixels
227
+ depth = 8
228
+ dimension = [image.width, image.height]
229
+ map = 'rgb'
230
+ image = MiniMagick::Image.get_image_from_pixels(pixels, dimension, map, depth ,'jpg')
231
+ image.write('/Users/rabin/output.jpg')
232
+
233
+ ```
234
+
235
+ In this example, the returned pixels should now have equal R, G, and B values.
236
+
237
+ ### Configuration
238
+
239
+ ```rb
240
+ MiniMagick.configure do |config|
241
+ config.cli = :graphicsmagick
242
+ config.timeout = 5
243
+ end
244
+ ```
245
+
246
+ For a complete list of configuration options, see
247
+ [Configuration](http://rubydoc.info/github/minimagick/minimagick/MiniMagick/Configuration).
248
+
249
+ ### Composite
250
+
251
+ MiniMagick also allows you to
252
+ [composite](http://www.imagemagick.org/script/composite.php) images:
253
+
254
+ ```rb
255
+ first_image = MiniMagick::Image.new("first.jpg")
256
+ second_image = MiniMagick::Image.new("second.jpg")
257
+ result = first_image.composite(second_image) do |c|
258
+ c.compose "Over" # OverCompositeOp
259
+ c.geometry "+20+20" # copy second_image onto first_image from (20, 20)
260
+ end
261
+ result.write "output.jpg"
262
+ ```
263
+
264
+ ### Layers/Frames/Pages
265
+
266
+ For multilayered images you can access its layers.
267
+
268
+ ```rb
269
+ gif.frames #=> [...]
270
+ pdf.pages #=> [...]
271
+ psd.layers #=> [...]
272
+
273
+ gif.frames.each_with_index do |frame, idx|
274
+ frame.write("frame#{idx}.jpg")
275
+ end
276
+ ```
277
+
278
+ ### Image validation
279
+
280
+ By default, MiniMagick validates images each time it's opening them. It
281
+ validates them by running `identify` on them, and see if ImageMagick finds
282
+ them valid. This adds slight overhead to the whole processing. Sometimes it's
283
+ safe to assume that all input and output images are valid by default and turn
284
+ off validation:
285
+
286
+ ```rb
287
+ MiniMagick.configure do |config|
288
+ config.validate_on_create = false
289
+ end
290
+ ```
291
+
292
+ You can test whether an image is valid:
293
+
294
+ ```rb
295
+ image.valid?
296
+ image.validate! # raises MiniMagick::Invalid if image is invalid
297
+ ```
298
+
299
+ ### Logging
300
+
301
+ You can choose to log MiniMagick commands and their execution times:
302
+
303
+ ```rb
304
+ MiniMagick.logger.level = Logger::DEBUG
305
+ ```
306
+ ```
307
+ D, [2016-03-19T07:31:36.755338 #87191] DEBUG -- : [0.01s] identify /var/folders/k7/6zx6dx6x7ys3rv3srh0nyfj00000gn/T/mini_magick20160319-87191-1ve31n1.jpg
308
+ ```
309
+
310
+ In Rails you'll probably want to set `MiniMagick.logger = Rails.logger`.
311
+
312
+ ### Switching CLIs (ImageMagick \<=\> GraphicsMagick)
313
+
314
+ Default CLI is ImageMagick, but if you want to use GraphicsMagick, you can
315
+ specify it in configuration:
316
+
317
+ ```rb
318
+ MiniMagick.configure do |config|
319
+ config.cli = :graphicsmagick # or :imagemagick or :imagemagick7
320
+ end
321
+ ```
322
+
323
+ You can also use `.with_cli` to temporary switch the CLI:
324
+
325
+ ```rb
326
+ MiniMagick.with_cli(:graphicsmagick) do
327
+ # Some processing that GraphicsMagick is better at
328
+ end
329
+ ```
330
+
331
+ **WARNING**: If you're building a multithreaded web application, you should
332
+ change the CLI only on application startup. This is because the configuration is
333
+ global, so if you change it in a controller action, other threads in the same
334
+ process will also have their CLI changed, which could lead to race conditions.
335
+
336
+ ### Metal
337
+
338
+ If you want to be close to the metal, you can use ImageMagick's command-line
339
+ tools directly.
340
+
341
+ ```rb
342
+ MiniMagick::Tool::Magick.new do |magick|
343
+ magick << "input.jpg"
344
+ magick.resize("100x100")
345
+ magick.negate
346
+ magick << "output.jpg"
347
+ end #=> `magick input.jpg -resize 100x100 -negate output.jpg`
348
+
349
+ # OR
350
+
351
+ convert = MiniMagick::Tool::Convert.new
352
+ convert << "input.jpg"
353
+ convert.resize("100x100")
354
+ convert.negate
355
+ convert << "output.jpg"
356
+ convert.call #=> `convert input.jpg -resize 100x100 -negate output.jpg`
357
+ ```
358
+
359
+ If you're on ImageMagick 7, you should probably use `MiniMagick::Tool::Magick`,
360
+ though the legacy `MiniMagick::Tool::Convert` and friends will work too. On
361
+ ImageMagick 6 `MiniMagick::Tool::Magick` won't be available, so you should
362
+ instead use `MiniMagick::Tool::Convert` and friends.
363
+
364
+ This way of using MiniMagick is highly recommended if you want to maximize
365
+ performance of your image processing. We will now show the features available.
366
+
367
+ #### Appending
368
+
369
+ The most basic way of building a command is appending strings:
370
+
371
+ ```rb
372
+ MiniMagick::Tool::Magick.new do |convert|
373
+ convert << "input.jpg"
374
+ convert.merge! ["-resize", "500x500", "-negate"]
375
+ convert << "output.jpg"
376
+ end
377
+ ```
378
+
379
+ Note that it is important that every command you would pass to the command line
380
+ has to be separated with `<<`, e.g.:
381
+
382
+ ```rb
383
+ # GOOD
384
+ convert << "-resize" << "500x500"
385
+
386
+ # BAD
387
+ convert << "-resize 500x500"
388
+ ```
389
+
390
+ Shell escaping is also handled for you. If an option has a value that has
391
+ spaces inside it, just pass it as a regular string.
392
+
393
+ ```rb
394
+ convert << "-distort"
395
+ convert << "Perspective"
396
+ convert << "0,0,0,0 0,45,0,45 69,0,60,10 69,45,60,35"
397
+ ```
398
+ ```
399
+ convert -distort Perspective '0,0,0,0 0,45,0,45 69,0,60,10 69,45,60,35'
400
+ ```
401
+
402
+ #### Methods
403
+
404
+ Instead of passing in options directly, you can use Ruby methods:
405
+
406
+ ```rb
407
+ convert.resize("500x500")
408
+ convert.rotate(90)
409
+ convert.distort("Perspective", "0,0,0,0 0,45,0,45 69,0,60,10 69,45,60,35")
410
+ ```
411
+
412
+ MiniMagick knows which options each tool has, so you will get an explicit
413
+ `NoMethodError` if you happen to have misspelled an option.
414
+
415
+ #### Chaining
416
+
417
+ Every method call returns `self`, so you can chain them to create logical groups.
418
+
419
+ ```rb
420
+ MiniMagick::Tool::Magick.new do |convert|
421
+ convert << "input.jpg"
422
+ convert.clone(0).background('gray').shadow('80x5+5+5')
423
+ convert.negate
424
+ convert << "output.jpg"
425
+ end
426
+ ```
427
+
428
+ #### "Plus" options
429
+
430
+ ```rb
431
+ MiniMagick::Tool::Magick.new do |convert|
432
+ convert << "input.jpg"
433
+ convert.repage.+
434
+ convert.distort.+("Perspective", "more args")
435
+ end
436
+ ```
437
+ ```
438
+ convert input.jpg +repage +distort Perspective 'more args'
439
+ ```
440
+
441
+ #### Stacks
442
+
443
+ ```rb
444
+ MiniMagick::Tool::Magick.new do |convert|
445
+ convert << "wand.gif"
446
+
447
+ convert.stack do |stack|
448
+ stack << "wand.gif"
449
+ stack.rotate(30)
450
+ stack.foo("bar", "baz")
451
+ end
452
+ # or
453
+ convert.stack("wand.gif", { rotate: 30, foo: ["bar", "baz"] })
454
+
455
+ convert << "images.gif"
456
+ end
457
+ ```
458
+ ```
459
+ convert wand.gif \( wand.gif -rotate 90 -foo bar baz \) images.gif
460
+ ```
461
+
462
+ #### STDIN and STDOUT
463
+
464
+ If you want to pass something to standard input, you can pass the `:stdin`
465
+ option to `#call`:
466
+
467
+ ```rb
468
+ identify = MiniMagick::Tool::Identify.new
469
+ identify.stdin # alias for "-"
470
+ identify.call(stdin: image_content)
471
+ ```
472
+
473
+ MiniMagick also has `#stdout` alias for "-" for outputting file contents to
474
+ standard output:
475
+
476
+ ```rb
477
+ content = MiniMagick::Tool::Magick.new do |convert|
478
+ convert << "input.jpg"
479
+ convert.auto_orient
480
+ convert.stdout # alias for "-"
481
+ end
482
+ ```
483
+
484
+ #### Capturing STDERR
485
+
486
+ Some MiniMagick tools such as `compare` output the result of the command on
487
+ standard error, even if the command succeeded. The result of
488
+ `MiniMagick::Tool#call` is always the standard output, but if you pass it a
489
+ block, it will yield the stdout, stderr and exit status of the command:
490
+
491
+ ```rb
492
+ compare = MiniMagick::Tool::Compare.new
493
+ # build the command
494
+ compare.call do |stdout, stderr, status|
495
+ # ...
496
+ end
497
+ ```
498
+
499
+ ## Limiting resources
500
+
501
+ ImageMagick supports a number of environment variables for controlling its
502
+ resource limits. For example, you can enforce memory or execution time limits by
503
+ setting the following variables in your application's process environment:
504
+
505
+ * `MAGICK_MEMORY_LIMIT=128MiB`
506
+ * `MAGICK_MAP_LIMIT=64MiB`
507
+ * `MAGICK_TIME_LIMIT=30`
508
+
509
+ For a full list of variables and description, see [ImageMagick's resources
510
+ documentation](http://www.imagemagick.org/script/resources.php#environment).
511
+
512
+ ## Changing temporary directory
513
+
514
+ ImageMagick allows you to change the temporary directory to process the image file:
515
+
516
+ ```rb
517
+ MiniMagick.configure do |config|
518
+ config.tmpdir = File.join(Dir.tmpdir, "/my/new/tmp_dir")
519
+ end
520
+ ```
521
+
522
+ The example directory `/my/new/tmp_dir` must exist and must be writable.
523
+
524
+ If not configured, it will default to `Dir.tmpdir`.
525
+
526
+ ## Ignoring STDERR
527
+
528
+ If you're receiving warnings from ImageMagick that you don't care about, you
529
+ can avoid them being forwarded to standard error:
530
+
531
+ ```rb
532
+ MiniMagick.configure do |config|
533
+ config.warnings = false
534
+ end
535
+ ```
536
+
537
+ ## Troubleshooting
538
+
539
+ ### Errors being raised when they shouldn't
540
+
541
+ This gem raises an error when ImageMagick returns a nonzero exit code.
542
+ Sometimes, however, ImageMagick returns nonzero exit codes when the command
543
+ actually went ok. In these cases, to avoid raising errors, you can add the
544
+ following configuration:
545
+
546
+ ```rb
547
+ MiniMagick.configure do |config|
548
+ config.whiny = false
549
+ end
550
+ ```
551
+
552
+ If you're using the tool directly, you can pass `whiny: false` value to the
553
+ constructor:
554
+
555
+ ```rb
556
+ MiniMagick::Tool::Identify.new(whiny: false) do |b|
557
+ b.help
558
+ end
559
+ ```
560
+
561
+ ## Thinking of switching from RMagick?
562
+
563
+ Unlike RMagick, MiniMagick is a much thinner wrapper around ImageMagick.
564
+
565
+ * To piece together MiniMagick commands refer to the [Mogrify
566
+ Documentation](https://imagemagick.org/script/mogrify.php). For instance
567
+ you can use the `-flop` option as `image.flop`.
568
+ * Operations on a MiniMagick image tend to happen in-place as `image.trim`,
569
+ whereas RMagick has both copying and in-place methods like `image.trim` and
570
+ `image.trim!`.
571
+ * To open files with MiniMagick you use `MiniMagick::Image.open` as you would
572
+ `Magick::Image.read`. To open a file and directly edit it, use
573
+ `MiniMagick::Image.new`.
@@ -4,14 +4,6 @@ require 'logger'
4
4
  module MiniMagick
5
5
  module Configuration
6
6
 
7
- ##
8
- # Set whether you want to use [ImageMagick](http://www.imagemagick.org) or
9
- # [GraphicsMagick](http://www.graphicsmagick.org).
10
- #
11
- # @return [Symbol] `:imagemagick`, `:imagemagick7`, or `:graphicsmagick`
12
- #
13
- attr_accessor :cli
14
-
15
7
  ##
16
8
  # If you don't have the CLI tools in your PATH, you can set the path to the
17
9
  # executables.
@@ -53,6 +45,13 @@ module MiniMagick
53
45
  # @return [Logger]
54
46
  #
55
47
  attr_accessor :logger
48
+ ##
49
+ # Temporary directory used by MiniMagick, default is `Dir.tmpdir`, but
50
+ # you can override it.
51
+ #
52
+ # @return [String]
53
+ #
54
+ attr_accessor :tmpdir
56
55
 
57
56
  ##
58
57
  # If set to `true`, it will `identify` every newly created image, and raise
@@ -65,7 +64,7 @@ module MiniMagick
65
64
  ##
66
65
  # If set to `true`, it will `identify` every image that gets written (with
67
66
  # {MiniMagick::Image#write}), and raise `MiniMagick::Invalid` if the image
68
- # is not valid. Useful for validating that processing was sucessful,
67
+ # is not valid. Useful for validating that processing was successful,
69
68
  # although it adds a bit of overhead. Defaults to `true`.
70
69
  #
71
70
  # @return [Boolean]
@@ -81,20 +80,17 @@ module MiniMagick
81
80
  attr_accessor :whiny
82
81
 
83
82
  ##
84
- # Instructs MiniMagick how to execute the shell commands. Available
85
- # APIs are "open3" (default) and "posix-spawn" (requires the "posix-spawn"
86
- # gem).
87
- #
88
- # @return [String]
89
- #
90
- attr_accessor :shell_api
83
+ # If set to `false`, it will not forward warnings from ImageMagick to
84
+ # standard error.
85
+ attr_accessor :warnings
91
86
 
92
87
  def self.extended(base)
88
+ base.tmpdir = Dir.tmpdir
93
89
  base.validate_on_create = true
94
90
  base.validate_on_write = true
95
91
  base.whiny = true
96
- base.shell_api = "open3"
97
92
  base.logger = Logger.new($stdout).tap { |l| l.level = Logger::INFO }
93
+ base.warnings = true
98
94
  end
99
95
 
100
96
  ##
@@ -189,6 +185,15 @@ module MiniMagick
189
185
  logger.level = value ? Logger::DEBUG : Logger::INFO
190
186
  end
191
187
 
188
+ def shell_api=(value)
189
+ warn "MiniMagick.shell_api is deprecated and will be removed in MiniMagick 5. The posix-spawn gem doesn't improve performance recent Ruby versions anymore, so support for it will be removed."
190
+ @shell_api = value
191
+ end
192
+
193
+ def shell_api
194
+ @shell_api || "open3"
195
+ end
196
+
192
197
  # Backwards compatibility
193
198
  def reload_tools
194
199
  warn "MiniMagick.reload_tools is deprecated because it is no longer necessary"
@@ -42,7 +42,7 @@ module MiniMagick
42
42
 
43
43
  def cheap_info(value)
44
44
  @info.fetch(value) do
45
- format, width, height, size = self["%m %w %h %b"].split(" ")
45
+ format, width, height, size = parse_warnings(self["%m %w %h %b"]).split(" ")
46
46
 
47
47
  path = @path
48
48
  path = path.match(/\[\d+\]$/).pre_match if path =~ /\[\d+\]$/
@@ -62,11 +62,23 @@ module MiniMagick
62
62
  raise MiniMagick::Invalid, "image data can't be read"
63
63
  end
64
64
 
65
+ def parse_warnings(raw_info)
66
+ return raw_info unless raw_info.split("\n").size > 1
67
+
68
+ raw_info.split("\n").each do |line|
69
+ # must match "%m %w %h %b"
70
+ return line if line.match?(/^[A-Z]+ \d+ \d+ \d+(|\.\d+)([KMGTPEZY]{0,1})B$/)
71
+ end
72
+ raise TypeError
73
+ end
74
+
65
75
  def colorspace
66
76
  @info["colorspace"] ||= self["%r"]
67
77
  end
68
78
 
69
79
  def mime_type
80
+ warn "[MiniMagick] MiniMagick::Image#mime_type has been deprecated, because it wasn't returning correct result for all formats ImageMagick supports. Unfortunately, returning the correct MIME type would be very slow, because it would require ImageMagick to read the whole file. It's better to use Marcel and MimeMagic gems, which are able to determine the MIME type just from the image header."
81
+
70
82
  "image/#{self["format"].downcase}"
71
83
  end
72
84
 
@@ -15,7 +15,7 @@ module MiniMagick
15
15
  # methods.
16
16
  #
17
17
  # Use this to pass in a stream object. Must respond to #read(size) or be a
18
- # binary string object (BLOBBBB)
18
+ # binary string object (BLOB)
19
19
  #
20
20
  # Probably easier to use the {.open} method if you want to open a file or a
21
21
  # URL.
@@ -274,7 +274,7 @@ module MiniMagick
274
274
  #
275
275
  attribute :resolution
276
276
  ##
277
- # Returns the message digest of this image as a SHA-256, hexidecimal
277
+ # Returns the message digest of this image as a SHA-256, hexadecimal
278
278
  # encoded string. This signature uniquely identifies the image and is
279
279
  # convenient for determining if an image has been modified or whether two
280
280
  # images are identical.
@@ -340,13 +340,18 @@ module MiniMagick
340
340
  #
341
341
  # 1) one for each row of pixels
342
342
  # 2) one for each column of pixels
343
- # 3) three elements in the range 0-255, one for each of the RGB color channels
343
+ # 3) three or four elements in the range 0-255, one for each of the RGB(A) color channels
344
344
  #
345
345
  # @example
346
346
  # img = MiniMagick::Image.open 'image.jpg'
347
347
  # pixels = img.get_pixels
348
348
  # pixels[3][2][1] # the green channel value from the 4th-row, 3rd-column pixel
349
349
  #
350
+ # @example
351
+ # img = MiniMagick::Image.open 'image.jpg'
352
+ # pixels = img.get_pixels("RGBA")
353
+ # pixels[3][2][3] # the alpha channel value from the 4th-row, 3rd-column pixel
354
+ #
350
355
  # It can also be called after applying transformations:
351
356
  #
352
357
  # @example
@@ -357,19 +362,22 @@ module MiniMagick
357
362
  #
358
363
  # In this example, all pixels in pix should now have equal R, G, and B values.
359
364
  #
365
+ # @param map [String] A code for the mapping of the pixel data. Must be either
366
+ # 'RGB' or 'RGBA'. Default to 'RGB'
360
367
  # @return [Array] Matrix of each color of each pixel
361
- def get_pixels
368
+ def get_pixels(map="RGB")
369
+ raise ArgumentError, "Invalid map value" unless ["RGB", "RGBA"].include?(map)
362
370
  convert = MiniMagick::Tool::Convert.new
363
371
  convert << path
364
372
  convert.depth(8)
365
- convert << "RGB:-"
373
+ convert << "#{map}:-"
366
374
 
367
375
  # Do not use `convert.call` here. We need the whole binary (unstripped) output here.
368
376
  shell = MiniMagick::Shell.new
369
377
  output, * = shell.run(convert.command)
370
378
 
371
379
  pixels_array = output.unpack("C*")
372
- pixels = pixels_array.each_slice(3).each_slice(width).to_a
380
+ pixels = pixels_array.each_slice(map.length).each_slice(width).to_a
373
381
 
374
382
  # deallocate large intermediary objects
375
383
  output.clear
@@ -378,6 +386,23 @@ module MiniMagick
378
386
  pixels
379
387
  end
380
388
 
389
+ ##
390
+ # This is used to create image from pixels. This might be required if you
391
+ # create pixels for some image processing reasons and you want to form
392
+ # image from those pixels.
393
+ #
394
+ # *DANGER*: This operation can be very expensive. Please try to use with
395
+ # caution.
396
+ #
397
+ # @example
398
+ # # It is given in readme.md file
399
+ ##
400
+ def self.get_image_from_pixels(pixels, dimension, map, depth, mime_type)
401
+ pixels = pixels.flatten
402
+ blob = pixels.pack('C*')
403
+ import_pixels(blob, *dimension, depth, map, mime_type)
404
+ end
405
+
381
406
  ##
382
407
  # This is used to change the format of the image. That is, from "tiff to
383
408
  # jpg" or something like that. Once you run it, the instance is pointing to
@@ -436,6 +461,9 @@ module MiniMagick
436
461
  @info.clear
437
462
 
438
463
  self
464
+ rescue MiniMagick::Invalid, MiniMagick::Error => e
465
+ new_tempfile.unlink if new_tempfile && @tempfile != new_tempfile
466
+ raise e
439
467
  end
440
468
 
441
469
  ##
@@ -14,9 +14,10 @@ module MiniMagick
14
14
  stdout, stderr, status = execute(command, stdin: options[:stdin])
15
15
 
16
16
  if status != 0 && options.fetch(:whiny, MiniMagick.whiny)
17
- fail MiniMagick::Error, "`#{command.join(" ")}` failed with error:\n#{stderr}"
17
+ fail MiniMagick::Error, "`#{command.join(" ")}` failed with status: #{status.inspect} and error:\n#{stderr}"
18
18
  end
19
19
 
20
+ stderr = stderr.lines[2..-1].join if stderr.start_with? %(WARNING: The convert command is deprecated in IMv7)
20
21
  $stderr.print(stderr) unless options[:stderr] == false
21
22
 
22
23
  [stdout, stderr, status]
@@ -25,10 +26,10 @@ module MiniMagick
25
26
  def execute(command, options = {})
26
27
  stdout, stderr, status =
27
28
  log(command.join(" ")) do
28
- send("execute_#{MiniMagick.shell_api.gsub("-", "_")}", command, options)
29
+ send("execute_#{MiniMagick.shell_api.tr("-", "_")}", command, options)
29
30
  end
30
31
 
31
- [stdout, stderr, status.exitstatus]
32
+ [stdout, stderr, status&.exitstatus]
32
33
  rescue Errno::ENOENT, IOError
33
34
  ["", "executable not found: \"#{command.first}\"", 127]
34
35
  end
@@ -50,9 +51,7 @@ module MiniMagick
50
51
  end
51
52
  in_w.close
52
53
 
53
- begin
54
- Timeout.timeout(MiniMagick.timeout) { thread.join }
55
- rescue Timeout::Error
54
+ unless thread.join(MiniMagick.timeout)
56
55
  Process.kill("TERM", thread.pid) rescue nil
57
56
  Process.waitpid(thread.pid) rescue nil
58
57
  raise Timeout::Error, "MiniMagick command timed out: #{command}"
@@ -44,8 +44,10 @@ module MiniMagick
44
44
  # @private
45
45
  attr_reader :name, :args
46
46
 
47
- # @param whiny [Boolean] Whether to raise errors on exit codes different
48
- # than 0.
47
+ # @param name [String]
48
+ # @param options [Hash]
49
+ # @option options [Boolean] :whiny Whether to raise errors on non-zero
50
+ # exit codes.
49
51
  # @example
50
52
  # MiniMagick::Tool::Identify.new(whiny: false) do |identify|
51
53
  # identify.help # returns exit status 1, which would otherwise throw an error
@@ -84,7 +86,7 @@ module MiniMagick
84
86
  whiny = args.fetch(0, @whiny)
85
87
 
86
88
  options[:whiny] = whiny
87
- options[:stderr] = false if block_given?
89
+ options[:stderr] = MiniMagick.warnings && !block_given?
88
90
 
89
91
  shell = MiniMagick::Shell.new
90
92
  stdout, stderr, status = shell.run(command, options)
@@ -179,7 +181,7 @@ module MiniMagick
179
181
  # Create an ImageMagick stack in the command (surround.
180
182
  #
181
183
  # @example
182
- # MiniMagick::Tool::Convert.new do |convert|
184
+ # MiniMagick::Tool::Magick.new do |convert|
183
185
  # convert << "wand.gif"
184
186
  # convert.stack do |stack|
185
187
  # stack << "wand.gif"
@@ -190,9 +192,15 @@ module MiniMagick
190
192
  # end
191
193
  # # executes `convert wand.gif \( wizard.gif -rotate 30 \) +append images.gif`
192
194
  #
193
- def stack
195
+ def stack(*args)
194
196
  self << "("
195
- yield self
197
+ args.each do |value|
198
+ case value
199
+ when Hash then value.each { |key, value| send(key, *value) }
200
+ when String then self << value
201
+ end
202
+ end
203
+ yield self if block_given?
196
204
  self << ")"
197
205
  end
198
206
 
@@ -213,7 +221,7 @@ module MiniMagick
213
221
  # Adds ImageMagick's pseudo-filename `-` for standard output.
214
222
  #
215
223
  # @example
216
- # content = MiniMagick::Tool::Convert.new do |convert|
224
+ # content = MiniMagick::Tool::Magick.new do |convert|
217
225
  # convert << "input.jpg"
218
226
  # convert.auto_orient
219
227
  # convert.stdout
@@ -227,12 +235,13 @@ module MiniMagick
227
235
  ##
228
236
  # Define creator operator methods
229
237
  #
238
+ # @example
230
239
  # mogrify = MiniMagick::Tool.new("mogrify")
231
240
  # mogrify.canvas("khaki")
232
241
  # mogrify.command.join(" ") #=> "mogrify canvas:khaki"
233
242
  #
234
243
  CREATION_OPERATORS.each do |operator|
235
- define_method(operator.gsub('-', '_')) do |value = nil|
244
+ define_method(operator.tr('-', '_')) do |value = nil|
236
245
  self << "#{operator}:#{value}"
237
246
  self
238
247
  end
@@ -251,10 +260,11 @@ module MiniMagick
251
260
  ##
252
261
  # Any undefined method will be transformed into a CLI option
253
262
  #
263
+ # @example
254
264
  # mogrify = MiniMagick::Tool.new("mogrify")
255
265
  # mogrify.adaptive_blur("...")
256
266
  # mogrify.foo_bar
257
- # mogrify.command.join(" ") "mogrify -adaptive-blur ... -foo-bar"
267
+ # mogrify.command.join(" ") # => "mogrify -adaptive-blur ... -foo-bar"
258
268
  #
259
269
  def method_missing(name, *args)
260
270
  option = "-#{name.to_s.tr('_', '-')}"
@@ -24,7 +24,7 @@ module MiniMagick
24
24
  end
25
25
 
26
26
  def tempfile(extension)
27
- Tempfile.new(["mini_magick", extension]).tap do |tempfile|
27
+ Tempfile.new(["mini_magick", extension], MiniMagick.tmpdir).tap do |tempfile|
28
28
  tempfile.binmode
29
29
  yield tempfile if block_given?
30
30
  tempfile.close
@@ -8,8 +8,8 @@ module MiniMagick
8
8
 
9
9
  module VERSION
10
10
  MAJOR = 4
11
- MINOR = 10
12
- TINY = 1
11
+ MINOR = 13
12
+ TINY = 2
13
13
  PRE = nil
14
14
 
15
15
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_magick
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.10.1
4
+ version: 4.13.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Corey Johnson
@@ -10,10 +10,10 @@ authors:
10
10
  - James Miller
11
11
  - Thiago Fernandes Massa
12
12
  - Janko Marohnić
13
- autorequire:
13
+ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2020-01-06 00:00:00.000000000 Z
16
+ date: 2024-07-08 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: rake
@@ -43,48 +43,6 @@ dependencies:
43
43
  - - "~>"
44
44
  - !ruby/object:Gem::Version
45
45
  version: 3.5.0
46
- - !ruby/object:Gem::Dependency
47
- name: guard
48
- requirement: !ruby/object:Gem::Requirement
49
- requirements:
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: '0'
53
- type: :development
54
- prerelease: false
55
- version_requirements: !ruby/object:Gem::Requirement
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- version: '0'
60
- - !ruby/object:Gem::Dependency
61
- name: guard-rspec
62
- requirement: !ruby/object:Gem::Requirement
63
- requirements:
64
- - - ">="
65
- - !ruby/object:Gem::Version
66
- version: '0'
67
- type: :development
68
- prerelease: false
69
- version_requirements: !ruby/object:Gem::Requirement
70
- requirements:
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- version: '0'
74
- - !ruby/object:Gem::Dependency
75
- name: posix-spawn
76
- requirement: !ruby/object:Gem::Requirement
77
- requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- version: '0'
81
- type: :development
82
- prerelease: false
83
- version_requirements: !ruby/object:Gem::Requirement
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- version: '0'
88
46
  - !ruby/object:Gem::Dependency
89
47
  name: webmock
90
48
  requirement: !ruby/object:Gem::Requirement
@@ -112,13 +70,13 @@ extensions: []
112
70
  extra_rdoc_files: []
113
71
  files:
114
72
  - MIT-LICENSE
73
+ - README.md
115
74
  - Rakefile
116
75
  - lib/mini_gmagick.rb
117
76
  - lib/mini_magick.rb
118
77
  - lib/mini_magick/configuration.rb
119
78
  - lib/mini_magick/image.rb
120
79
  - lib/mini_magick/image/info.rb
121
- - lib/mini_magick/immutable_image.rb
122
80
  - lib/mini_magick/shell.rb
123
81
  - lib/mini_magick/tool.rb
124
82
  - lib/mini_magick/tool/animate.rb
@@ -140,7 +98,7 @@ homepage: https://github.com/minimagick/minimagick
140
98
  licenses:
141
99
  - MIT
142
100
  metadata: {}
143
- post_install_message:
101
+ post_install_message:
144
102
  rdoc_options: []
145
103
  require_paths:
146
104
  - lib
@@ -148,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
106
  requirements:
149
107
  - - ">="
150
108
  - !ruby/object:Gem::Version
151
- version: '2.0'
109
+ version: '2.3'
152
110
  required_rubygems_version: !ruby/object:Gem::Requirement
153
111
  requirements:
154
112
  - - ">="
@@ -156,8 +114,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
114
  version: '0'
157
115
  requirements:
158
116
  - You must have ImageMagick or GraphicsMagick installed
159
- rubygems_version: 3.1.2
160
- signing_key:
117
+ rubygems_version: 3.5.11
118
+ signing_key:
161
119
  specification_version: 4
162
120
  summary: Manipulate images with minimal use of memory via ImageMagick / GraphicsMagick
163
121
  test_files: []
@@ -1,18 +0,0 @@
1
- module MiniMagick
2
- class Image
3
- def initialize(source)
4
- if source.is_a?(String) || source.is_a?(Pathname)
5
- @source_path = source.to_s
6
- elsif source.respond_to?(:path)
7
- @source_path = source.path
8
- else
9
- fail ArgumentError, "invalid source object: #{source.inspect} (expected String, Pathname or #path)"
10
- end
11
- end
12
-
13
- def method_missing
14
- end
15
- end
16
- end
17
-
18
- image = MiniMagick::Image.new()