Page 1 of 1

Geography Not Working After Switching Servers

Posted: 2011-02-24T17:33:27-07:00
by gotzenn
Hi everyone! I've got a system built that creates dynamic ads for users, and places their contact info, headline & text on the ad and saves it as a new image. It's worked flawlessly for several years now, but I just moved it to a new hosting server (previously on MediaTemple Grid), and it's not behaving the same way. Specifically, all of the text displays correctly except one - the "caption" element, which shows up on the top-left of the image regardless of the "geometry" settings that I specify. All the other text elements are using the draw command, as they are single-line. But the body text must wrap, therefore it uses the "caption" command. This happens on every single ad - they all work except the body text.

I checked, and both servers are using the same version of ImageMagick (I intentionally installed the older version on the new server to match):
Old Server: ImageMagick 6.2.4 09/16/06 Q16 http://www.imagemagick.org
New Server: ImageMagick 6.2.4 09/17/10 Q16 http://www.imagemagick.org

Below is the code I'm using, which works fine on the old server, and again it renders on the new server, but with the body text in the top left. I've replaced the dynamic PHP variables with actual text & numbers so you can see it better.

Code: Select all

exec("/usr/bin/convert -size '1275'x'1650' xc:white color_full1.png -geometry '1275'x'1650'!+0+0 -composite -fill black -font Arial.ttf -pointsize 48 -gravity Northwest -draw \"text 45,830 'HEADLINE GOES HERE'\" -font AvantGarde-Demi -pointsize 22 -gravity South -draw \"text 0,100 ' ADDRESS GOES HERE '\" -font AvantGarde-Demi -pointsize 34 -gravity Northwest -draw \"text 600,1000 'BULLET POINTS GO HERE'\" -font AvantGarde-Book -pointsize 20 -background transparent -size 480x -gravity Northwest -geometry '1275'x'1650'+45+950 caption:'BODY TEXT GOES HERE' -composite $savelocation");
I've tried a number of different techniques to get Geometry working again, but no matter what, it refuses to place the text at the coordinates I specify. I really need to get it working, otherwise I'll have to reprogram the entire system (and hundreds of ads) from scratch using GD, which I really don't want to have to do. Any help would be appreciated!

Re: Geography Not Working After Switching Servers

Posted: 2011-02-24T18:37:28-07:00
by fmw42
I am no expert on PHP but seems that you should not need quotes around numbers and especially with '1275'x'1650'!+0+0. If you need quotes, try putting them around the whole expression, such as '1275x1650!+0+0'

Just be sure there are no spaces in such expressions.

Re: Geography Not Working After Switching Servers

Posted: 2011-02-24T18:52:53-07:00
by gotzenn
Thanks for the tip! Those quotes are actually removed when the command is run - they were left over from when I replaced the PHP variables with real text, and are needed for telling the script to interpret the PHP variable.

Re: Geography Not Working After Switching Servers

Posted: 2011-02-24T18:55:20-07:00
by gotzenn
Here's the actual code, without the PHP quotes, to avoid any confusion:

Code: Select all

exec("/usr/bin/convert -size 1275x1650 xc:white color_full1.png -geometry 1275x1650!+0+0 -composite -fill black -font Arial.ttf -pointsize 48 -gravity Northwest -draw \"text 45,830 HEADLINE GOES HERE\" -font AvantGarde-Demi -pointsize 22 -gravity South -draw \"text 0,100 ADDRESS GOES HERE\" -font AvantGarde-Demi -pointsize 34 -gravity Northwest -draw \"text 600,1000 BULLET POINTS GO HERE\" -font AvantGarde-Book -pointsize 20 -background transparent -size 480x -gravity Northwest -geometry 1275x1650+45+950 caption:BODY TEXT GOES HERE -composite NEWFILE.JPG");

Re: Geography Not Working After Switching Servers

Posted: 2011-02-24T18:58:58-07:00
by anthony
Also using -draw for drawing text is a PAIN. as you have quotes within quotes.

Worse with PHP is that you have multiple levels of quoting to handle..

PHP exec("....") quoting, The shell argument quoting, and with -draw more quotes inside the argument.

You are basically really asking for trouble!


Also looking at the 'caption element'...

