Archive for January, 2008

Typesetting

Ever had the situation where you have a piece of string which you need to overlay on an image? Maybe a situation where the area reserved for the string is known in pixels but you need to know the font size to fill most of the area? Think no more!

Here is a small example of how to fit a certain piece of a string on to an area of which you know the width and the height or only the width. The magic happens through the ImageMagick CAPTION: format. You can see from the example images how the parameters actually affect the image.

PHP:
  1. <?php
  2.  
  3. /* How wide is our image */
  4. $image_width = 200;
  5.  
  6. /* Give zero for autocalculating the height */
  7. $image_height = 200;
  8.  
  9. /* Specify the text */
  10. $text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  11.          Mauris lectus mi, mattis non, euismod vel, sagittis nec, ipsum.";
  12.  
  13. /* Instanciate imagick */
  14. $im = new Imagick();
  15.  
  16. /* Create new image using caption: pseudo format */
  17. $im->newPseudoImage( $image_width, $image_height, "caption:" . $text );
  18.  
  19. /* Put 1px border around the image */
  20. $im->borderImage( 'black', 1, 1 );
  21.  
  22. /* PNG format */
  23. $im->setImageFormat( "png") ;
  24.  
  25. /* Output */
  26. header( "Content-Type: image/png" );
  27. echo $im;
  28.  
  29. ?>

Here is image with width 100 and height 0:

width_100_height_0.png

Width 100 Height 50:

width_100_height_50.png

Width 200 Height 200 (as you can see the font size is now larger):

width_200_height_200.png

Padding thumbnail with color

I know, it's been a while since I last blogged. This is because a lot of things are happening in my personal life. I recently relocated to London from Finland and started a new job. Things are quite busy but I will try to post an example now and then. In the meanwhile I would like to hear about sites using Imagick, so if your project is not super secret please post an url and maybe a small explanation what you're doing with Imagick on the site. This is purely for my personal interest.

Anyway, to the point. Today's example originates from a question asked by a user. How do I thumbnail the image inside given dimensions proportionally and fill the "blank" areas with a color? Well, the answer is here :)

The code is for Imagick 2.1.0 but adapting to older versions should not be hard.

PHP:
  1. <?php
  2. /* Define width and height of the thumbnail */
  3. $width = 100;
  4. $height = 100;
  5.  
  6. /* Instanciate and read the image in */
  7. $im = new Imagick( "test.png" );
  8.  
  9. /* Fit the image into $width x $height box
  10. The third parameter fits the image into a "bounding box" */
  11. $im->thumbnailImage( $width, $height, true );
  12.  
  13. /* Create a canvas with the desired color */
  14. $canvas = new Imagick();
  15. $canvas->newImage( $width, $height, 'pink', 'png' );
  16.  
  17. /* Get the image geometry */
  18. $geometry = $im->getImageGeometry();
  19.  
  20. /* The overlay x and y coordinates */
  21. $x = ( $width - $geometry['width'] ) / 2;
  22. $y = ( $height - $geometry['height'] ) / 2;
  23.  
  24. /* Composite on the canvas  */
  25. $canvas->compositeImage( $im, imagick::COMPOSITE_OVER, $x, $y );
  26.  
  27. /* Output the image*/
  28. header( "Content-Type: image/png" );
  29. echo $canvas;
  30.  
  31. ?>

The source image:
test.png

The resulting image:
testphp.png