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

PHP chapter 2 graphics

Chapter 2 covers basic graphics concepts in PHP, including image creation, color allocation, and sending images to the browser using functions from the GD library. It also discusses scaling images with imageCopyResized and imageCopyResampled functions, as well as generating PDF documents using the FPDF class. Additionally, it explains the use of include() and require() functions for incorporating PHP files into scripts.

Uploaded by

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

PHP chapter 2 graphics

Chapter 2 covers basic graphics concepts in PHP, including image creation, color allocation, and sending images to the browser using functions from the GD library. It also discusses scaling images with imageCopyResized and imageCopyResampled functions, as well as generating PDF documents using the FPDF class. Additionally, it explains the use of include() and require() functions for incorporating PHP files into scripts.

Uploaded by

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

Chapter 2

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

• destination image link, source image, x,y –coordinate of destination,


x,y-coordinates of source point, destination width, height, source
width, height
• ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$width,$height);
Scaling images program1
<?php
$src=ImageCreateFromJPEG('php.jpg');
$width=ImageSx($src);
$height=ImageSy($src);
$x=$width/2;
$y=$height/2;
$dst=ImageCreateTrueColor($x,$y);
ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$width,$height);
header('Content-Type:image/png');
ImagePNG($dst);
?>
Scaling images program2
<?php
$src=ImageCreateFromJPEG('php.jpg');
$width=ImageSx($src);
$height=ImageSy($src);
$x=$width/2;
$y=$height/2;
$dst=ImageCreateTrueColor($x,$y);
Imagecopyresized($dst,$src,0,0,0,0,$x,$y,$width,$height);
header('Content-Type:image/png');
ImagePNG($dst);
?>
Creation of PDF document
• FPDF is a PHP class which allows to generate PDF files with PHP.
• F stands for free. It is free to use and it does not require API.
Advantages of FPDF
• Choice of measure unit, page format and margins
• Allow to set page header and footer management
• It provides automatic line break, page break & text justification
• It supports images in various formats (JPEG, PNG, GIF)
• It allows to set up colors, links, TrueType, Type1 & encoding
support
• It allows page compression

Link to download FPDF class:


http://www.fpdf.org/en/download.php
Include() & require() function
• We can include the content of a PHP file into another PHP file
before the server executes it.
• Include() function & require() function: takes all the text in a
specified file and copies it into the file that uses the include
function.
• If there is any problem in loading a file then the include()
function generates a warning but script will continue execution.
• If there is any problem in loading a file then the require()
function generates a fatal error and halt execution of script.
Creation of PDF document
<?php
require('fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(60,10,'Hello PHP World!',1,1,'C');
$pdf->Output();
?>
Cell(w, h, txt, border, ln, align, fill, link)
•W- cell width
•H-cell height
•Txt- string to print
•Border- 0(no border) 1(frame)
•Ln- current position should go after call (o:to right 1: to next line 2: below)
•Align- L-left R-right C-center
•Fill-cell background true , transparent false(default value)
•Link- URL

You might also like