Clay
Clay

Reputation: 159

Image Watermark MiniMagick & Rails 5.2

I am trying to watermark the product images from a model. I have set static images for testing but I am getting an error and I don't understand why.

Can anyone assist in why I am getting this error:

undefined method `composite' for "CIAO12.png":String

Current Setup: Rails 5.2 gem 'mini_magick', '~> 4.8' (Included in the Gemfile)

Brew install imagemagick (Successful)

Code is testing on static images but will be change to the product image instead of the 'CIA012.png' image.

Show Action

<div class="card-body">
  <h5 class="card-title"><%= @product.product_name %></h5>
  <% if @product.image.attached? %>
   <%= image_tag(@product.watermark) %>
  <% end %>
</div>

Product Model

def watermark
  first_image  = "CIAO12.png"
  second_image = "watermark.png"
  result = first_image.composite(second_image) do |c|
    c.compose "Over"    # OverCompositeOp
    c.geometry "+20+20" # copy second_image onto first_image from (20, 20)
  end
  result.write "output.jpg"
end

Upvotes: 3

Views: 447

Answers (1)

Skripin Pavel
Skripin Pavel

Reputation: 1

The problem is that you are trying to call the composite method on a String object. You should call it on a MiniMagick::Image object. I hope this helps if anyone else is facing the same issue.

require 'mini_magick'

def watermark
  first_image = MiniMagick::Image.open("CIAO12.png")

  second_image = MiniMagick::Image.open("watermark.png")

  result = first_image.composite(second_image) do |c|
    c.compose "Over"   
    c.geometry "+20+20"
  end

  result_path = "path/to/save/result.png" 
  result.write(result_path)

  result_path
end

Upvotes: 0

Related Questions