hw to offset text from the top of an image and give it a text alignment of center

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
dt59
Posts: 49
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 » 2017-08-26T00:48:08-07:00

fmw42 wrote:
2017-08-25T16:35:14-07:00
try this

Code: Select all

<?php
$ou = "magick "; 
$ou .= "(-background white -fill black -gravity center -pointsize 36 label:'my first text' -trim +repage ) ";
$ou .= "(-background white -fill black -gravity center -pointsize 36 label:'center text' -trim +repage ) ";
$ou .= "-gravity center -smush +20 -bordercolor white -border 10 -bordercolor red -border 10 ";
$ou .= "(-size 500x500 xc:white ) +swap -gravity northwest -geometry +57+146 -compose over -composite text_result.png";
exec("$ou 2>&1", $out, $returnval);
foreach($out as $text)
{echo "$text<br>";}
?>
fm, what are u trying to do on line 6 of ur code, there seems to be problem there i mean syntax error

User avatar
fmw42
Posts: 22105
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 » 2017-08-26T10:06:10-07:00

I am sending stderr to stdout and saving (errors) messages in $out. Then use foreach to display that to your web page when running the PHP code.

dt59
Posts: 49
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 » 2017-08-26T13:44:43-07:00

fmw42 wrote:
2017-08-26T10:06:10-07:00
I am sending stderr to stdout and saving (errors) messages in $out. Then use foreach to display that to your web page when running the PHP code.
but the above code is not working. showing me error on that same line. pls help me fm i really need to make these work. if possible just review that ur above code. pls :?

Bonzo
Posts: 2851
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by Bonzo » 2017-08-26T14:03:38-07:00

If you are on a Linux system you need to escape the ( ) like \( \)

snibgo
Posts: 9404
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by snibgo » 2017-08-26T14:44:05-07:00

This line?

Code: Select all

