Adding shadow on word wrapped text image

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
busybee235
Posts: 5
Joined: 2013-06-14T03:00:11-07:00
Authentication code: 6789

Adding shadow on word wrapped text image

Post by busybee235 »

I want to add shadow to text on word wrapped text image. Googled many pages and experimented with gaussian, blur, shadow commands for dropping shadow but no success. I'm a newbie, recently started working with IM.

This is my code:
$line = "Anatidaephobia is the fear that somewhere in the world there is a duck watching you.";
exec("convert -size 500x -background white -pointsize 35 -font arial-italic -gravity center -fill black caption:\"$line\" output.jpg");

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

Re: Adding shadow on word wrapped text image

Post by snibgo »

See examples on http://www.imagemagick.org/Usage/blur/#shadow

For your needs, you could do:

Code: Select all

convert ^
  -size 500x -background None -pointsize 35 -font arial-italic -gravity center -fill black caption:%line% ^
  ( +clone -shadow 80x3+5+5 ) ^
  +swap ^
  -background White ^
  -layers merge +repage ^
  output.jpg
Windows script; adjust as required for your language.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adding shadow on word wrapped text image

Post by snibgo »

Explanation of my script:

"-shadow" takes an image and replaces it with its shadow. The shadow of an opaque rectangular image will be a rectangle. We want shadows of the letters, not of the entire rectangle, so we write the letters on a transparent background ("-background None").

We need to put the "-shadow" within "( +clone ... )" because we want to keep the letters, not to replace them.

We want the letters to appear on top of the shadow (not the shadow on top of the letters), so we "+swap" before "-layers merge". We don't want any transparency in the final image, so we merge the layers with a white background. (Saving to jpg will automatically remove transparency, but by merging with a black background.)

We don't actually need "+repage", because this removes offsets that aren't stored in jpg files. If the output file was changed to be .png, we would have an offset in the file, which we probably would not want.
snibgo's IM pages: im.snibgo.com
busybee235
Posts: 5
Joined: 2013-06-14T03:00:11-07:00
Authentication code: 6789

Re: Adding shadow on word wrapped text image

Post by busybee235 »

Thanks Snibgo, it worked. :D
busybee235
Posts: 5
Joined: 2013-06-14T03:00:11-07:00
Authentication code: 6789

Re: Adding shadow on word wrapped text image

Post by busybee235 »

Hey Snibgo, your code works absolutely fine on my localhost environment but doesn't work on my webhost. After checking I came to know that my hosting providers are providing IM version 5.5.6 Q16 which they installed in 2003. The shadow command doesn't work at all on their servers.

This doesn't works:

Code: Select all

exec("/usr/bin/convert -size 500x -background transparent -pointsize 30 -font verdana-bold -gravity center -fill white caption:\"$line\" ( +clone -shadow 100x2+3+0 ) +swap -background transparent -layers merge +repage output.png");	
This works:

Code: Select all

exec("/usr/bin/convert -size 500x -background transparent -pointsize 30 -font verdana-bold -gravity center -fill white caption:\"$line\" output.png");
Even font verdana-bold not working. It is just plain old text image.
Can you provide any alternative to shadow command for shadow effect?

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

Re: Adding shadow on word wrapped text image

Post by snibgo »

I suggest you ask your hoster to upgrade IM.

You can list the available fonts with "convert -list font".

For shadows without using the "-shadow" command, see http://www.imagemagick.org/Usage/blur/#shadow_internals
snibgo's IM pages: im.snibgo.com
busybee235
Posts: 5
Joined: 2013-06-14T03:00:11-07:00
Authentication code: 6789

Re: Adding shadow on word wrapped text image

Post by busybee235 »

Thanks for replying snibgo, I talked to them, they told it will take a long time and will affect their clients. So, I can't say they will do it!
Can you tell me how can I add a layer of text of different color below like this:
Image to this code:

Code: Select all

exec("/usr/bin/convert -size 500x -background transparent -pointsize 30 -font verdana-bold -gravity center -fill white caption:\"$line\" output.png");
Thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adding shadow on word wrapped text image

Post by snibgo »

Perhaps you can install IM in your user account, and use that. I don't use Unix, and can't advise.

For the "Anthony" example, something like this. I don't have the Candice font, so have used Arial. (Windows script; adjust for your language.)

Code: Select all

%IM%convert ^
  -size 320x100 xc:lightblue  -font Arial -pointsize 72 ^
  -fill Black -draw "text 28,68 'Anthony'" ^
  -fill White -draw "text 25,65 'Anthony'" ^
  text_draw.png
snibgo's IM pages: im.snibgo.com
busybee235
Posts: 5
Joined: 2013-06-14T03:00:11-07:00
Authentication code: 6789

Re: Adding shadow on word wrapped text image

Post by busybee235 »

I have a shared hosting so its impossible to for IM updation.
The code above you provided works with draw command, I want to make it work with below one. I tried many times but couldn't can you please help?

Code: Select all

exec("/usr/bin/convert -size 500x -background transparent -pointsize 30 -font verdana-bold -gravity center -fill white caption:\"$line\" output.png");
Adding a black layer of text and then the above one.

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

Re: Adding shadow on word wrapped text image

Post by snibgo »

Windows script:

Code: Select all

set line="Anatidaephobia is the fear that somewhere in the world there is a duck watching you."

%IM%convert ^
  -size 500x -background None ^
  -pointsize 35 -font arial-italic -gravity center ^
  -fill Black caption:%line% -repage +2+2 ^
  -fill White caption:%line% ^
  -background LightBlue ^
  -layers merge ^
  out.png
snibgo's IM pages: im.snibgo.com
Post Reply