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"; [...]

Get the size of an image

Get the size of an image

<?php
$size = getimagesize (“test.jpg”);
echo $size[3];
?>

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 [...]

Random Image

<?php    
$images=array(‘a.jpg’,’b.jpg’,’c.jpg’,’d.jpg’);    
$image=$images[array_rand($images)];   
echo ‘<img src=”path/to/’.$image.’” />’;
?>

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>

Random image example

This example uses an array to store images and then produces a random number and display the image on the screen

<?php
//an array of 4 images in the same direcory as the script
$images = array(“a1.gif”,”a2.gif”,”a3.gif”,”a4.gif”);
//generate a random number
srand((double)microtime()*1000000);
$randomno = (rand()%4);
//display image
echo (“<img src=\”$images[$randomno]\”>”);
?>