exec("$ou" 2>&1", $out, $returnval);
Quotes usually come in pairs. Perhaps:

Code: Select all

exec("$ou 2>&1", $out, $returnval);
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22105
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 » 2017-08-26T15:09:00-07:00

snibgo wrote:exec("$ou 2>&1", $out, $returnval);
Yes, thanks snibgo. I missed the extra quote.

dt59
Posts: 49
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 » 2017-08-26T20:44:50-07:00

you men are genius worked like charm i can now see my errors and fix them, one after the other. thanks fmw for ur smart code, snibgo and bonzo big thanks. but there seems to be alot errors:

magick: unable to open image '(-background': No such file or directory @ error/blob.c/OpenBlob/3109.
magick: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
magick: no image to apply a property "%w" @ warning/property.c/GetMagickPropertyLetter/2561.
magick: unknown image property "%w" @ warning/property.c/InterpretImageProperties/3499.
magick: no image to apply a property "%h" @ warning/property.c/GetMagickPropertyLetter/2449.
magick: unknown image property "%h" @ warning/property.c/InterpretImageProperties/3499.
magick: no image to apply a property "%m" @ warning/property.c/GetMagickPropertyLetter/2480.
magick: unknown image property "%m" @ warning/property.c/InterpretImageProperties/3499.

i don't understand why the first error is showing; and most importantly the other errors. Can somebody help me pls
Last edited by dt59 on 2017-08-26T21:04:49-07:00, edited 1 time in total.

dt59
Posts: 49
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 » 2017-08-26T20:51:53-07:00

Bonzo wrote:
2017-08-26T14:03:38-07:00
If you are on a Linux system you need to escape the ( ) like \( \)
no, am using window, do i still need to escape if yes hw do i escape

User avatar
fmw42
Posts: 22105
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 » 2017-08-26T21:04:52-07:00

You do not need escapes in your code here. But for windows, escapes are the ^ character.

snibgo
Posts: 9404
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by snibgo » 2017-08-26T22:15:25-07:00

dt59 wrote:magick: unable to open image '(-background'
Like I said upthread:
snibgo wrote:You need a space after each "(".
As you are running PHP, you should follow PHP escaping rules, not Microsoft BAT or CMD escaping rules.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22105
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 » 2017-08-26T22:21:06-07:00

snibgo wrote:You need a space after each "(".
I missed that, too. Thanks, snibgo.

dt59
Posts: 49
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 » 2017-08-27T12:41:55-07:00

its not still working, can somebody please help me fix it. here is code:

Code: Select all

$ou = "magick "; 
$ou .= "(". "-background white -fill black -gravity center -pointsize 36 label:'my first text' -trim +repage" ." ) ";
$ou .= "(". "-background white -fill black -gravity center -pointsize 36 label:'center text' -trim +repage" ." ) ";
$ou .= "-gravity center -smush +20 -bordercolor white -border 10 -bordercolor red -border 10 ";
$ou .= "(". "-size 500x500 xc:white" .")"." +swap -gravity northwest -geometry +57+146 -compose over -composite text_result.png";
//echo $ou.'<br/>';
echo (exec("$ou 2>&1", $out, $returnval).'<br/>');
//var_dump($out);
if ($returnval == '0'){
	echo "Image created sucessfully";
}
else{
	echo '<h1>There was an error when trying to generate images such as:</h1>';
	foreach($out as $text){echo
		"$text<br>";
	}
}


User avatar
fmw42
Posts: 22105
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 » 2017-08-27T13:29:52-07:00

try this. note I put spaces between your ( and the first character. That is what snibgo was trying to tell you.

Code: Select all

<?php
$ou = "magick "; 
$ou .= "( -background white -fill black -gravity center -pointsize 36 label:'my first text' -trim +repage ) ";
$ou .= "( -background white -fill black -gravity center -pointsize 36 label:'center text' -trim +repage ) ";
$ou .= "-gravity center -smush +20 -bordercolor white -border 10 -bordercolor red -border 10 ";
$ou .= "( -size 500x500 xc:white ) +swap -gravity northwest -geometry +57+146 -compose over -composite text_result.png";
exec("$ou 2>&1", $out, $returnval);
foreach($out as $text)
{echo "$text<br>";}
?>
I do not use PHP much so an not sure about all your quotes nor the outer quotes needed by PHP. One of the other PNP users may have to help if this does not work.

dt59
Posts: 49
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 » 2017-08-27T13:57:24-07:00

hm, two errors are still showing :( :

magick: unable to open image 'first': No such file or directory @ error/blob.c/OpenBlob/3109.
magick: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.

what can i do next...

User avatar
fmw42
Posts: 22105
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 » 2017-08-27T14:02:27-07:00

dt59 wrote:
2017-08-27T13:57:24-07:00
hm, two errors are still showing :( :

magick: unable to open image 'first': No such file or directory @ error/blob.c/OpenBlob/3109.
magick: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.

what can i do next...
The first message is coming because it is seeing the text as separate words and ignoring the quotes. So it see the text as "my" and then thinks first is a new image. Sorry I do not know what to tell you. I have heard this happen before on windows PHP, but do not know how to fix it. You could try to escape the single quotes or escape double quotes. Probably the latter. Windows may not like single quotes.

try this

Code: Select all

<?php
$ou = "magick "; 
$ou .= "( -background white -fill black -gravity center -pointsize 36 label:^"my first text^" -trim +repage ) ";
$ou .= "( -background white -fill black -gravity center -pointsize 36 label:^"center text^" -trim +repage ) ";
$ou .= "-gravity center -smush +20 -bordercolor white -border 10 -bordercolor red -border 10 ";
$ou .= "( -size 500x500 xc:white ) +swap -gravity northwest -geometry +57+146 -compose over -composite text_result.png";
exec("$ou 2>&1", $out, $returnval);
foreach($out as $text)
{echo "$text<br>";}
?>
Sorry, I am not a Windows user. So you may need more help from Windows PHP users.

Post Reply