You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CarrierWave 3.0 comes with a change in the way of handling the file extension on conversion. This results in following issues if you use `process convert: :format` to change the file format:
41
+
42
+
- If you have it on the uploader itself (not within a version), the file extension of the cached file will change. That means if you serve both CarrierWave 2.x and 3.x simultaneously on the same workload (e.g. using blue-green deployment), a cache file stored by 2.x can't be retrieved by 3.x and vice versa.
43
+
- If you have it within a version, the file extension of the stored file will change. You need to perform `#recreate_versions!` to make it usable again.
44
+
45
+
To preserve the 2.x behavior, you can set `force_extension false` right after calling `process convert: :format`. See [#2659](https://github.com/carrierwaveuploader/carrierwave/pull/2659) for the detail.
46
+
38
47
## Getting Started
39
48
40
49
Start off by generating an uploader:
@@ -224,6 +233,20 @@ class MyUploader < CarrierWave::Uploader::Base
224
233
end
225
234
```
226
235
236
+
## Changing the filename
237
+
238
+
To change the filename of uploaded files, you can override `#filename` method in the uploader.
239
+
240
+
```ruby
241
+
classMyUploader < CarrierWave::Uploader::Base
242
+
deffilename
243
+
"image.#{file.extension}"# If you upload 'file.jpg', you'll get 'image.jpg'
244
+
end
245
+
end
246
+
```
247
+
248
+
Some old documentations (like [this](https://stackoverflow.com/a/5865117)) may instruct you to safeguard the filename value with `if original_filename`, but it's no longer necessary with CarrierWave 3.0 or later.
249
+
227
250
## Securing uploads
228
251
229
252
Certain files might be dangerous if uploaded to the wrong location, such as PHP
@@ -302,17 +325,8 @@ You no longer need to do this manually.
302
325
303
326
## Adding versions
304
327
305
-
Often you'll want to add different versions of the same file. The classic example is image thumbnails. There is built in support for this*:
306
-
307
-
*Note:* You must have Imagemagick installed to do image resizing.
308
-
309
-
Some documentation refers to RMagick instead of MiniMagick but MiniMagick is recommended.
310
-
311
-
To install Imagemagick on OSX with homebrew type the following:
312
-
313
-
```
314
-
$ brew install imagemagick
315
-
```
328
+
Often you'll want to add different versions of the same file. The classic example is generating image thumbnails while preserving the original file to be used for high-quality representation.
329
+
In this section we'll explore how CarrierWave supports working with multiple versions. The image manipulation itself is covered in [another section](#manipulating-images).
One important thing to remember is that process is called *before* versions are
349
363
created. This can cut down on processing cost.
350
364
351
-
### Processing Methods: mini_magick
352
-
353
-
-`convert` - Changes the image encoding format to the given format, eg. jpg
354
-
-`resize_to_limit` - Resize the image to fit within the specified dimensions while retaining the original aspect ratio. Will only resize the image if it is larger than the specified dimensions. The resulting image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.
355
-
-`resize_to_fit` - Resize the image to fit within the specified dimensions while retaining the original aspect ratio. The image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.
356
-
-`resize_to_fill` - Resize the image to fit within the specified dimensions while retaining the aspect ratio of the original image. If necessary, crop the image in the larger dimension. Optionally, a "gravity" may be specified, for example "Center", or "NorthEast".
357
-
-`resize_and_pad` - Resize the image to fit within the specified dimensions while retaining the original aspect ratio. If necessary, will pad the remaining area with the given color, which defaults to transparent (for gif and png, white for jpeg). Optionally, a "gravity" may be specified, as above.
358
-
359
-
See `carrierwave/processing/mini_magick.rb` for details.
360
-
361
-
### conditional process
365
+
### Conditional processing
362
366
363
367
If you want to use conditional process, you can only use `if` statement.
364
368
@@ -442,8 +446,27 @@ class MyUploader < CarrierWave::Uploader::Base
442
446
end
443
447
```
444
448
445
-
The option `:from_version` uses the file cached in the `:thumb` version instead
446
-
of the original version, potentially resulting in faster processing.
449
+
### Customizing version filenames
450
+
451
+
CarrierWave supports [customization of filename](#changing-the-filename) by overriding an uploader's
452
+
#filename method, but this doesn't work for versions because of the limitation on how CarrierWave
453
+
re-constructs the filename on retrieval of the stored file.
454
+
Instead, you can override `#full_filename` with providing a version-aware name.
455
+
456
+
```ruby
457
+
classMyUploader < CarrierWave::Uploader::Base
458
+
version :thumbdo
459
+
deffull_filename(for_file)
460
+
'thumb.png'
461
+
end
462
+
process convert:'png'
463
+
end
464
+
end
465
+
```
466
+
467
+
Please note that `#full_filename` mustn't be constructed based on a dynamic value
468
+
that can change from the time of store and time of retrieval, since it will result in
469
+
being unable to retrieve a file previously stored.
447
470
448
471
## Making uploads work across form redisplays
449
472
@@ -911,67 +934,81 @@ CarrierWave.configure do |config|
911
934
end
912
935
```
913
936
914
-
## Using RMagick
937
+
## Manipulating images
915
938
916
939
If you're uploading images, you'll probably want to manipulate them in some way,
917
-
you might want to create thumbnail images for example. CarrierWave comes with a
918
-
small library to make manipulating images with RMagick easier, you'll need to
919
-
include it in your Uploader:
940
+
you might want to create thumbnail images for example.
920
941
921
-
```ruby
922
-
classAvatarUploader < CarrierWave::Uploader::Base
923
-
includeCarrierWave::RMagick
924
-
end
942
+
### Using MiniMagick
943
+
944
+
MiniMagick performs all the operations using the 'convert' CLI which is part of the standard ImageMagick kit.
945
+
This allows you to have the power of ImageMagick without having to worry about installing
946
+
all the RMagick libraries, it often results in higher memory footprint.
947
+
948
+
See the MiniMagick site for more details:
949
+
950
+
https://github.com/minimagick/minimagick
951
+
952
+
To install Imagemagick on OSX with homebrew type the following:
953
+
954
+
```
955
+
$ brew install imagemagick
925
956
```
926
957
927
-
The RMagick module gives you a few methods, like
928
-
`CarrierWave::RMagick#resize_to_fill` which manipulate the image file in some
929
-
way. You can set a `process` callback, which will call that method any time a
930
-
file is uploaded.
931
-
There is a demonstration of convert here.
932
-
Convert will only work if the file has the same file extension, thus the use of the filename method.
958
+
And the ImageMagick command line options for more for what's on offer:
Check out the manipulate! method, which makes it easy for you to write your own
948
-
manipulation methods.
949
-
950
-
## Using MiniMagick
973
+
#### List of available processing methods:
951
974
952
-
MiniMagick is similar to RMagick but performs all the operations using the 'convert'
953
-
CLI which is part of the standard ImageMagick kit. This allows you to have the power
954
-
of ImageMagick without having to worry about installing all the RMagick libraries.
975
+
-`convert` - Changes the image encoding format to the given format(eg. jpg). This operation is treated specially to trigger the change of the file extension, so it matches with the format of the resulting file.
976
+
-`resize_to_limit` - Resize the image to fit within the specified dimensions while retaining the original aspect ratio. Will only resize the image if it is larger than the specified dimensions. The resulting image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.
977
+
-`resize_to_fit` - Resize the image to fit within the specified dimensions while retaining the original aspect ratio. The image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.
978
+
-`resize_to_fill` - Resize the image to fit within the specified dimensions while retaining the aspect ratio of the original image. If necessary, crop the image in the larger dimension. Optionally, a "gravity" may be specified, for example "Center", or "NorthEast".
979
+
-`resize_and_pad` - Resize the image to fit within the specified dimensions while retaining the original aspect ratio. If necessary, will pad the remaining area with the given color, which defaults to transparent (for gif and png, white for jpeg). Optionally, a "gravity" may be specified, as above.
955
980
956
-
See the MiniMagick site for more details:
981
+
See `carrierwave/processing/mini_magick.rb`for details.
957
982
958
-
https://github.com/minimagick/minimagick
983
+
### Using RMagick
959
984
960
-
And the ImageMagick command line options for more for what's on offer:
985
+
CarrierWave also comes with support for RMagick, a well-known image processing library.
986
+
To use it, you'll need to include this in your Uploader:
0 commit comments