<?php /* Run on console: php image.php */ require_once '../src/PLib.php'; PLib\import('image'); $img1 = new PLib\Image('assets/feet.jpg'); // Copy the image // Scale it to max width 700 or max height 700 // Crop it around the center to a size of 500x500 pixels $img2 = $img1->copy('feet2.jpg')->scale(700, 700)->crop_center(500, 500); // Now lets manipulate the image outside of the object with some standalone // PHP image functions. // Grab the image resource handler from the object $ih = $img2->resource(); /* Manipulate the image as you see fit */ $font = PLib\combine_path(dirname(__FILE__), 'assets/Ubuntu-B.ttf'); $text = 'Summertime'; $white = imagecolorallocate($ih, 255, 255, 255); $black = imagecolorallocate($ih, 0, 0, 0); imagettftext($ih, 24.0, 0.0, 21, 51, $black, $font, $text); imagettftext($ih, 24.0, 0.0, 20, 50, $white, $font, $text); // And while we're at it, make it sepia and save it all $img2->sepia()->save();
<?php /* Run on console: php streamreader.php This script removes all unneccessary whitespace from a CSS file */ require_once '../src/PLib.php'; PLib\import('streamreader'); $sr = new PLib\StreamReader('assets/style.css'); $buf = ''; $char = null; $delims = str_split(";,:#.{}\n\r\t "); while (($char = $sr->read()) !== false) { switch ($char) { case "\t": case "\r": case "\n": case " ": $n = $sr->peek(); $p = $sr->look_behind(); // If the prevoius or next charachter is a delimiter we can // dispose the whitespace char. if (in_array($n, $delims) || in_array($p, $delims)) { continue 2; } break; case "/": // It's a comment, skip it if ($sr->peek() === '*') { $sr->read();
<?php require_once '../src/PLib.php'; PLib\import('net'); $cookiejar = new PLib\Net\HTTPCookie('cookies.cki', PLIB_TMP_DIR); $cli = new PLib\Net\HTTPRequest($cookiejar); $cli->cache(60); $resp = $cli->get('http://www.google.com'); echo (string) $resp . "\n";
<?php require_once '../src/PLib.php'; PLib\import('xml/builder'); $doc = new PLib\XML\HTMLDocument(); $root = $doc->add_node('html'); $head = $root->add_node('head'); $head->add_node('meta', null, array('charset' => 'utf-8')); $head->add_node('title', 'My web site & stuff'); $head->add_node('link', null, array('rel' => 'stylesheet', 'href' => 'style.css')); $head->add_node('script', 'if (document.body.length > 0) document.body.write ("This is my life");'); $body = $root->add_node('body'); $body->add_node('h1', 'Welcome to my site'); $body->add_node('p', 'This is just some silly stuff ')->add_node('img', null, array('src' => 'mypic.jpg', 'alt' => 'A picture of me')); $body->add_node_tree(' <div class="my-class"> <div class="left"> <p>Some content to the left</p> </div> <div class="right last"> <p>Some content to the right</p> </div> </div> '); unset($head, $body, $root); echo $doc->render(true);
<?php /* Run on console: php cache2.php This is the exact same script as cache.php except in this script a Cache object is used instead of the convenience functions in PLib/cache.php */ require_once '../src/PLib.php'; PLib\import('cache'); $cache = new PLib\Cache(); // Arbitrary cache key $key = 'my cache key'; // Cache life time. 20 seconds as an example. $cache_lifetime = 20; $data = null; // See if we have any cached data if ($data = $cache->get($key)) { // If it's expired set $data to null to regenerate it if ($data->is_expired()) { $data = null; } else { echo "+ Got data from cache!\n\n"; $data = $data->data; } } if (!$data) { $data = 'This is my data'; $cache->add($key, $data, $cache_lifetime); } echo "{$data}\n";
<?php /* Run on console: php file-dir.php */ require_once '../src/PLib.php'; PLib\import('io'); $dir = new PLib\Dir(__DIR__); $dir->sort(); while ($file = $dir->emit()) { printf("%-50s %-5s %s\n", $file, $file->filetype, $file->nicesize); } echo "\n"; print_r($dir->contents);
<?php require_once '../src/PLib.php'; PLib\import('htmlparser'); use PLib\HTML\Parser; $html = ' <!doctype html> <html> <head> <title>My HTML</title> <link rel="stylesheet" href="style.css"> <script src="script.js"></script> <script> $(function () { $("#mydiv").html ("<h2>Some shit man</h2>"); }); </script> </head> <body> <div id="mydiv"> <p><span id="myspan">Hello world • Hello universe</span></p> <ul> <li>List item 1</li> <li>List item 2</li> <li>List item <i>3</i></li> </ul> <!-- A picture of me --> <!-- A picture of me indeed --> <img src="myimage.jpg" alt="A picture of me"> </div> <div>
<?php /* Run on console: php streamreader.php This script removes all unneccessary whitespace from a CSS file. This is the same script as streamreader.php except this scripts reads the CSS file into memory and manipulates it through the StringReader class instead of the StreamReader class. */ require_once '../src/PLib.php'; PLib\import('io'); PLib\import('string'); $file = new PLib\File('assets/style.css'); $sr = new PLib\StringReader($file->get_contents()); $buf = ''; $char = null; $delims = str_split(";,:#.{}\n\r\t "); while (($char = $sr->read()) !== false) { switch ($char) { case "\t": case "\r": case "\n": case " ": $n = $sr->peek(); $p = $sr->look_behind(); // If the prevoius or next charachter is a delimiter we can // dispose the whitespace char. if (in_array($n, $delims) || in_array($p, $delims)) { continue 2; }