Search:

Categories



Use a specific font

This example shows how to use a specific font.


<?php
header("Content-type: image/gif");
$image = imagecreate( 500, 250 );
$blue = imagecolorallocate($image, 0,0,255 );
$font = "ARIALBD.TTF";
imageTTFtext( $image, 50, 0, 20, 100, $blue, $font, www.getphp.net );
imagegif($image);
?>

Share

A GD Effect

This is a user submitted example of a ggraphical effect created using GD, quite a nice example of what you can do with PHP and GD

<?php
$img_disp = imagecreate(1000,1000);
$backcolor = imagecolorallocate($img_disp,0,0,0);
imagefill($img_disp,0,0,$backcolor);
$textcolor = imagecolorallocate($img_disp,255,0,0);
$x1 = 0;
$y1 = 0;
$x2 = 0;
$y2 = 1000;
for(;;){
$y1 = $y1 + 20;
$x2 = $x2 + 20;
imageline($img_disp,$x1,$y1,$x2,$y2,$textcolor);
if ($y1 == 1000){
break;
}
}
header(“Content-type: image/png”);
imagepng($img_disp); // Draw the image
imagedestroy($img_disp); // Delete the image from the server’s memory
?>

Share

Create A PNG

This example shows how to create a PNG file

<html>
<body>
<?php
 //create a 300 by 300 image
    $img=ImageCreate(300,300);
 //a black background
    $bgcolor=ImageColorAllocate($img,0,0,0);
 //red color
    $red=ImagecolorAllocate($img,255,0,0);
 //create a red rectangle
    Imagerectangle($img,50,50,150,250,$red);
 //create a filled red rectangle
    Imagefilledrectangle($img,50,170,150,270,$red);
 //create the png file
    ImagePNG($img,”sample.png”);
 //clean up resources
    ImageDestroy($img);
?>
<img src=”sample.png”>
</body>
</html>

Share

GD generated email address

This example came about with the need to display an email address on a site but reducing the risk that some clown would use an email harvester to obtain this from your website, it uses php and the GD library to generate an image which contains your email address

<?php
Header(“Content-type: image/jpeg”);
//create a new image
$image = ImageCreate(200,200);
//create white (for background )
$white = ImageColorAllocate($image ,255,255,255);
//create blue for text
$blue = ImageColorAllocate($image , 0,0,255);
//OK lets create our white background
ImageFilledRectangle($image ,0,0,200,200,$white);
//display some text
ImageString($image,10,15,10,’youremail@youremail.com’,$blue);
//output image to browser as a JPEG
ImageJPEG($image);
//clean up by destroying the image
ImageDestroy($image);
?>

NOw include this on your page like this

<img src=”gdemail.php”>

Share

GIF creation with PHP

This example shows how to create a GIF using PHP

<?php
Header(“Content-type: image/gif”);
$string=implode($argv,” “);
$im = imagecreatefromgif(“images/button1.gif”);
$orange = ImageColorAllocate($im, 220, 210, 60);
$px = (imagesx($im)-7.5*strlen($string))/2;
ImageString($im,3,$px,9,$string,$orange);
ImageGif($im);
ImageDestroy($im);

?>

Share

imagestring example

This is an example of using the imagestring function in PHP

<?php
Header(“Content-type: image/jpeg”);
//width and height of image
$width = 468;
$heigth = 60;
$image = ImageCreate($width,$heigth);
//black background
$black = ImageColorAllocate($image,0,0,0);
//for the text
$white = ImageColorAllocate($image,255,255,255);
/*
parameters for imagestring:
1: resource image, in this case $image
2: font, 1 to 5 are built in fonts in this case 3
3: int x, x co-ordinate in this case 10
4: int y, y co-ordinate in this case 20
5: string s, the string to be displayed in this case “hello world”
6: int col. the color to display the text in, in this case the variable $white
*/
imagestring($image,3,10,20,”Hello World”,$white);
//output jpeg
ImageJpeg($image);
//clean up
ImageDestroy($image);
?>

Share