Comments (2)
Hey @toobulkeh, thanks for this note. Since CarrierWave can directly provide the path to an uploaded image, it can be used with imgix-rails directly, similar to Paperclip. I went ahead and documented that here: https://github.com/imgix/imgix-rails#paperclip-and-carrierwave. Thanks again!
from imgix-rails.
Here's an uploader we used on a recent project (it does have a few faults, but we didn't know this gem existed!)
class PhotoUploader < CarrierWave::Uploader::Base
storage :fog
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
def default_url(*args)
"https://#{ENV["AWS_S3_BUCKET"]}.s3.amazonaws.com/#{self.default_path}"
end
def default_path(*args)
"images/#{model.class.to_s.underscore}/#{mounted_as}/blank.png"
end
def filename
"#{secure_token}.#{file.extension}" if original_filename.present?
end
def plain
@client ||= Imgix::Client.new(host: ENV["IMGIX_SOURCE"], include_library_param: false, secure: true)
output = @client.path("/#{self.default_path}")
if self.path.present?
output = @client.path("/#{self.path}")
end
output.to_url
end
def store_dir
"images/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def to_s
self.source
end
def thumbnail
self.source(height: 400)
end
def gallery
self.source(height: 1200)
end
protected
def secure_token(length=32)
var = :"@#{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(length/2))
end
def source(opts={})
width = opts[:width]
height = opts[:height] || opts[:width]
fit = opts[:fit] || "crop"
format = opts[:format] || "jpg"
quality = opts[:quality] || 50
@client ||= Imgix::Client.new(host: ENV["IMGIX_SOURCE"], include_library_param: false, secure: true)
output = @client.path("/#{self.default_path}")
if self.path.present?
output = @client.path("/#{self.path}")
end
output.fit(fit)
output.format(format)
output.quality(quality)
output = output.width(width) if width.present?
output = output.height(height) if height.present?
output.to_url
end
end
from imgix-rails.
Related Issues (20)
- Updating imgix-rb dependency to v4? HOT 2
- ssrf external service interaction to malicious external source - security issue HOT 8
- `ix_image_url` doesn't work with fingerprinted assets anymore HOT 3
- Usage with multiple sources? HOT 1
- Multiple secure_url_tokens when using multiple sources HOT 3
- max_width not working HOT 4
- Support Multiple Sources HOT 4
- data-srcset instead of srcset HOT 5
- Switching between dev (local filesystem) and production (aws s3) HOT 2
- SemVer? Changelog? HOT 4
- ix_picture_tag provides wrong ouput of v3 HOT 1
- extra srcset link generated causes issue on non-retina displays HOT 1
- bundler restricted to version <2.0
- ActiveStorage support HOT 5
- Support latest version of imgix-rb HOT 1
- How to use inside a model? HOT 9
- `min_width` and `max_width` are rendered to HTML tag HOT 1
- Digest hash HOT 3
- Getting static image instead of GIF HOT 4
- data attributes instead of plain srcset, src, sizes attribute HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from imgix-rails.