Dynamic Images with PHP

You may or may not have noticed that my headerchanges slightly everytime you load the page. If you want to check it out, go ahead I’ll wait.

Ok, so now that you have checked it out, I will show you how it is done using php (I’m sure that some of you are curious).

Here is the code:

Most of it is self explanatory, but some of the interesting lines are 9, where we create the image with the set width and height and store it in a variable called $im, then on 11 we fill the image with an rgb value (0,0,0) in this case. Line 14 then creates a variable using teh ImageColorAllocate function, which was previously used to fill the background. This variable is later used to colour the text that is added to the image. We also have to tell the browser what kind of image it is (line 16), this unfortunately means that it is more difficult to add to a webpage (without it spitting out header already sent errors). To get around those errors, you display the image using:

 

Finally we built the rest of the image on line 25 and display it on 27 using the ImagePNG function (because we told the browser it was a png on line 16). Nice and simple.


Related posts:

  1. RE: Top 10 PHP Techniques That Will Save You Time and Effort
  2. Finding corrupted images
  3. Making isset Recursive
  4. Documenting PHP Code
  5. Why implement PHP in Java?

Tags: , , , , , , , , , ,

  • http://syrbot.com Greg

    where’s the beef? i meant the code?
    http://blog.seanja.com/2008/06/dynamic-images-with-php/

    • http://blog.seanja.com/ SeanJA

      That is a good question… it seems to have gone missing, I shall have to try and find it.

  • http://seanja.com SeanJA

    All fixed!

  • http://seanja.com SeanJA

    Man, I should really test these things out locally before putting them on my site… the problem is that the header image needs to be absolutely referenced apparently, so that the image shows up…