Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nic Shulver, Other Useful PHP Bits More Useful PHP Read the contents list in the PHP online manual You will find loads of interesting.

Similar presentations


Presentation on theme: "Nic Shulver, Other Useful PHP Bits More Useful PHP Read the contents list in the PHP online manual You will find loads of interesting."— Presentation transcript:

1 Nic Shulver, N.A.Shulver@staffs.ac.uk Other Useful PHP Bits More Useful PHP Read the contents list in the PHP online manual You will find loads of interesting functions Using include() and require() The dirname(__FILE__) trick PDF writing through a free add-on library getimagesize() The mail() functions

2 Nic Shulver, N.A.Shulver@staffs.ac.uk Other Useful PHP Bits Using include() and require() Examples of PHP’s “file insertion” command include(“myStandardHeader.inc”); include(“/root/subdir/library/visaPayments.php”); include(“../libs/MyGraphix.php”); The “included” file is effectively added into your source code at the line where the “include()” command is issued The “require()” command is identical except that if it fails (example: no such file) then PHP stops with an error

3 Nic Shulver, N.A.Shulver@staffs.ac.uk Other Useful PHP Bits The dirname(__FILE__) trick The value __FILE__ (double underscore at beginning and end) is provided for you by PHP It’s a kind of run-time “constant” and it is the full filename of your script For example, it had the value: “c:\Inetpub\wwwroot\phpTest\writePDF\writePDF.php” for a PDF-writing test script on a staff machine The command dirname(__FILE__) returns something like: “c:\Inetpub\wwwroot\phpTest\writePDF”

4 Nic Shulver, N.A.Shulver@staffs.ac.uk Other Useful PHP Bits Why use dirname(__FILE__)? Very useful when you need the path to your local directory (handy for database driver initialisation) Useful if you ever expect to change the directory your code works from By moving development code into a production server Or by changing your directory tree Why not just use __DIR__ ? This is a replacement for dirname(__FILE__) Appeared in PHP 5.3.0 (released late 2008)

5 Nic Shulver, N.A.Shulver@staffs.ac.uk Other Useful PHP Bits PDF writing Free and commercial PDF libraries available Simple, easy to use free library is FPDF (Free PDF) at http://www.fpdf.org/http://www.fpdf.org/ Tutorials at http://www.fpdf.org/en/tutorial/index.phphttp://www.fpdf.org/en/tutorial/index.php Online documentation at http://www.fpdf.org/en/doc/index.php http://www.fpdf.org/en/doc/index.php Downloads at http://www.fpdf.org/en/download.phphttp://www.fpdf.org/en/download.php The example on the next slide was generated with the FPDF library

6 Nic Shulver, N.A.Shulver@staffs.ac.uk Other Useful PHP Bits

7 <?php require('/Inetpub/wwwroot/phpLib/fpdf.php'); $pdf=new FPDF(); $pdf->AddPage(); $pdf->Image('exLogo.png',10,8);// Adds a "Corporate Logo" // Bit of text under the logo $pdf->SetFont('Courier','',8); $pdf->Ln(20); $pdf->Cell(64, 10, 'All rights reserved (c)2005 The RealBig Corporation.',0,1); // main chunks of text $pdf->SetFont('Helvetica','B',16); $pdf->Write(0,'Hello World!'); $pdf->Ln(8); $sExampleText = "This PDF is generated by ". __FILE__. "! Example "; $pdf->SetFont('Times','',12); $pdf->Write(5, $sExampleText. "1...\n"); $pdf->Write(5, $sExampleText. "2...\n"); $pdf->Write(5, $sExampleText. "3...\n");

8 $pdf->Write(5, $sExampleText. "4...\n"); $pdf->Write(5, $sExampleText. "5...\n"); $xpos = $pdf->GetX();// draws a mid-blue horizontal line 100 units long $ypos = $pdf->GetY(); $pdf->SetDrawColor(128, 128, 255); $pdf->Line($xpos,$ypos, $xpos+100,$ypos); $pdf->Write(8, $sExampleText. "6...\n"); $pdf->Write(10, $sExampleText. "7...\n"); $pdf->Write(12, $sExampleText. "8...\n"); // draws an orange box with a mid-red border, 100 units wide and 25 units high $xpos = $pdf->GetX(); $ypos = $pdf->GetY(); $pdf->SetDrawColor(255, 80, 80); $pdf->SetLineWidth(0.75); $pdf->SetFillColor(248, 191, 46); $pdf->Rect($xpos,$ypos, 100,25, 'DF'); $pdf->Output(); ?>

9 Nic Shulver, N.A.Shulver@staffs.ac.uk Other Useful PHP Bits The getimagesize function Sometimes you need to check the size of an image before using it For example, a photo gallery service might allow users to upload and view images Good to check image size and scale appropriately for images that exceed predetermined limits Could be used to help scale images for thumbnails getimagesize() returns a four-element array (or list)

10 Nic Shulver, N.A.Shulver@staffs.ac.uk Other Useful PHP Bits The getimagesize function You can use this kind of code for easy access: list($width, $height, $type, $attr) = getimagesize("exLogo.png"); $width and $height contain just what you would expect $type holds a number giving information about the image format (GIF, JPG, PNG, SWF, SWC, PSD, TIFF, BMP, IFF, JP2, JPX, JB2, JPC, XBM, or WBMP) – for example, PNG gives a value of 3 $attr holds part of an HTML image tag, for example: width="320" height="64"

11 Nic Shulver, N.A.Shulver@staffs.ac.uk Other Useful PHP Bits The postman only knocks once... This simple code may be used to send email messages: Note this only works if the PHP server also has a mail server running – you’d have to check with the administrator

12 Nic Shulver, N.A.Shulver@staffs.ac.uk Other Useful PHP Bits There’s much more out there! We have looked at a few interesting areas: Using include() and require() to add blocks of code into our PHP – great for modularity and re-use The dirname(__FILE__) trick that helps us find where we are on the server PDF writing through a free add-on library, including bitmapped graphics and simple vector graphics The getimagesize() for dynamically determining the size of an image in one of a dozen or more formats The mail() function – it depends on other resources and it is not very efficient, but it is very easy to use


Download ppt "Nic Shulver, Other Useful PHP Bits More Useful PHP Read the contents list in the PHP online manual You will find loads of interesting."

Similar presentations


Ads by Google