Adding text to posters with RMagick and Carrierwave. uninitialized constant PhotoUploader::Draw - ruby-on-rails

I am trying to do image manipulation as process to add some text to my app.
The effect I'm looking for is : adding text to image, after saving.
The problem I'm dealing here is I get error: uninitialized constant PhotoUploader::Draw.
photo_uploader.rb
process :poster
def poster
manipulate! do |source|
txt = Draw.new
txt.family
txt.pointsize = 12
txt.gravity = Magick::SouthGravity
txt.stroke = "#000000"
title = Demot.last.title
source = source.resize_to_fill(400, 400).border(10, 10, "black")
source.annotate(txt, 0, 0, 0, 40, title)
end
end

Change to
txt = Magick::Draw.new

Related

Prawn/PDF: Multiple formats/text on one line?

When I try the following code:
text "Hello "
text "World"
They render Hello on top of World instead of World right after Hello. I have some complicated formatting (highlighting, different font sizes etc) on text that I need on one line. I know that the :inline_formatting option exists but it seems this is too complicated to use that option.
I have the following code:
highlight_callback.rb:
class HighlightCallback
def initialize(options)
#color = options[:color]
#document = options[:document]
end
def render_behind(fragment)
original_color = #document.fill_color
#document.fill_color = #color
#document.fill_rectangle(fragment.top_left,
fragment.width,
fragment.height)
#document.fill_color = original_color
end
end
order.pdf.prawn:
highlight = HighlightCallback.new(:color => 'ffff00', :document => self)
#code....
text "Authorized Signature: "
formatted_text [{:text => "_" * 15, :callback => highlight }], :size => 20
which is producing the attached image. How can I get the signature line on the same level as the text?
Ruby 2.5.1
Rails 5.2.0
It's enough to change method text to text_box, i.e.:
bounding_box([0, cursor], width: 540, height: 40) do
stroke_color 'FFFF00'
stroke_bounds
date = 'Date: '
text_box date, style: :bold
text_box DateTime.now.strftime('%Y/%m/%d'), at: [bounds.left + width_of(date), cursor]
text_box "Signature ________________", align: :right
end
Example:
To place text at a exact position you can use text_box with the option :at.
You can get the width of your text with pdf.width_of(str) (use the same style optione :size etc. otherwise it will use the default settings to calculate)

Rails Gem “Axlsx” / “Rails-Axlsx” - Adding Image

