I’m trying to get watermarked image by sending get request from next.js front-end as follows:
Here I’m sending event_image id so that I can access it in back-end and apply watermark to it then return to front-end
`const handleImageDownload = async (eventImageId) => {
try {
const response = await getWatermarkedImageApi(eventImageId);
if (response.status !== 200) {
throw new Error('Failed to watermark image');
}
const data = response.data;
console.log(data);
} catch (error) {
setError(error.message);
} finally {
setLoading(false);
}
}`
Now coming to rails back-end in event_image/watermark_image operation
def add_watermark_to_image(ctx, event_image:, **)
return if event_image.blank?
image_name = event_image.image_name
return if image_name.blank? # Check if the image name is present
image_uploader = EventImageUploader.new(event_image, :image_name)
image_uploader.retrieve_from_store!(event_image)
image_path = image_name.path
return if image_path.blank? # Check if the image path is present
watermark = MiniMagick::Image.open(Rails.root.join('public', 'assets', 'images', 'watermark.png'))
watermark.resize "100x100^"
watermark.gravity "center"
image_target = MiniMagick::Image.open(image_path)
image_target.composite(watermark) do |c|
c.gravity "center"
end
new_image_path = Rails.root.join('tmp', "watermarked_#{Time.now.to_i}.png")
image_target.write(new_image_path)
ctx[:watermarked_image_path] = new_image_path.to_s
end
Here when I tried to hit the api I get error as follows:
Completed 500 Internal Server Error in 72942ms (ActiveRecord: 1.7ms | Allocations: 36342)
Errno::ENOENT (No such file or directory @ rb_sysopen – event_image/image_names/413/blob):
app/concepts/v1/event_image/operation/watermark_image.rb:50:in add_watermark_to_image' app/controllers/v1/event_images_controller.rb:5:in
watermark_image’
despite image_path being correct I’m getting this error
I tried to byebug the code it’s fine upto
return if image_path.blank?
but still gives error that there isn’t any image at the path provided
also from developer tools I checked if image path is correct and it’s correct then why
this error for file Errno::ENOENT (No such file or directory @ rb_sysopen – event_image/image_names/413/blob):
Tejas Ukalkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.