How to place text on image - ruby-on-rails

Is there a way to place text on image in Rails? I am using Carrierwave for image upload, but I don't think it supports watermarking.
I tried attaching image watermark and made it work but can't figure out how to watermark with text.
For example, this is good way to place image watermark.

This is the code that I user to put watermarks on a image I am sure that you can change it a bit to make it your own. Also make sure that you have Magick turned on.
Take a look at carrierwave-add-a-watermark-to-processed-images its similar
# Process files as they are uploaded:
process :resize_to_fill => [850, 315]
process :convert => 'png'
process :watermark
def watermark
manipulate! do |img|
logo = Magick::Image.read("#{Rails.root}/app/assets/images/watermark.png").first
img = img.composite(logo, Magick::NorthWestGravity, 15, 0, Magick::OverCompositeOp)
end
end

Related

Carrierwave MiniMagick PDF Preview

Uploading files in my Rails app via Carrierwave / MiniMagick. Trying to create previews of the first page of PDFs. Ran into a few issues:
1) Some PDFs convert but the background is all black. Images and comments are visible
2) Some PDFs result in this error:
ImageProcessing::Error - Source format is multi-layer, but destination format is single-layer. If you care only about the first layer, add `.loader(page: 0)` to your pipeline. If you want to process each layer, see https://github.com/janko/image_processing/wiki/Splitting-a-PDF-into-multiple-images or use `.saver(allow_splitting: true)`.:
My codes looks like this:
version :thumb do
process :convert => 'jpg'
process :resize_to_limit => [50, 50]
def full_filename (for_file = model.file.file)
"preview_thumb.jpg"
end
end
Not clearly understanding which MiniMagick command line arg to add and how to add them.

Carrierwave: convert an uploaded PNG to JPG by replacing the original version (or: having versions with a different file format than original file)

I have the following model:
class ScreenshotUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
convert :jpg
version :thumb do
process resize_to_fill: [50, 50]
end
def extension_whitelist
%w(jpg jpeg gif png)
end
version :print do
process border: ['black']
process quality: 80
end
end
The upload of the image happens via pasting an image from the clipboard via https://github.com/layerssss/paste.js and is saved as a base64 encoded string into a <textarea>, then uploaded using the https://github.com/y9v/carrierwave-base64 gem:
class Finding < ApplicationRecord
mount_base64_uploader :screenshot, ScreenshotUploader
end
In the HTML form, it looks like this:
After uploading, the result is the following files:
screenshot.png it's a PNG, not a JPG!
thumb_screenshot.jpg
print_screenshot.jpg
But I need the original file to be also converted to JPG, as I need to save disk space. How can I achieve this?
You can do it like it written on the carrier wave documentation
Just replace system("mogrify -resize '1200\>' #{file.file}") with system("mogrify -format jpg #{file.file}") and then remove original file.
Adding to Vasiliy's answer, I came up with the following:
after :store, :convert_original_to_jpg
def convert_original_to_jpg(new_file)
if version_name.nil?
system("mogrify -format jpg -quality 80 #{file.file}")
system("unlink #{file.file}") # Remove the old PNG file
model.update_column mounted_as, "#{mounted_as}.jpg" # The filename in the DB also needs to be manually set to .jpg!
end
end
While this works for creating the file, it does not when updating the file, as the new_file parameter then is nil, and thus all images are removed.
I think this is some quirk that has to do with the carrierwave-base64 gem, and I don't have any motivation to dig into this any further. So the proposed solution might not be too useful, but for the sake of documentation I wanted to post it here.
In my special case, I decided to let go of the idea of saving disk space by converting PNG to JPG. Instead, I simply set process quality: 80 to save at least some space on the versions.
For the original PNG (which is saved in lossless state by carrierwave-base64 gem), I simply use the following code to shrink its quality:
after :store, :optimise_images
def optimise_images(new_file)
return if Rails.env.test? # Optimising consumes quite some time, so let's disable it for tests
if version_name.nil?
image_optim = ImageOptim.new pngout: false,
svgo: false,
pngcrush: false,
optipng: false,
pngquant: {allow_lossy: true}, # Everything disabled except pngquant, to keep the performance at a good level
advpng: false
image_optim.optimize_images!(Dir["#{File.dirname(file.file)}/*.png"])
end
end

How can I a create thumbnail for any files like (image, video, PDF, .xlsx, .ai, etc) using carrierwave in Rails 5

I've tried with
system("convert -strip -thumbnail '100x>' #{org_file.file}[0] #{thumb}.png")
but this is not working on Heroku..
Is there any way to create a thumbnail?
Thanks
You would need to define it in your uploader
#Create different versions of your uploaded files:
version :thumb do
process :resize_to_fit => [50, 50]
end
checkout this

Watermarking with MiniMagick

Versions:
Ruby 2.2.3
Rails 4.2.4
mini_magick: 4.2.10
Carrierwave 0.10.0
Description
I am trying to create a watermarker for a small gallery using CarrierWave as uploader.
I want the watermark to be sized compared to the current image. Therefore I am trying to use a .svg-file with different opacities and a transparent background.
I am using a watermarker based on Carrierwave add a watermark to processed images
require 'mini_magick'
class Watermarker
def initialize(original_path, watermark_path)
#original_path = original_path.to_s
#watermark_path = watermark_path.to_s
end
def watermark!(options = {})
options[:gravity] ||= 'SouthEast'
image = MiniMagick::Image.open(#original_path)
watermark = MiniMagick::Image.open(#watermark_path)
result = image.composite(watermark, 'png') do |c|
c.gravity options[:gravity]
end
result.write #original_path
end
end
And calling this as a process from my uploader.
My problems:
I cannot get the watermarker to input the picture with transparent background. I played around with:
https://github.com/minimagick/minimagick#composite
http://www.imagemagick.org/script/composite.php
But no progress.
I cannot adjust the size of the overlay image properly. There is a lot of settings for the geometry command but I'm stuck.
Any ideas and help would be great.

Using carriewave-vips with 16-bit tiff

I'm using carrierwave-vips (with ruby-vips) to upload and process 16 bit tiff. The 16 bit tiff will get save (not a problem for carrierewave alone), but I also want to process a thumbnail (jpeg). The problem is that the resulting thumbnail is completely blown out. What can I do?
The problem is that the 16 bit digtals that represents your band levels aren't getting scaled down. Add the following method to your uploader:
def convert_to_8bit
manipulate! do |image|
#vips specific
image.scale
end
end
and then process: :convert_to_8bit in your version.

Resources