Write text to a .png with php

I just read a wicked tutorial on DiscoMoose on how to add text onto an existing png file and now I'm ready to make my next widget! it's going to be a custom link box with a variety of button styles to choose from. Choose any text and the button will be displayed with the text on there.

It might save a lot of trouble for newbies who don't have experience in coding or if someones best image app is MSPaint!

here's my first test to see if I could run the code..

dynamic button

it is called with http://www.fiddyp.co.uk/internal/testingphp/png.php?text=woot!

here is the code for png.php

PHP:
  1. // load the image from the file specified:
  2.  
  3. $im = imagecreatefrompng("button.png");
  4. // if there's an error, stop processing the page:
  5. if(!$im)
  6. {
  7.     die("");
  8. }
  9. // define some colours to use with the image
  10. $yellow = imagecolorallocate($im, 255, 255, 0);
  11. $black = imagecolorallocate($im, 0, 0, 0);
  12. // get the width and the height of the image
  13. $width = imagesx($im);
  14. $height = imagesy($im);
  15. // draw a black rectangle across the bottom, say, 20 pixels of the image:
  16. //imagefilledrectangle($im, 0, ($height-20) , $width, $height, $black);
  17. // now we want to write in the centre of the rectangle:
  18. $font = 4; // store the int ID of the system font we're using in $font
  19. $text = $_GET['text']; // store the text we're going to write in $text
  20. // calculate the left position of the text:
  21. $leftTextPos = ( $width - imagefontwidth($font)*strlen($text) )/2;
  22. // finally, write the string:
  23. imagestring($im,$font,$leftTextPos-1, $height-35,$text,$black);
  24. imagestring($im, $font, $leftTextPos, $height-36, $text, $yellow);
  25.  
  26. // output the image
  27. // tell the browser what we're sending it
  28. Header('Content-type: image/png');
  29. // output the image as a png
  30. imagepng($im);
  31. // tidy up
  32. imagedestroy($im);

yey!

Popularity: 11% [?]

4 Comments

  1. jalaj (20 comments.)
    Posted October 31, 2007 at 4:44 am | Permalink

    Hi Andy
    That’s a nice and small piece of code (on linked page). With regards to the comment posted on my blog, here is where I find the VB family lacking. While the same functionality in ASP requires third party (and often commercial) components, the free GD library (available on nearly all installations) for PHP does this in breeze… So it’s no surprise that I left conding in ASP for to start on PHP 3 years ago.

    jalaj’s last blog post..The Blog Revisited - 5

  2. Posted October 31, 2007 at 10:30 am | Permalink
    3 years ago?? wow, you must be really advanced now! have you made any programs for wordpress?
  3. Reviewlot (1 comments.)
    Posted July 4, 2008 at 5:32 pm | Permalink

    Wow this is great. I will give it a try. I really like your blog as well, definitely will check other parts of it.

  4. Goodpennystock (1 comments.)
    Posted July 4, 2008 at 9:04 pm | Permalink

    Thanks a lot for sharing I will be trying this.

    Goodpennystocks last blog post..Penny stocks lists

One Trackback

  1. By Trackbacks (0 comments.) on August 20, 2008 at 9:53 am

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*