Professional Documents
Culture Documents
PHP Programs With Images
PHP Programs With Images
PHP Programs With Images
Steps for Install GD Library for Run Smooth Images on Php Server
GD is an open-source code library that is required to create and manipulate images in PHP. It is
used for creating PNG, JPEG, and GIF images. It is commonly used to create charts, graphics,
thumbnails, etc, and website development is the most common application of GD.
1. Open XAMPP
2. Locate and open php.ini in your editor by click on Apsche Config button.
3. Find ;extension=gd.
4. Remove semicolon from ;extension=gd and save the file.
5. Go to php folder. It is usually present in C:\xampp.
6. Look for php_gd.dll in the ext folder.
7. Copy php_gd.dll and paste it into the following folder.
C:\Windows\System32
<?php
create_image();
print "<img src=image.png?".date("U").">";
function create_image()
{
$im = @imagecreate(200, 200) or die("Cannot Initialize new GD image
stream");
$background_color = imagecolorallocate($im, 255, 255, 0); // yellow
$red = imagecolorallocate($im, 255, 0, 0); // red
$blue = imagecolorallocate($im, 0, 0, 255); // blue
imageline ($im, 5, 5, 195, 5, $red);
imageline ($im, 5, 5, 195, 195, $blue);
imagepng($im,"image.png");
imagedestroy($im);
}
?>
Output
<?php
// to define the size of the image
$image= imagecreate(400, 200);
// to define the background color of the image
$bg=imagecolorallocate($image, 45, 150, 145);
// to fill the selected color in the background
$tx=imagecolorallocate($image,0,0,0);
imagestring($image,5,100,100,"sairam",$tx);
Header("Content-Type: image/png");
Imagepng($image);
Imagedestroy($image);
?>
</body>
</html>
Output
3) Draw a rectangle
<?php
create_image();
print "<img src=image.png?".date("U").">";
function create_image()
{
$im = @imagecreate(200, 200) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 0); // yellow
$red = imagecolorallocate($im, 255, 0, 0); // red
$blue = imagecolorallocate($im, 0, 0, 255); // blue
imagerectangle ($im, 5, 10, 195, 50, $red);
imagefilledrectangle ($im, 5, 100, 195, 140, $blue);
imagepng($im,"image.png");
imagedestroy($im);
}
?>
Output
4)Draw ellipse
<?php
create_image();
print "<img src=image.png?".date("U").">";
function create_image()
{
$im = @imagecreate(200, 200) or die("Cannot Initialize new GD image
stream");
$background_color = imagecolorallocate($im, 255, 255, 0); // yellow
$red = imagecolorallocate($im, 255, 0, 0); // red
$blue = imagecolorallocate($im, 0, 0, 255); // blue
imageellipse($im, 50, 50, 40, 60, $red);
imagefilledellipse($im, 150, 150, 60, 40, $blue);
imagepng($im,"image.png");
imagedestroy($im);
}
?>
Output
<?php
create_image();
print "<img src=image.png?".date("U").">";
function create_image(){
$im = @imagecreate(200, 200)or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 0); // yellow
$red = imagecolorallocate($im, 255, 0, 0); // red
<?php
if (!$_POST) {
//show form
?>
<!DOCTYPE html>
<html>
<head>
<title>Image Creation Form</title>
<style type="text/css">
fieldset{border: 0; padding: 0px 0px 12px 0px;}
fieldset lable {margin-left: 24px;}
legend, label {font-weight:bold;}
</style>
</head>
<body>
<h1>Create an Image</h1>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Image Size:</legend><br/>
<lable for ="w">W:</lable>
<input type="text" id="w" name="w" size="5" maxlength="5" />
<lable for ="h">H:</lable>
<input type="text" id="h" name="h" size="5" maxlength="5" />
</fieldset>
<fieldset>
<legend>Background color:</legend><br/>
<lable for="b_r">R:</lable>
<input type="text" id="b_r" name="b_r" size="3" maxlength="3" />
<lable for="b_g">G:</lable>
<input type="text" id="b_g" name="b_g" size="3" maxlength="3" />
<lable for="b_b">B:</lable>
<input type="text" id="b_b" name="b_b" size="3" maxlength="3" />
</fieldset>
<fieldset>
<legend>Text color:</legend><br/>
<lable for="t_r">R:</lable>
<input type="text" id="t_r" name="t_r" size="3" maxlength="3" />
<lable for="t_g">G:</lable>
<input type="text" id="|t_g" name="t_g" size="3" maxlength="3" />
<lable for="t_b">B:</lable>
<input type="text" id="t_b" name="t_b" size="3" maxlength="3" />
</fieldset>
<fieldset>
<legend>Text starting position:</legend><br/>
<lable for="x">X:</lable>
<input type="text" id="x" name="x" size="3" maxlength="3" />
<lable for="y">Y:</lable>
<input type="text" id="y" name="y" size="3" maxlength="3" />
</fieldset>
Output
7)3D pie chart
<?php
$myImage = ImageCreate(300,300);
ImageDestroy($myImage);
?>
Output
<?php
// It create the size of image or blank image.
$image_size = imagecreatetruecolor(500, 300);
// free memory
imagedestroy($image_size);
?>
Output