tags:

views:

2032

answers:

3

I want to create a small function in PHP which takes in arguments like color, shape, transparency etc. and outputs a PNG image. I heard about PHP GD library but I want to know how can one create something as creative as soon.media.mit.edu

+5  A: 
Sam152
hey! that was really cool. I never knew this could be achieved using these functions.
apnerve
Oh yeah, this is the tip of the iceberg. There is so much out there.
Sam152
+3  A: 

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" />
niaher
A: 

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.

SpliFF