0% found this document useful (0 votes)
61 views

PHP Programs With Images

GD is an open-source PHP extension used to create and modify images. The document provides 9 code examples demonstrating various GD functions: 1. Drawing lines and shapes 2. Adding text to images 3. Creating images from user input 4. Modifying existing images 5. Using color fills Each example outputs the resulting image and demonstrates functions like imageline, imagestring, imagecreatefromjpeg, and imagecolorallocate. Installation instructions for the GD extension on XAMPP are also provided at the beginning.

Uploaded by

akurathikotaiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

PHP Programs With Images

GD is an open-source PHP extension used to create and modify images. The document provides 9 code examples demonstrating various GD functions: 1. Drawing lines and shapes 2. Adding text to images 3. Creating images from user input 4. Modifying existing images 5. Using color fills Each example outputs the resulting image and demonstrates functions like imageline, imagestring, imagecreatefromjpeg, and imagecolorallocate. Installation instructions for the GD extension on XAMPP are also provided at the beginning.

Uploaded by

akurathikotaiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

PHP programs with Drawing 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.

GD library must be installed in the server

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

Restart the XAMPP server.

1)Draw lines in php

<?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

2) Draw a string in php

<?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

5) Draw text directions

<?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

imagestring($im, 2, 5, 50, "Hello !", $red);


imagestring($im, 3, 5, 90, "Hello !", $red);
imagestring($im, 4, 5, 130, "Hello !", $red);
imagestring($im, 5, 5, 170, "Hello !", $red);
imagestringup($im, 5, 140, 150, "Hello !", $red);
imagepng($im,"image.png");
imagedestroy($im);
}
?>
Output

6) image creation from user input

<?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>

<p><lable for ="string">Text String:</lable>


<input type="text" id="string" name="string" size="35" /></p>

<p><lable for ="font_size">Font Size:</lable>


<select id ="font_size" name="font_size">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select></p>

<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>

<button type="submit" name="submit" value="create">Create Image</button>


</form>
</body>
</html>
<?php
} else {
//create image
//create the canvas
$myImage = ImageCreate($_POST['w'], $_POST['h']);

//set up some colors


$background = ImageColorAllocate ($myImage, $_POST['b_r'],
$_POST['b_g'], $_POST['b_b']);
$text =ImageColorAllocate ($myImage,$_POST['t_r'],
$_POST['t_g'],$_POST['t_b']);
ImageString($myImage, $_POST['font_size'], $_POST['x'],
$_POST['y'],$_POST['string'], $text);
header ("Content-type: image/png");
ImagePNG($myImage);
ImageDestroy($myImage);
}
?>

Output
7)3D pie chart

<?php

$myImage = ImageCreate(300,300);

$white = ImageColorAllocate($myImage, 255, 255, 255);


$red = ImageColorAllocate($myImage, 255, 0, 0);
$green = ImageColorAllocate($myImage, 0, 255, 0);
$blue = ImageColorAllocate($myImage, 0, 0, 255);

ImageFilledArc($myImage, 100,100,200,150,0,90, $red, IMG_ARC_PIE);


ImageFilledArc($myImage, 100,100,200,150,90,180, $green, IMG_ARC_PIE);
ImageFilledArc($myImage, 100,100,200,150,180,360, $blue, IMG_ARC_PIE);

header ("Content-type: image/png");


ImagePng($myImage);

ImageDestroy($myImage);
?>
Output

8) image modifying existing image


<?php
//use existing image as a canvas
$myImage = ImageCreateFromPng("image.png");
//allocate the color white
$white = ImageColorAllocate($myImage, 255, 255, 255);
//draw on the new canvas
ImageFilledEllipse($myImage, 100, 70, 20, 20, $white);
ImageFilledEllipse($myImage, 175, 70, 20, 20, $white);
ImageFilledEllipse($myImage, 250, 70, 20, 20, $white);
//output the image to the browser
header ("Content-type: image/png");
ImagePng($myImage);

//clean up after yourself


ImageDestroy($myImage);
?>
Output

9) using a color fill

<?php
// It create the size of image or blank image.
$image_size = imagecreatetruecolor(500, 300);

// Set the background color of image using


// imagecolorallocate() function.
$bg = imagecolorallocate($image_size, 0, 103, 0);

// Fill background with above selected color.


imagefill($image_size, 0, 0, $bg);

// output image in the browser


header("Content-type: image/png");
imagepng($image_size);

// free memory
imagedestroy($image_size);

?>
Output

You might also like