300 likes | 387 Views
PHP: SimpleXML. Read or write XML objects …just reading XML today Help: http://php.net/manual/en/book.simplexml.php. PHP: SimpleXML. Step 1: Get some XML $xmldata = simplexml_load_file(‘stuff.xml‘); (can open a file over HTTP or the local FS) $ xmldata = simplexml_load_string($mystring);
E N D
PHP: SimpleXML • Read or write XML objects • …just reading XML today • Help: http://php.net/manual/en/book.simplexml.php
PHP: SimpleXML • Step 1: Get some XML $xmldata = simplexml_load_file(‘stuff.xml‘); (can open a file over HTTP or the local FS) $ xmldata = simplexml_load_string($mystring); • Both functions return an Object • Both functions require a well formed XML input
PHP: SimpleXML • Step two: Select data out of your XML • Run some XPATH on that XML $resultxml = $xmldata -> xpath('/channel/'); this object is a subset of $xmldata
PHP: SimpleXML • Step two: Select data out of your XML(option 2) • Treat your XML object like multidimensional arrays echo $xmldata -> channel -> item[0] -> title;
PHP: SimpleXML • Loops! foreach($xmldata->channel->item as $item){ echo “<h3>” . $item->title . “</h3>; }
PHP: SimpleXML • Attributes! $value = $xmldata->attributes()->myattribute;
Example • http://www.rpi.edu/~gillw3/websys/gd/rss-parser.php
In Class: • Create a php page that • Points to some remote RSS file • Prints out a list of each of the item’s published dates
PHP GD & MYSQL Image Processing & Database Storage
Why Process Images • Automatic Watermarks • Watermark images viewed on ‘free’ page • Unmarked image on ‘pay-for’ page • Resize images on the fly • Thumbnail / Gallery View • Custom images for mobile devices • Create charts and graphs on the fly with dynamic data
Process Once or at Every Request • Blogger, Flickr, &c all make thumbnails at the point of upload. • For high volume operations, disk space is always cheaper than processor time • Processing Requests on the fly necessary if you are customizing images every time they are loaded • Charts are updated after a set duration
Creating Images • imagecreatefromjpeg($path_to_file) • Creates an image object using a preexisting image file • There are functions for png, gif, bmp &c • imagecreatetruecolor($height, $width) • Creates new image object • imagecreatefromstring($binary_data)
Image Properties • imagesx($image_resource) • Returns the width (in pixels) of an image object • imagesy($image_resource) • Returns the height of an image object Aspect Ratio = x/y or w/h • When resizing images, aspect ratio must be maintained
Outputting Images • imagejpeg($image_resource) • Outputs a jpeg format image to the browser • Other functions for png, gif, bmp &c • imagejpeg($image_resource, $path_to_file) • Saves image to the server
A Lame Example http://www.rpi.edu/~gillw3/websys/gd/lame.php
A Dorky Example http://www.rpi.edu/~gillw3/websys/gd/dorky.php
Image Processing • imagecopyresampled(lotsofargs) • Used to resize an image • imagecopymerge(lotsofargs) • Used to place an image on top of another • imagefilter(lotsofargs) • Manipulates the content of an image
Water Mark Example PHP.NET: http://us.php.net/manual/en/image.examples.merged-watermark.php
A Graph Example 0,0 100,100
A Graph Example • http://www.rpi.edu/~gillw3/websys/gd/graph.php
Storing Images in Databases • Binary Data can be stored in fields with a ‘blob’ type • Other data to store • File type (.jpg, png…) • File size • Original file name • EXIF data • This data can be lost after GD functions are run
Uploading Images • <input type=“file” name=“myfile”> • Creates that include a file input box in an html form • The form must have • method = “post” • enctype = “multipart/form-data”
Processing the Form • Image is stored on server in a TMP directory • $_FILES accesses files posted to the server • $_FILES['userfile']['name'] – name of file on the client • $_FILES['userfile']['type'] – file mimetype • $_FILES['userfile']['size'] – file size • $_FILES['userfile']['tmp_name'] – name of file on the server • MORE: • http://us.php.net/features.file-upload
Putting Uploaded File into MYSQL • Open the uploaded file fopen() • Read it in to a string fread() • Use that string in an MYSQL INSERT command
Getting Images Out of MYSQL • Create a PHP image object from DB output • Imagecreatefromstring($row[‘img’]) • Unlike other Imagecreate functions, it auto-detects the image encoding type
PHP GD / MYSQL • An Example • http://www.rpi.edu/~gillw3/websys/gd/dbgd.php
Put an Image in a Database • In PHPMYADMIN • Create a table called images • Add two fields • id, type INT – Auto-inc • img, type BLOB • Click the INSERT tab • Upload an image from your desktop to your database
Improve my graph code Find the extreme values in the array Use imagesx() and imagesy() and count() to force the array data to fill any graph size In Class Work