Question: In this assignment you will program a dynamic, database driven image and a PDF file in PHP. Make a new directory on the server in
In this assignment you will program a dynamic, database driven image and a PDF file in PHP.
Make a new directory on the server in putty.exe by running the following command:
mkdir labs
cd labs
You will create new files on the server and edit them by running the nano command like this:
nano MyFile.html
To stop editing and save press Ctr+X. Learn more about the nano editor online.
Part 1: Database driven image
Step 1. In the first step you will create a custom image with text. You will need the font file DUNGEON.TTF transferred to your server. Make sure it is saved in capital letters and it is in the same directory as your PHP script. Please lookup on php.net the following function: imagettftext Create a script called makeImage.php on the server with the following code:
$white=imagecolorallocate($im, 255, 255, 255); $black=imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im,0,0,399,29,$white); $d=date("h:i:s"); $text = "Hello World! ".$d; $font='DUNGEON.TTF'; imagettftext($im, 20,0,11,21, $black, $font, $text); imagepng($im); imagedestroy($im);
?> Please note that the header() function in PHP allows you to create binary data as images, PDFs, and any types of files. To test step 1, please navigate to your site. Replace x.x.x.x and port with your values.
http://x.x.x.x:port/labs/makeImage.php You should see a PNG image with the current time.
Step 2:
Let's get data from the database. You will be access a database called world. The database has information about countries all over the world. The user and password to the database is in the mysql_connect function. You can run custom queries to preview the data.
Please make script called viewDB.php: Step 3. Now, let's connect the database to the image. Please create a script called makeImageDBData.php:
mysql_connect("localhost","read","read"); mysql_select_db("world"); $query="select name from Country order by name desc limit 10"; $r=mysql_query($query); $textData = ""; while($row=mysql_fetch_array($r)) { $textData = $textData.$row['name']." "; } mysql_close(); // Generate image header('Content-Type: image/png'); $im = imagecreatetruecolor(300,300); $white=imagecolorallocate($im, 255, 255, 255); $black=imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im,0,0,299,299,$white); $d=date("h:i:s"); $text = "Current Time: ".$d." "; $text = $text.$textData; $font='dum1thin.ttf'; //$font='DUNGEON.TTF'; imagettftext($im, 20,0,11,21, $black, $font, $text); imagepng($im); imagedestroy($im); ?> Step 4. Finally, create an HTML page called MyAd.html with the following code: This is a great page. The image will appear below:
Part 2: Database driven PDF file
Step 1.In this assignment you will create a PDF file, which draws data from the database. The database part is the same as in above image example. Step 1. Let's start by downloading FPDF library and installing in our directory. Some PHP libraries are enabled by editing the php.ini file. FPDF can be simply downloaded and extracted in your website directory. Download FPDF first: http://www.fpdf.org/en/dl.php?v=17&f=zip You can learn more about this library at fpdf.org. There are tutorials there, and overall, generating PDFs on the fly can be very useful. PDF files can be printed very reliably. Step 2. Unzip the file in a new directory called docs on the server You should see fpdf.php in \docs in addition to other files and directories. Step 3. Create a test script called viewTestPDF.php AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Cell(40,10,'Hello World'); $pdf->Output(); ?> Test the PDF by navigating to: http://x.x.x.x:port/labs/viewTestPDF.php The PDF files, once downloaded, are cached. So you need to close the tab or window, and navigate to the url again to reopen the PDF to see any changes. Step 4. Please create file viewPDF.php with database data. As solution to the assignment please attach the PDF file you generated.
$textData = ""; while($row=mysql_fetch_array($r)) { $textData = $textData.$row['name']." "; } $d=date("h:i:s"); $text = "Current Time: ".$d." "; $text = $text.$textData; mysql_close(); $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',16); $pdf->MultiCell(40,10,$text); $pdf->Output(); ?>
To turn in the assignment provide a working URL, like this one: http://x.x.x.x:port/labs/makeImage.php, to your lab for each of the following scripts:
makeImage.php
viewDB.php
makeImageDBData.php
MyAd.html
viewTestPDF
viewPDF.php
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