Code: Select all

   ... -font AvantGarde-Book -pointsize 20 -background transparent -size 480x \
   -gravity Northwest -geometry '1275'x'1650'+45+950 caption:'BODY TEXT GOES HERE' \
   -composite  ...
Why you are mixing up your settings for caption; and for -composite.
Also that -geometry specification in that location would probably be resizing the 'image before' the caption and not the caption itself!!!

Geometry does two things..
  • resize the current 'last image' (caption has not been created yet)
  • and save the 'offset' for gravity to be used later by composite
Because of this 'last image' handling in "Convert", using -geometry for resizing is not recommended.
See http://www.imagemagick.org/Usage/resize/#geometry

Note also the 'label with a size' is gravity effected! So that gravity setting applies to BOTH!
http://www.imagemagick.org/Usage/text/#label_bestfit

Also why do you specify a label size, then try to use geometry to resize the result! that is weird!

My best suggestion is re-order the geometry to AFTER the caption is created, but before -composite. Also set the settings you want for label and those for composition, separately, in the appropriate positions before those operations. Try NOT to mix them up as you have done.

Remember IM handling all settings and operations in the order it sees them. Settings remain set from one operation to the next until modified.

Re: Geography Not Working After Switching Servers

Posted: 2011-02-24T19:28:47-07:00
by gotzenn
Hi Anthony, Thanks so much for your response! I'll definitely try to reorder some of the commands to see if it makes a difference. Unfortunately there's no way for me to get around the PHP quotes issue, since I have to be able to generate the image from a user-submitted form. But that part is working ok anyway - it's only the caption text that doesn't work, at the very end of the command.

Code: Select all

-size 480x -gravity Northwest -geometry 1275x1650+45+950 caption:BODY TEXT GOES HERE -composite
You mentioned that geometry isn't designed for text, but the only reason I have it is because I need to specify the location to draw the caption (in this case 45 pixels from the left, and 950 pixels from the top). The "size" setting is specifying the width, so that my text wraps at 480 pixels and doesn't overlap other elements on the page. Is there another way to do that without using geometry?

But I guess my main question is that even though this code may not be the best way to write it (admittedly I created it several years ago when ImageMagick was brand new to me), it still has worked perfectly for me the past few years on my previous hosting server. So I was sort of hoping someone would be able to tell me what could cause the identical command to work on one server but not another, with the same version of ImageMagick installed.

Re: Geography Not Working After Switching Servers

Posted: 2011-02-24T20:34:15-07:00
by anthony
gotzenn wrote:Hi Anthony, Thanks so much for your response! I'll definitely try to reorder some of the commands to see if it makes a difference. Unfortunately there's no way for me to get around the PHP quotes issue, since I have to be able to generate the image from a user-submitted form. But that part is working ok anyway - it's only the caption text that doesn't work, at the very end of the command.

Code: Select all

-size 480x -gravity Northwest -geometry 1275x1650+45+950 caption:BODY TEXT GOES HERE -composite
You mentioned that geometry isn't designed for text, but the only reason I have it is because I need to specify the location to draw the caption (in this case 45 pixels from the left, and 950 pixels from the top). The "size" setting is specifying the width, so that my text wraps at 480 pixels and doesn't overlap other elements on the page. Is there another way to do that without using geometry?
It wasn't the PHP quotes but the need for -draw to also use two levels o quoting (shell and internal)
-annotate does the same function, but ony has one level of quoting for the two. It was actually why it was
created. however -annotate does do 'percent escapes' and '@file' reading, where draw does not. On the other hand any time IM reads from a file it will never to any percent substitutions so as to provide a safe way for text handling.

I do hope you sanitize all possible user inputs for 'unusual characters', including quotes, and handle them appropriately. See... http://xkcd.com/327/

Getting back to the problem. I can understand your desire to word-wrap the user text.

-size 480x -gravity Northwest caption:'BODY TEXT GOES HERE' \
-geometry +45+950 -composite

If you want a larger text, increase the density, pointsize, and size settings as appropriate for your images resolution, text line spacing, and word-wrap width, respectively.

NOTE I removed the resize parts of the -geometry setting, as they should not be needed for resizing text.

Re: Geography Not Working After Switching Servers

Posted: 2011-02-24T22:55:48-07:00
by gotzenn
Anthony, You're a genius!! That worked! Thank you so much for your help - you just saved me hours upon hours of reprogramming...