Получение первого изображения в gif с использованием carrierwave - PullRequest
6 голосов
/ 26 марта 2012

Я использую несущую волну для загрузки GIF-файлов, которая прекрасно работает, проблема возникает, когда я пытаюсь сгенерировать версию для большого пальца и конвертировать GIF-файл в формате JPEG с большим пальцем только первое изображение в GIF, я получаю ошибку:

LocalJumpError in ImagesController#create

no block given (yield)

app/controllers/images_controller.rb:21:in `new'
app/controllers/images_controller.rb:21:in `create'

Запрос

Параметры:

{"utf8"=>"✓",
"authenticity_token"=>"lPEjP1WtPxFdizL2/FAWGHzOZPtecb5nKzKO8dg5ZdE=",
"image"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007ff5d4cdc720 @original_filename="some-file-name.gif",
@content_type="image/gif",
@headers="Content-Disposition: form-data; name=\"image[image]\"; filename=\"some-file-name.gif\"\r\nContent-Type: image/gif\r\n",
@tempfile=#<File:/var/folders/c8/1t7w8nln4b3bvs4_nv2cyn2m0000gt/T/RackMultipart20120326-5101-gcyvk0>>,
"remote_image_url"=>"",
"title"=>"The red panda",
"nsw"=>"0"},
"commit"=>"Roll GIF"}

Вот код, который я использую для создания большого пальца:

version :thumb do
    process :resize_to_limit => [200, 200]
    process :convert => 'jpg'
end

Надеюсь, что вы, ребята, можете помочь и заранее спасибо.

Ответы [ 4 ]

6 голосов
/ 18 ноября 2012

Чтобы удалить анимацию из изображения в формате gif с помощью carrierwave, вы можете определить следующий процессор:

def remove_animation
  manipulate! do |img, index|
    index == 0 ? img : nil
  end
end

Итак, код для версии для большого пальца будет:

version :thumb do
  process :remove_animation
  process :resize_to_limit => [200, 200]
  process :convert => 'jpg'
end
1 голос
/ 26 августа 2015

Добавьте в свой класс загрузчика следующее:

version :thumb do
  process :remove_animation
  process :resize_to_limit => [200, 200]
  process :convert => 'jpg'
end

# add process :remove_animation to other thumbs

private
def remove_animation
  if content_type == 'image/gif'
    manipulate! { |image| image.collapse! }
  end
end
1 голос
/ 24 августа 2012

Вот как я в итоге сплющил анимированный GIF (извлекая первое изображение GIF).

  process :custom_convert => 'jpg'

  # Method handles gif animation conversion by extracting out first frame within a gif
  def custom_convert(format)
    cache_stored_file! if !cached?
    image = ::Magick::Image.read(current_path)
    frames = image.first
    frames.write("#{format}:#{current_path}")
    destroy_image(frames)
  end
0 голосов
/ 28 марта 2012

Так вот код, который я использовал для достижения того, что хотел:

manipulate! do |image|
  gif = Magick::ImageList.new image.filename
  jpg = gif[0]
  jpg.resize_to_fill!(200,200)
  jpg.write("thumb_#{secure_token}.#{format}")
  jpg
end
...