1 / 29

PHP: SimpleXML

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

dai
Download Presentation

PHP: SimpleXML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP: SimpleXML • Read or write XML objects • …just reading XML today • Help: http://php.net/manual/en/book.simplexml.php

  2. 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

  3. 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

  4. 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;

  5. PHP: SimpleXML • Loops! foreach($xmldata->channel->item as $item){ echo “<h3>” . $item->title . “</h3>; }

  6. PHP: SimpleXML • Attributes! $value = $xmldata->attributes()->myattribute;

  7. Example • http://www.rpi.edu/~gillw3/websys/gd/rss-parser.php

  8. 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

  9. PHP GD & MYSQL Image Processing & Database Storage

  10. 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

  11. 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

  12. 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)

  13. 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

  14. 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

  15. A Lame Example http://www.rpi.edu/~gillw3/websys/gd/lame.php

  16. A Dorky Example http://www.rpi.edu/~gillw3/websys/gd/dorky.php

  17. 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

  18. Water Mark Example PHP.NET: http://us.php.net/manual/en/image.examples.merged-watermark.php

  19. A Graph Example 0,0 100,100

  20. A Graph Example • http://www.rpi.edu/~gillw3/websys/gd/graph.php

  21. 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

  22. 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”

  23. 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

  24. 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

  25. 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

  26. PHP GD / MYSQL • An Example • http://www.rpi.edu/~gillw3/websys/gd/dbgd.php

  27. 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

  28. 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

More Related