Page 1 of 1

Resize an image so that it fits the inner DrawingWand (Text)

Posted: 2009-01-29T02:54:23-07:00
by DerUnbenannte
Hi there,

I have to generate text-images dynamically and the size of the image have to fit the text-size of course.

That's my code so far:

Code: Select all

      $mw=NewMagickWand();
      $dw=NewDrawingWand();
      $pw['white']=NewPixelWand();
      $pw['black']=NewPixelWand();
      $pw['red']=NewPixelWand();
      PixelSetColor($pw['white'], 'white');
      PixelSetColor($pw['red'], '#C10600');
      PixelSetColor($pw['black'], 'black');

      MagickNewImage($mw, 200, 30, '#C10600');
      // MagickPaintTransparentImage($mw, $pw['red']);
      MagickSetFormat($mw, 'png');

      DrawSetFont($dw, dirname(__FILE__).'/../ClarendonLTStd-Bold.pfb');
      DrawSetFontSize($dw, self::FONT_SIZE);
      DrawSetStrokeColor($dw, $pw['black']);
      DrawSetStrokeWidth($dw, 0.8);
      DrawSetStrokeOpacity($dw, 0);
      DrawSetFillColor($dw, $pw['white']);

      MagickAnnotateImage($mw, $dw, 0, self::FONT_SIZE, 0, 'STARTSEITE');
      header('Content-type: image/png');
      MagickEchoImageBlob($mw);
      die;
and that's the result:
Image
As you can see the image itself is bigger than the text.
How can I resize the image so that it fits the generated text?

Thank you!

Re: Resize an image so that it fits the inner DrawingWand (Text)

Posted: 2009-01-29T08:32:08-07:00
by el_supremo
You could try trimming the image after the annotate:

Code: Select all

MagickTrimImage($mw,0);
If you still want a border around the text you can then add one with MagickBorderImage.

Pete

Re: Resize an image so that it fits the inner DrawingWand (Text)

Posted: 2009-02-02T09:49:33-07:00
by DerUnbenannte
That's it. Thank you very much! ;-)

Re: Resize an image so that it fits the inner DrawingWand (Text)

Posted: 2009-02-04T03:33:49-07:00
by DerUnbenannte
I´ve got another problem now:
When generating the image, my script does not know about the size, but MagickNewImage expects a width and a height.
Is there another way than creating a large "empty" image and finally trimming it?

Thanks.

Re: Resize an image so that it fits the inner DrawingWand (Text)

Posted: 2009-02-04T17:26:07-07:00
by el_supremo
You can use MagickQueryFontMetrics to find out the size of the string before it is drawn and use that as the dimensions of the new image. MagickQueryFontMetrics returns an array of 13 real numbers.
See http://studio.imagemagick.org/api/magic ... ontMetrics

Pete

Re: Resize an image so that it fits the inner DrawingWand (Text)

Posted: 2009-02-05T04:36:25-07:00
by DerUnbenannte
Thanks a lot.

I´ve found out another way for getting the text-dimensions:
MagickGetStringWidth and MagickGetStringHeight

Just FYI ;-)

Re: Resize an image so that it fits the inner DrawingWand (Text)

Posted: 2009-02-05T09:19:06-07:00
by el_supremo
I couldn't find those two functions in the MagickWand code but then I found them in the docs for MagickWand for PHP.
They are convenience functions added to the PHP version and they call MagickQueryFontMetrics for you.

Pete