I try to add one image to my workbook/worksheet.
The Code :
wb.add_worksheet(:name => "Doc1", :page_setup => setup, :print_options => options) do |sheet|
img = File.expand_path('../logo.jpg', __FILE__)
sheet.add_image(:image_src => img, :noMove => true) do |image|
image.width = 7
image.height = 6
image.start_at 2, 2
end
...
end
But if i open the document there is no image on the worksheet, how can i fix this ?
And how is the correct path to the image?
For this test i copy the jpg in my view folder, but usually all images in "app/assets/images/logo.jpg".
I try it with img = File.expand_path('../assets/images/logo.jpg', __FILE__), but it fails with "No file found!"
An example on https://pramodbshinde.wordpress.com/2013/12/29/design-spreadsheets-using-axlsx-in-rails/ show the following line of code
img = File.expand_path(Rails.root+'app/assets/images/result.png')
Here is another way you can achieve this:
Code:
img = File.open(Dir.glob("#{Rails.root}/app/assets/images/result.png, "r")
In the lines where you set the image width and height you have them very small and you could be overlooking the image as this number is in pixels. Try setting them to higher number.
the following worked for me:
img = File.expand_path(Rails.root+'app/assets/images/result.png')
sheet.add_image(:image_src => img, :noMove => true) do |image|
image.width = 100
image.height = 150
image.start_at 2, 2
end

How to add text to image?

I am trying to add text on my image with RMagick. This is my code:
version :thumb do
process :resize_to_limit => [400, 300]
process :addt
end
def addt
manipulate! do |img|
title = Magick::Draw.new
img = title.annotate(img, 0,0,0,40, 'test') {
self.font_family = 'Helvetica'
self.fill = 'white'
self.stroke = 'transparent'
self.pointsize = 32
self.font_weight = Magick::BoldWeight
self.gravity = Magick::CenterGravity
}
end
end
The problem with this code is that it totally blocks my application. I can't open any other part of my site and can't turn off my server process. I need to kill server process completely to start the application again.
What could be a problem?
just try this, i cant solve your code . but hope this one can help you with that.
1st install this gem
Source: https://github.com/rmagick/rmagick
next
To start playing with RMagick, you can stick this in one of your controllers:
require ‘RMagick’
include Magick
def addt
img = ImageList.new(‘Your image path eg.public/computer-cat.jpg’)
txt = Draw.new
img.annotate(txt, 0,0,0,0, “The text you want to add in the image”){
txt.gravity = Magick::SouthGravity
txt.pointsize = 25
txt.stroke = ‘#000000′
txt.fill = ‘#ffffff’
txt.font_weight = Magick::BoldWeight
}
img.format = ‘jpeg’
send_data img.to_blob, :stream => ‘false’, :filename => ‘test.jpg’, :type => ‘image/jpeg’, :disposition => ‘inline’
end
hope this one help you..
if you cant understand ..click this http://mikewilliamson.wordpress.com/2010/04/29/adding-text-to-pictures-with-rmagick-and-rails/

How to access model in carrierwave?

I did this the way it's below, but it's returning "no text". How to access model properly in carrierwave?
photo_uploader.rb
process :poster
def poster
manipulate! format:"jpg" do |source|
txt = Magick::Draw.new
txt.pointsize = 20
txt.gravity = Magick::SouthGravity
txt.fill = "white"
source.border(50, 50, "black").annotate(txt, 0, 0, 0, 0, "#{model.title}" )
end
end
You can access the model this way. All the error means is that there is actually no text in the title field of model. If you check the params hash, you'll probably see
"my_model"=>{"title"=>"", "image"=> ...)
So you can should check for a non-blank title in the controller:
unless params[:title].blank?
MyModel.create(params[:my_model])
end

How can I generate fonts images using Rmagick?

In my app, there is a controller with a method for generate images from TrueType and OpenType files, which receives parameters like "color", "arbitrary text", "background".
The problem is when the path file contains white space:
def imagefont
font = Font.find params[:font]
file = File.basename font.file, File.extname(font.file)
fontfile = File.path(Pathname.new("public/downloads/#{font.name.slice(0,1).capitalize}/#{file}/#{font.file}"))
options = {
:max_width => 240,
:text_color => params[:color],
:font_size => 35,
:text => params[:text],
:bg_color => params[:background],
:font => fontfile
}
if File.exists?(options[:font])
canvas = Magick::Image.new 50, 50
image = Magick::Draw.new
begin
image.annotate(canvas, 0, 0, 0, 0, options[:text]) do
image.pointsize = options[:font_size]
image.font = options[:font]
end
metrics = image.get_type_metrics canvas, options[:text]
canvas = Magick::Image.new(metrics.width, metrics.height){ self.background_color = options[:bg_color] }
options[:font_size] -= 1
end while metrics.width > options[:max_width]
image = Magick::Draw.new
image.font options[:font]
image.font_size options[:font_size]
image.fill options[:text_color]
image.text 0, 0, options[:text]
image.gravity = Magick::CenterGravity
image.draw canvas
temp = Tempfile.new([font.file, '.png'])
canvas.write(temp.path)
render :text => open(temp.path).read
end
end
The above code works, unless the font name contains whitespaces. In that case it displays the following error:
Magick::ImageMagickError in FontController#imagefont
non-conforming drawing primitive definition `Blick' # error/draw.c/DrawImage/3146
In this case the font name is "A Blick for All Seasons", so I think it's a problem with quotes. I tried put simple quotes, but I wasn't successful.
I hadn't answers, but I got a possible solution. I found and I have modified the method in the gem files that receives the fontfile parameter. Initially appears thus:
# File lib/RMagick.rb, line 335
def font(name)
primitive "font #{name}"
end
I've changed adding simple quotes and I got it, works fine:
# File lib/RMagick.rb, line 335
def font(name)
primitive "font \'#{name}\'"
end
I think I should send a "pull request" with this change. Unless there is another answer.

Resources