https://github.com/timfjord/carrierwave-data-uri
Carrierwave plugin that allows create image from data uri
https://github.com/timfjord/carrierwave-data-uri
carrierwave carrierwave-plugin ruby
Last synced: 3 months ago
JSON representation
Carrierwave plugin that allows create image from data uri
- Host: GitHub
- URL: https://github.com/timfjord/carrierwave-data-uri
- Owner: timfjord
- License: mit
- Created: 2015-02-06T15:32:26.000Z (over 11 years ago)
- Default Branch: main
- Last Pushed: 2022-05-10T14:42:12.000Z (about 4 years ago)
- Last Synced: 2026-02-15T02:28:24.734Z (4 months ago)
- Topics: carrierwave, carrierwave-plugin, ruby
- Language: Ruby
- Homepage:
- Size: 21.5 KB
- Stars: 25
- Watchers: 2
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://stand-with-ukraine.pp.ua)
# CarrierWave::DataUri 
Carrierwave plugin that allows to create images from data uri
:warning: Please read the [migration notes](#migration-notes) before upgrading to a new major version.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'carrierwave-data-uri'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install carrierwave-data-uri
## Usage
```ruby
class User < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
end
```
Then a data string can be used as a source for the uploader
```
user = User.find 123
user.avatar_data_uri = 'data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP////8AAP///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw=='
user.save
```
the data string can be retrieved too
```
user.avatar_data_uri # => data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP////8AAP///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw==
```
There will be no error in case of empty or invalid data.
There is a loud version available that could be used to raise an error on empty or invalid data:
```
user = User.find 123
user.avatar_data_uri_loud = '' # will raise `CarrierWave::DataUri::InvalidData` error
```
Optionally, to customize the file name, specify the `#{column}_data_filename` and `#{column}_data_mimetype` attributes before the `#{column}_data_uri` attribute.
```ruby
user = User.find 123
user.avatar_data_filename = 'somefile.jpg'
user.avatar_data_mimetype = 'image/jpeg'
user.avatar_data_uri = 'data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP////8AAP///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw=='
user.save
```
## Migration Notes
#### Version 0.2.0
- Assigning empty data is not raising an error now. Use loud version(`model.field_data_uri_loud =`) instead.
## Contributing
1. Fork it ( https://github.com/[my-github-username]/carrierwave-data-uri/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
## Credits
* http://www.davehulihan.com/uploading-data-uris-in-carrierwave/