How I can use Line Break like this with imagemagick

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

How I can use Line Break like this with imagemagick

Post by mostafanastary »

Hi,
i found http://stackoverflow.com/questions/3577 ... magemagick

and it have

$draw = new ImagickDraw();
$x = 0;
$y=20;
$angle = 0;
$padding = 10;
$str = "some text for testing of a word wrap in imagemagick";
$str = wordwrap($str, 10,"\r");
$str_array = explode("\n",$str);
foreach($str_array as $line)
$im->annotateImage( $draw, $x, $y+$padding, $angle, $line );
}

to line break, I need to use this code with php and imagemagick and exec command.

any help please?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How I can use Line Break like this with imagemagick

Post by fmw42 »

This code is using Imagick API. It is not directly usable for PHP exec(). You would have to create the equivalent command line code with unix shell or windows batch script to do the looping and extract each word from the list.

This is a simple solution:

Code: Select all

convert -background white -gravity center -fill black -font Arial -pointsize 18 -interline-spacing 10 \
label:"some\ntext\nfor\ntesting\nof\na\nword\nwrap\nin\nimagemagick" result.gif
see
http://www.imagemagick.org/Usage/text/# ... ne-spacing

change the -gravity to west if you want it aligned on the left side.
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

Re: How I can use Line Break like this with imagemagick

Post by mostafanastary »

hi
i have problem like: viewtopic.php?t=19249
i do not want to use /n because i get the text from input form.also i need to change something , but i can use the wordwrap to explode text with /n.

for example : I have a input like:
This code is using Imagick API. It is not directly usable for PHP . You would have to create the equivalent command line code with unix shell or windows batch script to do the looping and extract each word from the list.

i can explode /n it and my result will be:
line1 - This code is using Imagick API. It is not directly usable for PHP .
line2 - You would have to create the equivalent command line code with unix shell or
line3 - windows batch script to do the looping and extract each word from the list.

so i need to draw first line 3 then line 2 and then line1

is it possible?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How I can use Line Break like this with imagemagick

Post by Bonzo »

Code: Select all

$draw = new ImagickDraw();
 $x = 0;
 $y=20;
 $angle = 0;
 $padding = 10;
 $str = "some text for testing of a word wrap in imagemagick";
 $str = wordwrap($str, 10,"\r");
 $str_array = explode("\n",$str);
 foreach($str_array as $line)
 $im->annotateImage( $draw, $x, $y+$padding, $angle, $line );
 }
You can use most of the above code as it is; you need to create your canvas and then write the text. I would concat the text into a string in the foreach adding an -annotate +0+{$padding} to each new line and use that in your imagemagick command.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How I can use Line Break like this with imagemagick

Post by fmw42 »

I am not sure I understand your problem and the reference you provided. If it has to do with UTF8 characters, I believe you asked that before and got answers.

If you do not want to manually insert \n, then you can automate that. Try this ($text can come from your form element). This is just PHP exec compatible. If you need to use Imagick, then do what user bonzo suggested above.

Code: Select all

$text="some text for testing of a word wrap in imagemagick"
$newtext=str_replace(" ", "\n", $text);
exec("convert -background white -gravity center -fill black -font Arial -pointsize 18 -interline-spacing 10 \
label:"."$newtext"." result.gif");
Some PHP expert should correct my quoting in the exec command if it is wrong.
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

Re: How I can use Line Break like this with imagemagick

Post by mostafanastary »

Hi fred, you true, i got my answer, but this topic is about line auto break in imagemagick, thank you for answer and thanks bonzo.
i will try that.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How I can use Line Break like this with imagemagick

Post by fmw42 »

You can do automatic line breaks (not word breaks) by using caption: or pango: instead of label:

see
http://www.imagemagick.org/Usage/text/#caption
http://www.imagemagick.org/Usage/text/#pango

try this:

Code: Select all

text="some text for testing of a word wrap in imagemagick"
convert -background white -size 200x -gravity center -fill black -font Arial -pointsize 18 -interline-spacing 10 \
caption:"$text" result.gif
You just need to specify the width and pointsize and IM will automatically break the line where needed.
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

Re: How I can use Line Break like this with imagemagick

Post by mostafanastary »

it's true, but also i told already, in need draw lines, revers... first 3, then 2, then 1
i found a way to draw persian or arabic text without pango , just imagemagick and some php code, i will share that with imgaemaick ORG :) very soon,
i hope that use full.

best regards.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How I can use Line Break like this with imagemagick

Post by fmw42 »

but also i told already, in need draw lines
I have no idea what lines you want to draw. If you mean rows of text, that is what caption: did. If you want to draw a word at a time, then the label process works just fine.

So I am not sure what kind of result you are trying to achieve.

If you have found a way to do that, then please show us your code and resulting image.
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

Re: How I can use Line Break like this with imagemagick

Post by mostafanastary »

i mean row, in persian we say line :)

but the some result with : aban.ttf (farsi font ) and with imagemagick without pango or etc... :

Image

hejaz.ttf:

Image

ahmad.ttf:

Image


if let me, i share code on the end.

best regards.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How I can use Line Break like this with imagemagick

Post by fmw42 »

mostafanastary wrote:i mean row, in persian we say line :)

but the some result with : aban.ttf (farsi font ) and with imagemagick without pango or etc... :

Image

hejaz.ttf:

Image

ahmad.ttf:

Image


if let me, i share code on the end.

best regards.

What does this have to do with your original question at the top of this topic, regarding the code you provided? Please keep the posts to one subject; otherwise, it gets very confusing for people to answer your questions. Did you resolve the original question?
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

Re: How I can use Line Break like this with imagemagick

Post by mostafanastary »

but topic is about line break fred,
I open a new topic and explain about the code.
thank you
mostafanastary
Posts: 44
Joined: 2014-11-28T16:19:41-07:00
Authentication code: 6789

Re: How I can use Line Break like this with imagemagick

Post by mostafanastary »

what is your idea about the results?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How I can use Line Break like this with imagemagick

Post by fmw42 »

Sorry, I am confused. I do not see any line breaks in your examples above. So I do not connect those results to the title of your topic.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How I can use Line Break like this with imagemagick

Post by snibgo »

Pango wordwrap (automatic newlines) seems to work correctly for Arabic. For example (Windows BAT syntax):

Code: Select all

convert ^
  -size 200x200 -gravity Center ^
  -background khaki ^
  -font Arial -pointsize 50 ^
  pango:"صَوْرة سِحْر" ^
  u8_pa_a2ww.png
Image
snibgo's IM pages: im.snibgo.com
Post Reply