GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Friday, March 11, 2011

PHP - Start GD Library

The code below is just draw a black png image to browser:
<?php
header('Content-type: image/png');

// initialization 250x300 pixels
$img = imagecreate(250,300);

// backgrouning
$black = imagecolorallocate($img, 0,0,0);

imagepng($img);
imagedestroy($img);
?>


To add a string, just do

<?php
header('Content-type: image/png');

// initialization 250x300 pixels
$img = imagecreate(250,300);

// backgrouning
$black = imagecolorallocate($img, 0,0,0);

$text_color = imagecolorallocate($img, 0, 15, 250);
// nan, nan, left, top, text, color
imagestring($img, 2, 5, 5, "A simple text string", $text_color);

imagepng($img);
imagedestroy($img);
?>

Other Sample:

<?php
header('Content-type: image/png');
$img = imagecreate(900,700);

$lime = imagecolorallocate($img, 200, 250, 0);

//drawing string
$text_color = imagecolorallocate($img, 10, 10, 10);
imagestring($img, 12, 65, 150, "Lucia", $text_color);

//drawing rectangle box
$rect_color = imagecolorallocate($img, 10, 250, 10);
imagerectangle($img, 0, 0, 50, 100, $rect_color);

//drawing solid rectangle box
$rect_color = imagecolorallocate($img, 100, 20, 150);
imagefilledrectangle($img, 60, 0, 120, 120, $rect_color);

// drawing ellipse (handle, centerX, centerY, width, height, color)
$ect_color = imagecolorallocate($img, 200, 0, 0);
imagefilledellipse($img, 200, 50, 100, 50, $ect_color);


imagepng($img);
imagedestroy($img);


?> 


Share/Bookmark

No comments:

Post a Comment