Write text to a .png with php

August
31

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]
// load the image from the file specified:

$im = imagecreatefrompng(“button.png”);
// if there’s an error, stop processing the page:
if(!$im)
{
die(“”);
}
// define some colours to use with the image
$yellow = imagecolorallocate($im, 255, 255, 0);
$black = imagecolorallocate($im, 0, 0, 0);
// get the width and the height of the image
$width = imagesx($im);
$height = imagesy($im);
// draw a black rectangle across the bottom, say, 20 pixels of the image:
//imagefilledrectangle($im, 0, ($height-20) , $width, $height, $black);
// now we want to write in the centre of the rectangle:
$font = 4; // store the int ID of the system font we’re using in $font
$text = $_GET['text']; // store the text we’re going to write in $text
// calculate the left position of the text:
$leftTextPos = ( $width – imagefontwidth($font)*strlen($text) )/2;
// finally, write the string:
imagestring($im,$font,$leftTextPos-1, $height-35,$text,$black);
imagestring($im, $font, $leftTextPos, $height-36, $text, $yellow);

// output the image
// tell the browser what we’re sending it
Header(‘Content-type: image/png’);
// output the image as a png
imagepng($im);
// tidy up
imagedestroy($im);
[/php]

yey!

Code


0 Comments zu “Write text to a .png with php”

  1. jalaj
    31.10.07 4:44 am

    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

    #1

  2. Andy Bailey
    31.10.07 10:30 am

    3 years ago?? wow, you must be really advanced now! have you made any programs for wordpress?

    #2

  3. Secret Cash System Business
    20.02.08 2:47 am

    Cellular Ties in to Business Phone System…

    The roving sales reps at my company have something to be happy about. We recently updated our phone system to allow them to use the full resources of our office phone system from their cell phones….

    #3

  4. Reviewlot
    04.07.08 5:32 pm

    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

  5. Goodpennystock
    04.07.08 9:04 pm

    Thanks a lot for sharing I will be trying this.

    Goodpennystocks last blog post..Penny stocks lists

    #5