Git Product home page Git Product logo

Comments (2)

paulstraw avatar paulstraw commented on September 26, 2024 1

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.

toobulkeh avatar toobulkeh commented on September 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.