PHP chapter 2 graphics
PHP chapter 2 graphics
Graphics
2.4Basic Graphics concept
• In web we can use images in the form of logos, buttons, photographs,
charts, advertisements, and icons PHP supports graphics creation with
the GD and lilimb2 extensions.
• Creating an image: to create an image in memory to work with,
imagecreate(x_size, y_size); function.
• To set colors to be used in the image, imagecolorallocate() function
• imagecolorallocate(image, red, green, blue) ; color value 0-255
• To send aJPEG image back to the browser, use header function to set
the image’s type & then send image with imagejpeg() function.
• Also can use imagegif(), imagewbmp(), imagepng().
• After sending image can use imagedestroy() function.
• Images with text: imagesting() function used to draw string
horizontally at given position.
• bool imagestring($img, $font, $x, $y, $string, $color)
Image create program
<?php
header("Content-type:image/png");
$handle=imagecreate(130,50);
$bg_color=imagecolorallocate($handle,240,240,140);
$txt_color=imagecolorallocate($handle,0,0,0);
imagestring($handle,5,5,18,"MSBTE.org.in",$txt_color);
imagepng($handle);
?>
Embedding created images in HTML pages
<html>
<head>
<title>
Embedding created images in HTML pages
</title>
</head>
<body>
<h2> Embedding created images in HTML pages </h2>
This is a blank image created on the server:
<br>
<img src="img2.php">
</body>
</html>
Scaling images
• To change size of an image we have 2 functions as
• imageCopyResized()
• imagecopyResampled()
• Resampled function has features pixel interpolation to give smooth
edges and clarity to resized images so it is slow than resized.
• Copies a rectangular portion of 1 image to another image, smoothly
interpolating pixel values. Reduce size but retain clarity.
• Imagecopyresized($dst,$src,0,0,0,0,$x,$y,$width,$height);