Create a dynamic PNG image Create a dynamic PNG image php php

Create a dynamic PNG image


This is a good example, you can do virtually everything using these functions. While possible, creating an image like the one you described would be pretty hard by I have done some weird stuff with gradients, loops and colors though.

If you wanted to make an image like that dynamically based on some parameters you can always create the images beforehand in photoshop and then overlay them based on what a user selects.

There is a lot of fun you can have.

Edit: Oh by the way, if your interested giving an invalid parameter shows some of the python code that is responsible for creating the image and causing the error. It would be a good place to get some idea of the code.

2nd Edit: This is just something I have done with this sort of technology. Bear in mind it was quite a while ago. It accepts a name based on the query string and basically does a few loops with a lot of random numbers.

Here is the source code, I apologize for any stupid code/quotes. This was written quite a while ago, when I was about 14 I believe (probably many flaws).

<?phpheader("Content-type:image/jpeg");$array=array("I am a monument to all your sins", "Currently making pizza","Best before 12/7/09", "Farming Onions");        function imagettftext_cr(&$im, $size, $angle, $x, $y, $color, $fontfile, $text)        {            // retrieve boundingbox            $bbox = imagettfbbox($size, $angle, $fontfile, $text);            // calculate deviation            $dx = ($bbox[2]-$bbox[0])/2.0 - ($bbox[2]-$bbox[4])/2.0;         // deviation left-right            $dy = ($bbox[3]-$bbox[1])/2.0 + ($bbox[7]-$bbox[1])/2.0;        // deviation top-bottom            // new pivotpoint            $px = $x-$dx;            $py = $y-$dy;            return imagettftext($im, $size, $angle, $px, $y, $color, $fontfile, $text);        }$image = imagecreate(500,90);$black = imagecolorallocate($image,0,0,0);$grey_shade = imagecolorallocate($image,40,40,40);$white = imagecolorallocate($image,255,255,255);$text = $array[rand(0,sizeof($array)-1)];// Local font files, relative to script$otherFont = 'army1.ttf';$font = 'army.ttf';if($_GET['name'] == ""){ $name = "Sam152";}else{$name= $_GET['name'];}$name = substr($name, 0, 25);    //BG text for Namewhile($i<10){imagettftext_cr($image,rand(2,40),rand(0,50),rand(10,500),rand(0,200),$grey_shade,$font,$name);$i++;}//BG text for sayingwhile($i<10){imagettftext_cr($image,rand(0,40),rand(90,180),rand(100,500),rand(200,500),$grey_shade,$otherFont,$text);$i++;}// Main Textimagettftext_cr($image,35,0,250,46,$white,$font,$name);imagettftext_cr($image,10,0,250,76,$white,$otherFont,$text);imagejpeg($image);?>


Here's the code that I used before to generate an image with two names, which are accepted from query string parameters. I use a prepared background image and put the names on top of it.

<?php// Print two names on the picture, which accepted by query string parameters.$n1 = $_GET['n1'];$n2 = $_GET['n2'];Header ("Content-type: image/jpeg");$image = imageCreateFromJPEG("images/someimage.jpg");$color = ImageColorAllocate($image, 255, 255, 255);// Calculate horizontal alignment for the names.$BoundingBox1 = imagettfbbox(13, 0, 'ITCKRIST.TTF', $n1);$boyX = ceil((125 - $BoundingBox1[2]) / 2); // lower left X coordinate for text$BoundingBox2 = imagettfbbox(13, 0, 'ITCKRIST.TTF', $n2);$girlX = ceil((107 - $BoundingBox2[2]) / 2); // lower left X coordinate for text// Write names.imagettftext($image, 13, 0, $boyX+25, 92, $color, 'ITCKRIST.TTF', $n1);imagettftext($image, 13, 0, $girlX+310, 92, $color, 'ITCKRIST.TTF', $n2);// Return output.ImageJPEG($image, NULL, 93);ImageDestroy($image);?>

To display the generated image on the page you do something like this:

<img src="myDynamicImage.php?n1=bebe&n2=jake" />


Not a direct answer to "doing it in PHP" but you can call some powerful command-line software from PHP. In particular ImageMagick will draw everything including the kitchen sink. It also has the advantage of being available to "back-end" scripts for "out-of-band" processing (ie, performing image processing after the request completes (faster user feedback) or late at night in batches when resources are tight during peak times.