Generating text with glow and stroke

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?".
Post Reply
mezz

Generating text with glow and stroke

Post by mezz »

Hey, Wondering if anyone can lend a further hand, already had some fantastic help on another forum to get so far but if anyone else can assist that would be great :)

Im basicaly trying to generate some text using my own font and then apply a dark red stroke and a black glow effect around the outside of the text.

So far my code creates this result.

Image

and this is what I would like to generate

Image

Heres my code so far

Code: Select all

	exec("convert -size 200x50 xc:none -stroke red -strokewidth 1 -font /home/###/lib/fonts/havoc.ttf -pointsize 35 -draw \"gravity center fill white text 0,-5 \"Username\" \" temp.png 2>&1", $array); 
	echo "<br>".print_r($array)."<br>";
	$array=array();
	echo "</pre>";
	
	// Create the shadow
	exec("convert -background none -fill white temp.png -shadow 80x5+0+0 -channel A -evaluate multiply 5 +channel -trim shadow.png 2>&1", $array); 
	echo "<br>".print_r($array)."<br>";
	$array=array();
	
	// Combine the shadow and text
	exec("convert shadow.png temp.png -gravity center -composite temp2.png 2>&1", $array); 
	echo "<br>".print_r($array)."<br>";
	$array=array();

	$size = getimagesize ("temp2.png");
	$width = $size[0]+50;
	$height = $size[1]+20;
	
	// Place the text image onto a background that is 50px wider and 20px higher than the text
	exec("convert -size {$width}x{$height} xc:#262422 temp2.png -gravity center -composite mezzi.jpg 2>&1", $array); 
	echo "<br>".print_r($array)."<br>";
	
	echo '<img src="mezzi.jpg"/>';
	
	unlink("temp.png");
	unlink("shadow.png");
Im getting this error though, the image generates without the stroke and the shadow is verticaly aligned lower then the text not exactly behind it

convert: Memory allocation failed `transparent'

Any ideas from anyone ? :)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Generating text with glow and stroke

Post by anthony »

I suggest you simplify it to basic commands. rather than expect us to code what you have. Also giving us a location to download the 'havoc' font can be a big help.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mezz

Re: Generating text with glow and stroke

Post by mezz »

Hey,

I dont really expect the whole thing coded for me, the majority of it works theres just some errors that I was hoping someone could maybe spot and give me pointers on? I cant see them myself since Im new to imagemagick, Ive read over the pages of commands etc and done a lot of google searches before posting here.

The font can be downloaded here http://www.matt-adamson.com/havoc.ttf

I know theres a problem with the glow and my font because if I just use something like verdana the glow appears fine, its just when I change to any other font like havoc.ttf that the glow disapears again.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Generating text with glow and stroke

Post by anthony »

You really should have simplified.

Your problem is you tried to use double quotes within -draw double quotes
withing the PHP double quotes!!! the result was no quotes around 'username'.
In other words you did not add the right number of backslashes, and are in a 'backslash storm'.

Instead of -draw use -annotate instead -- works a LOT better, and no third layer of quoting needed. Also i suggest using single quotes rather than trying to use another set of double quotes!

convert -size 200x50 xc:none -stroke red -font havoc.ttf -pointsize 35 -gravity center -fill white -annotate +0-5 'Username' temp.png
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mezz

Re: Generating text with glow and stroke

Post by mezz »

Hey again thanks for your help and sorry for not simplyfying I just had no idea where the error was because the code was actualy outputting text, just not with the stroke.

I get what you mean about the quotes so Ive corrected that thanks.

Going to look up annotate in the documentation see if I can learn bit more about using it.

Even though I made those changes I still get that error showing and still no red stroke :S Even though the error shows it doesnt seem to stop it outputting the text, I guess maybe it stops it applying stroke though as its not there?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Generating text with glow and stroke

Post by anthony »

Especially look at IM Examples...
http://www.imagemagick.org/Usage/text/#annotate
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mezz

Re: Generating text with glow and stroke

Post by mezz »

Still no luck with this even after reading all the documentation. The stroke still doesnt appear.

Does no one have any idea on why it might not be showing?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Generating text with glow and stroke

Post by anthony »

It appears perfectly for me with that font.
I suggest you use -annotate instead of -draw.

NOTE: for some reason the internal name of havoc is "bagpack"
which is what my font type list generator picks up.
I don't think havoc is a very nicely created font file
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mezz

Re: Generating text with glow and stroke

Post by mezz »

I changed to annotate when you originaly mentioned it :) Still has the same problem though, also I renamed the font file to match its internal name and pointed the script to this instead, no luck though :( Im running out of ideas here. Like you said it appears perfect so should work but just doesnt :S
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Generating text with glow and stroke

Post by anthony »

The only reason the stroke should not appear is if you used a 'none' or transparent stroke color, or 'white'. Even having a strokewidth of 0 does not make the stroke disappear.

The default stroke color is of course fully transparent or undefined.

The font is coming out so it is not a font issue, leaving option settings and IM version, but it would have to be extremely old for that to be an issue.

As such I am at a loss myself. You said it works on the command line, for that machine right. It only does not work for PHP exec. these are different environments, could you have two IM versions installed, can you try using the full path name for the 'convert' command. can you output "convert -version" and compare that to the same command on the command line of the same machine?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mezz

Re: Generating text with glow and stroke

Post by mezz »

Hey thanks for replying again.

I dont think I mentioned the command line ? I havent tried it as I rent a server and I only have access via php I think.

Strange thing is surely its not a version issue as if I change the font from havoc.ttf /bagpack.ttf to something like arial, the stroke works fine, when I set it back to my custom font the stroke disapears. I also tested with another font I downloaded and the stroke appears on that fine, so it appears its just this font that wont display the stroke :S

Do you know of anything that would prevent IM from creating a stroke around a font? It must be the font Im using but I really need to use this specific one :S
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Generating text with glow and stroke

Post by anthony »

mezz wrote:I dont think I mentioned the command line ? I havent tried it as I rent a server and I only have access via php I think.
Doesn't matter, you are executing IM via the command line interface. The PHP wrapper however provided you with the ability to use temporary files, and do extra calculations outside of IM proper. It is a good mix.

I don't think the name is important, just pointing it out as an indication that the font many not be nicely formed.

I outlined all the posible reasons the stroke may not be visible. However it it works for one font, and you only change the font, then it should work for that new font. If it doesn't then it is a font problem. However as I have see it working for the new font, then it could not be the font so something else is different.

What works for one TTF font, should work perfectally for another TTF font!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply