如何“事后”用载波裁剪图像?在铁轨上?

发布于 2024-11-09 03:26:09 字数 440 浏览 0 评论 0原文

我想做的是上传图像,然后将用户带到一个新页面,在该页面中我将使用 Jcrop 让用户选择他们想要裁剪的图像部分,然后存储该图像。本质上,我想让它成为一个两阶段的过程。

我知道如何执行 javascript 部分,并且了解如何创建此功能的基本流程。但是,我不知道如何实现这一点的载波细节

我能找到的最接近的东西是:

image.recreate_versions!

但我仍然无法传递高度/宽度并开始 x,y 来裁剪它。

例如,我如何告诉 Carrierwave “事后”进行裁剪 - 即不是在第一次上传图像时进行裁剪?我看到“处理”图像的方法,但它们会以固定的高度和宽度自动发生。我怎样才能推迟这个呢?

本质上,我想做的是动态定义一个版本,我可以在其中指定高度、宽度以及 x,y

谢谢

What I'd like to do is upload an image, then take the user to a new page where I will use Jcrop to let the user select the part of the image they want to crop, and then store that image. Essentially, I want to make it a 2-stage process.

I know how to do the javascript part, and I understand the basic flow of how to create this functionality. However, I am not aware of the carrierwave specifics on how to accomplish this.

The closest thing I can find is:

image.recreate_versions!

But I still can't pass in the height/width and starting x,y to crop it.

For example, how can I tell carrierwave to do the cropping 'after the fact' - i.e. not when the image is uploaded for the first time? I see methods to "process" the image, but they happen automatically with fixed Height and Width. How can I delay this?

Essentially, what I'd like to do is define a version dynamically, where I can specify the height and width and x,y

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城半夏 2024-11-16 03:26:09

这是我能做的最好的了。可能有一种更简单的方法来做到这一点,但这是我的技巧:

这是传递裁剪信息时我的“POST”控制器操作:

  def update_crop
    @user = User.find(current_user.id)
    @user.crop(params[:x].to_i, params[:y].to_i, params[:h].to_i, params[:w].to_i)

    redirect_to(profile_path, :notice => 'Your profile and avatar was successfully updated.')
  end

这是添加到包含“头像”图像上传器的用户模型的方法:

  def crop(x, y, h, w)
    image = Magick::ImageList.new(avatar.current_path)
    cropped_image = image.crop(x, y, h, w)
    cropped_image.write(avatar.current_path)

    avatar.recreate_versions!
  end

基本上这只是劫持当前的,覆盖它,然后告诉 Carrierwave 创建

This is the best I can do. There's probably an easier way to do it, but this is my hack:

Here is my 'POST' controller action when the cropping info is passed:

  def update_crop
    @user = User.find(current_user.id)
    @user.crop(params[:x].to_i, params[:y].to_i, params[:h].to_i, params[:w].to_i)

    redirect_to(profile_path, :notice => 'Your profile and avatar was successfully updated.')
  end

Here is the method to add to the User model that contains an "avatar" image uploader:

  def crop(x, y, h, w)
    image = Magick::ImageList.new(avatar.current_path)
    cropped_image = image.crop(x, y, h, w)
    cropped_image.write(avatar.current_path)

    avatar.recreate_versions!
  end

Basically this just hi-jacks the current one, overwrites it, and then tells Carrierwave to create

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文