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

MagickWand for PHP is an object-oriented PHP interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning MagickWand for PHP.
Post Reply
DerUnbenannte

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

Post 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!
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

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

Post 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
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
DerUnbenannte

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

Post by DerUnbenannte »

That's it. Thank you very much! ;-)
DerUnbenannte

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

Post 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.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

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

Post 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
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
DerUnbenannte

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

Post by DerUnbenannte »

Thanks a lot.

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

Just FYI ;-)
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

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

Post 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
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Post Reply