[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

text to images using RMagick

Ray

3/14/2006 8:43:00 PM

Hi, I used this thread
(http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/c1e73e86ca02c6af/7868ea24e7069eea?#7868ea...)
to get text wrapping working, and it's working great.

The problem I can't seem to crack is forcing linefeeds or newline
breaks. How do I do it in a string constructed as a "caption:"? Or is
there another way to create an image with automatically wrapping text
that would support this?

Ta, Ray

"Timothy Hunter" <cycli...@nc.rr.com> wrote in message

news:Klpqe.1304$1u3.19632@twister.southeast.rr.com...
> Okay, I just uploaded a new version of RMagick, version 1.8.2. With this
> version you can use this script:
> require 'RMagick'
> include Magick

> img = Image.read("caption:My very long caption which should wrap at 200
> pixels") do
> self.size = "200x"
> self.pointsize = 20
> self.font = "Tahoma"
> end

> img[0].display

7 Answers

Tim Hunter

3/14/2006 11:08:00 PM

0

Ray wrote:
> Hi, I used this thread
> (http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/c1e73e86ca02c6af/7868ea24e7069eea?#7868ea...)
> to get text wrapping working, and it's working great.
>
> The problem I can't seem to crack is forcing linefeeds or newline
> breaks. How do I do it in a string constructed as a "caption:"? Or is
> there another way to create an image with automatically wrapping text
> that would support this?
>
> Ta, Ray
>
> "Timothy Hunter" <cycli...@nc.rr.com> wrote in message
>
> news:Klpqe.1304$1u3.19632@twister.southeast.rr.com...
>
>>Okay, I just uploaded a new version of RMagick, version 1.8.2. With this
>>version you can use this script:
>>require 'RMagick'
>>include Magick
>
>
>>img = Image.read("caption:My very long caption which should wrap at 200
>>pixels") do
>> self.size = "200x"
>> self.pointsize = 20
>>self.font = "Tahoma"
>> end
>
>
>>img[0].display
>
>

This works for me. You may need to upgrade your ImageMagick. I'm using
ImageMagick-6.2.6-3.

require 'RMagick'

str = "caption:This is\na string\nwith embedded\nnewlines\n"

img = Magick::Image.read(str) {self.size = "200x200"; self.gravity =
Magick::CenterGravity}
img[0].display

Ray

3/17/2006 7:25:00 AM

0

Hi Tim, thanks for your quick response. I'm very new to this, Im on Mac
OSX 10.4.5, how do I check the version of ImageMagick installed?

Also, when I do get it working, can all text formatting (eg: bold,
italic, colours, etc) work with the caption parameter?

Ta, Ray

Ray

3/17/2006 8:37:00 AM

0

Tim, i just tried this:

Tiger:/opt/local/bin rails$ composite -version
Version: ImageMagick 6.2.4 02/24/06 Q16 http://www.image...

So i guess I'm running ImageMagick 6.2.4???

Also, to clarify my question around text formatting, I need to set
formats in line (on particular words), not globally to the caption
parameter. Can it be done?

Cheers and happy St. Patrick's Day, Ray

Tim Hunter

3/17/2006 1:15:00 PM

0

Ray wrote:
> Tim, i just tried this:
>
> Tiger:/opt/local/bin rails$ composite -version
> Version: ImageMagick 6.2.4 02/24/06 Q16 http://www.image...
>
> So i guess I'm running ImageMagick 6.2.4???

Yes, that's right.

>
> Also, to clarify my question around text formatting, I need to set
> formats in line (on particular words), not globally to the caption
> parameter. Can it be done?
>

No. ImageMagick is an image processing library so its support for text
is limited. If you absolutely have to do this using Ruby+ImageMagick
then you might have more luck with RMagick's RVG class. It'll still be
hard. While RVG does allow you to specify styling on individual words,
it doesn't support embedded newlines so you'll have to do the
positioning yourself. Check out RVG's support for text at
http://www.simplesystems.org/RMagick/doc/rv..., with particular
attention to tspans:
http://www.simplesystems.org/RMagick/doc/rvg...

Another approach would be to produce an EPS file, or a PDF (with
PDF::Writer) that contains the formatted text and then convert it to
the destination format with RMagick.

> Cheers and happy St. Patrick's Day, Ray

Thanks! You too!

Tim Hunter

3/18/2006 11:35:00 PM

0

Ray wrote:
> Hi Tim, thanks for your quick response. I'm very new to this, Im on Mac
> OSX 10.4.5, how do I check the version of ImageMagick installed?
>
> Also, when I do get it working, can all text formatting (eg: bold,
> italic, colours, etc) work with the caption parameter?
>
> Ta, Ray
>
Enter this command in irb:

irb -r RMagick
puts Magick::Long_version

The doc on the "caption" builtin format[1] says that you can use these
options with caption:

antialias
background_color
border_color
density
fill
font
gravity
pointsize

font_style and font_weight aren't on the list, so you can't specify
italic or bold with caption. You could specify a bold or italic font by
name instead. (This is a limitation of ImageMagick, not RMagick.) To
specify those kinds of attributes you're going to have to use the
Draw#text method[2] or RVG::Text[3] or the Draw#annotate method[4]. The
annotate method will handle strings with embedded newlines, the other
two don't.

Keep in mind that ImageMagick will not alter a font (the way Windows
slants a font to fake italics, for example) to match the attributes you
specify. It simply searches its list of fonts (which may not include all
the fonts you have on your system) to find the closest match and uses
that. If you ask for a bold Arial and it doesn't have Arial Bold on its
list, it'll just give you Arial. If you absolutely need a certain font,
the best thing to do is to specify the full path to the font. Anthony
Thyssen has a great set of ImageMagick examples, including one about
fonts[5]. The examples use the ImageMagick commands but it's not hard to
map them into the RMagick API. (If you do need help translating the
commands into RMagick send me an email and I'll see what I can do.)

[1]http://www.simplesystems.org/RMagick/doc/imusage.html#built...
[2]http://www.simplesystems.org/RMagick/doc/draw...
[3]http://www.simplesystems.org/RMagick/doc/rv...
[4]http://www.simplesystems.org/RMagick/doc/draw.htm...
[5]http://www.cit.gu.edu.au/~anthony/graphics/imag...

Alex Combas

3/20/2006 8:52:00 PM

0

The rmagick docs look absolutely amazinghttp://www.simplesystems.org/RMagick/doc/index.h... right at the very start the doc basicallysays "if you're on windows then this wont work" and thenprovides no example to show how to make it work.=> Img.write("foo.gif") #for windows users please!Finding out how to get started is usually the hardest part of learninganything new.Geez, thanks, heh.On 3/18/06, Tim Hunter <cyclists@nc.rr.com> wrote:> Ray wrote:> > Hi Tim, thanks for your quick response. I'm very new to this, Im on Mac> > OSX 10.4.5, how do I check the version of ImageMagick installed?> >> > Also, when I do get it working, can all text formatting (eg: bold,> > italic, colours, etc) work with the caption parameter?> >> > Ta, Ray> >> Enter this command in irb:>> irb -r RMagick> puts Magick::Long_version>> The doc on the "caption" builtin format[1] says that you can use these> options with caption:>> antialias> background_color> border_color> density> fill> font> gravity> pointsize>> font_style and font_weight aren't on the list, so you can't specify> italic or bold with caption. You could specify a bold or italic font by> name instead. (This is a limitation of ImageMagick, not RMagick.) To> specify those kinds of attributes you're going to have to use the> Draw#text method[2] or RVG::Text[3] or the Draw#annotate method[4]. The> annotate method will handle strings with embedded newlines, the other> two don't.>> Keep in mind that ImageMagick will not alter a font (the way Windows> slants a font to fake italics, for example) to match the attributes you> specify. It simply searches its list of fonts (which may not include all> the fonts you have on your system) to find the closest match and uses> that. If you ask for a bold Arial and it doesn't have Arial Bold on its> list, it'll just give you Arial. If you absolutely need a certain font,> the best thing to do is to specify the full path to the font. Anthony> Thyssen has a great set of ImageMagick examples, including one about> fonts[5]. The examples use the ImageMagick commands but it's not hard to> map them into the RMagick API. (If you do need help translating the> commands into RMagick send me an email and I'll see what I can do.)>> [1]http://www.simplesystems.org/RMagick/doc/imusage.html#builtin_f... [2]http://www.simplesystems.org/RMagick/doc/draw.htm... [3]http://www.simplesystems.org/RMagick/doc/rvgtex... [4]http://www.simplesystems.org/RMagick/doc/draw.html#an... [5]http://www.cit.gu.edu.au/~anthony/graphics/imagick6/text/>... Combashttp://noodlejunkie.blo...

Tim Hunter

3/20/2006 10:52:00 PM

0

Alex Combas wrote:
> The rmagick docs look absolutely amazing
>
> http://www.simplesystems.org/RMagick/doc/...
>
> However right at the very start the doc basically
> says "if you're on windows then this wont work" and then
> provides no example to show how to make it work.
>
> => Img.write("foo.gif") #for windows users please!
>
> Finding out how to get started is usually the hardest part of learning
> anything new.

Thanks for the kind words, Alex! I guess I need to look to look at that
part of the doc again, because I really don't want to mislead Windows
users. I guess you're talking about this note:

MS Windows users note: The display method does not work on native MS
Windows. You must use an external viewer to view the images you create.

What I'm _trying_ to say is that ImageList#display doesn't work on
native MS Windows, not that Image#write doesn't work.

To clarify the doc, the Image#write method works just fine on MS
Windows, as do all of the other RMagick methods, with the exception of
those methods that need X11. To be specific, the methods that rely on
X11 (and thus don't work on MS Windows) are Image#display,
ImageList#display, ImageList#animate, and Image.capture. And, when I say
"MS Windows" I mean "native" MS Windows, not Cygwin. The difference is
that you can install X11 on Cygwin but not on "native" MS Windows.

Until I get the doc fixed up, you might be able to figure out the
confusing bits by looking at and running some of the example scripts
that ship with RMagick. As always, Ruby makes the code simple and easy
to understand.

Thanks for pointing out this deficiency in the doc. I'll see what I can
do to remedy it!