上传现有的图片版本,使其成为自己的 "base" 图片

Upload existing image version so it becomes its own "base" image

现有图像版本,例如 thumb_xyz.jpg,如何通过 Carrierwave 重新上传,以便缩略图成为其自己的 "base" 图像?

我试过把商店弄乱!上传者,但我无法让它工作...

uploader.ImageUploader.new
uploader.store!(image_url(:thumb))

这是一种解决方案,您可以调整和修改以满足您的需要!它会生成具有自己的裁剪版本的原始父图像,以及裁剪后的完全独立的基础图像。 Carrierwave 中通常使用和引用的父图像裁剪版本用作各种占位符,作为生成新基本图像的一种方式。所以在实践中,它会随着每一种作物不断变化。

如果上传器安装到模型 class 上(就像这里的情况),则需要通过关联的控制器完成工作,而不是在上传器或模型中 class。这非常重要。

在这种情况下,由于目标是有选择地提取和上传现有图像版本,并且由于裁剪本身是控制器更新操作的一部分,因此在此处添加了代码。要了解的关键事项之一是裁剪文件本身的位置:@image.image.versions[:crop]。有了这些知识,接下来只需将它作为参数传递即可。

images_controller.rb
...
def update
  respond_to do |format|
    if @image.update(image_params)
      format.html { redirect_to @image, notice: 'Image was successfully updated.'}
      format.json { render :show, status: :ok, location: @image }

      #### HERE IS THE SOLUTION ###
      @crop_image = current_user.images.build(image: @image.image.versions[:crop])
      if @crop_image.save
        format.html { redirect_to @crop_image, notice: 'Crop image successfully created.'}
        format.json { render :show, status: :created, location: @crop_image }
      else
        format.html { render :new, notice: 'Crop image could not be saved for some reason...'}
    end
  else
    format.html { render :edit }
    format.json { render json: @image.errors, status: :unprocessable_entity }
  end
end

结束