class Dragonfly::LosslessRotate::Rotate

Only JPEG format

Public Instance Methods

call(content, degree = 90, optimize: nil, progressive: nil) click to toggle source
# File lib/dragonfly/lossless_rotate.rb, line 26
def call(content, degree = 90, optimize: nil, progressive: nil)
  unless [90, 180, 270, -90, -180, -270].include?(degree)
    warn "Rotate only by 90, 180 and 270 degrees allowed"
    degree = 90
  end

  optimize    = content.env[:libjpeg_optimize] if optimize.nil?
  progressive = content.env[:libjpeg_progressive] if progressive.nil?

  rotate(content, degree, optimize, progressive)
end

Private Instance Methods

jpeg?(content) click to toggle source
# File lib/dragonfly/lossless_rotate.rb, line 84
def jpeg?(content)
  content.shell_eval { |path| "identify -format \%m #{path}" } == "JPEG"
end
jpegtran_degrees(degree) click to toggle source
# File lib/dragonfly/lossless_rotate.rb, line 73
def jpegtran_degrees(degree)
  {
    90 => 90,
    180 => 180,
    270 => 180,
    -90 => 270,
    -180 => 180,
    -270 => 90
  }[degree]
end
pnmflip_degrees(degree) click to toggle source
# File lib/dragonfly/lossless_rotate.rb, line 62
def pnmflip_degrees(degree)
  {
    90 => 270,
    180 => 180,
    270 => 90,
    -90 => 90,
    -180 => 180,
    -270 => 270
  }[degree]
end
rotate(content, degree, optimize, progressive) click to toggle source
# File lib/dragonfly/lossless_rotate.rb, line 40
def rotate(content, degree, optimize, progressive)
  cjpeg_bin    = content.env[:cjpeg_bin] || "cjpeg"
  djpeg_bin    = content.env[:djpeg_bin] || "djpeg"
  jpegtran_bin = content.env[:jpegtran_bin] || "jpegtran"
  pnmflip_bin  = content.env[:pnmflip_bin] || "pnmflip"

  content.shell_update escape: false do |old_path, new_path|
    optimize_option    = " -optimize" if optimize
    progressive_option = " -progressive" if progressive

    lossless_rotate_command = "#{jpegtran_bin} -rotate #{jpegtran_degrees(degree)} -perfect#{progressive_option}#{optimize_option} '#{old_path}' > '#{new_path}'"

    decompress_command = " #{djpeg_bin} '#{old_path}'"
    pnmflip_command    = " #{pnmflip_bin} -r#{pnmflip_degrees(degree)}"
    cjpeg_command      = " #{cjpeg_bin}#{progressive_option}#{optimize_option} > '#{new_path}'"

    lossy_rotate_command = "#{decompress_command} |#{pnmflip_command} |#{cjpeg_command}"

    "#{lossless_rotate_command} ||#{lossy_rotate_command}"
  end
end