Posts tagged PHP
Calling all PHP Rockstars!
Apr 21st
Hey everyone
We’re looking for some skilled PHP developers to help us innovate with and improve on AMFPHP. If you think you’ve got the goods to help us get this project insanely powerful (but still simple and easy to use), drop us a line!
-
Ariel Sommeria-klein – Project Lead (ariel@amfphp.me)
-
Danny Kopping – Development Lead (danny@amfphp.me)
PHP Thumbnail Generator
Oct 6th
Here’s a very simple PHP class that i wrote to make thumbnail generation on JPG, PNG & GIF images really simple and painless.
NOTE: You will need to have GD installed on your server for this class to work!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | < ?php /** * Image thumbnail generator * Based on the work from http://sniptools.com/vault/generating-jpggifpng-thumbnails-in-php-using-imagegif-imagejpeg-imagepng */ class ThumbnailGenerator { public $sourceFile; public $destinationFile; public $width; public $height; public $format; public $scale; /** * ThumbnailGenerator::__construct() * * @param mixed $sourceFile Path to source file * @param mixed $destinationFile Path to destination file * @param mixed $width Thumbnail file width * @param mixed $height Thumbnail file height * @param string $format jpeg, png or gif * @param bool $scale Scale thumbnail * @return */ public function __construct($sourceFile, $destinationFile, $width, $height, $format="jpeg", $scale=true) { $this->sourceFile = $sourceFile; $this->destinationFile = $destinationFile; $this->width = $width; $this->height = $height; $this->format = $format; $this->scale = $scale; } /** * ThumbnailGenerator::generate() * * @return */ public function generate() { $fromFormat = "imagecreatefrom".$this->format; $sourceImage = $fromFormat($this->sourceFile); $sourceWidth = imagesx($sourceImage); $sourceHeight = imagesy($sourceImage); if($this->scale) { $ratio = $this->width / $sourceWidth; $this->width = $sourceWidth * $ratio; $this->height = $sourceHeight * $ratio; } $targetImage = imagecreate($this->width,$this->height); imagecopyresized($targetImage,$sourceImage,0,0,0,0,$this->width, $this->height,imagesx($sourceImage),imagesy($sourceImage)); $imageFunction = "image".$this->format; return $imageFunction($targetImage, $this->destinationFile, 100); } } ?> |
All credit goes to http://sniptools.com/vault/generating-jpggifpng-thumbnails-in-php-using-imagegif-imagejpeg-imagepng for the snippet!
Sample usage:
$tg = new ThumbnailGenerator("image.png", "thumbs/newimage.png", 100, 100, "png"); echo $tg->generate() ? "Thumbnail generated successfully" : "Failure!";
PHP Process Viewer for Unix
Oct 4th
While working on a project lately, i needed a script that would allow me to find certain processes with PHP and inspect their running times, etc. After struggling for a little bit, i came up with the following script that can output a plain view, JSON or XML of the running processes whose names match a regular expression passed to the script. The script can also kill all processes that match the search term.
This script is by no means complete, but i thought i’d share it with my readers ![]()
Save it as process_viewer and run it from the command line.
Please keep in mind that this script is still in a very rough form and should not be used on enterprise products; it’s merely a bit of buggering around for fun
Click here to download the script.
Usage:
process_viewer [search_regex] [mode] -o [format]
- search_regex – search term to find running processes (defaults to .+)
- mode – script mode (defaults to view)
- view | kill
- -o – flag to indicate that the script must output in a specified format
- format – output format (defaults to plain)
- plain | json | xml
Sample usage:
./process_viewer bash view -o xml
A Belated Announcement
Sep 9th
Well, it’s been a week now since i officially left my old employer – IceBlue InfoTech (which has now been acquired by Virtuosa)… I decided to start my own freelance consulting, development & training firm, creatively named Danny Kopping Consulting. Unfortunately, all the cool names were taken, so i went for the one that i knew wouldn’t be taken
.
I’ll be focusing primarily on building advanced, highly customized Flex-based systems and websites, using PHP, MySQL & Apache on the back-end. I’ll also be doing some training on the side for Flash & Flex (with a little PHP). Hopefully this freelance venture will pan out well and i’ll have even less time to muck about than i did before
A special thanks goes out to my best mate Neil de la Harpe for doing all my corporate branding!
I’ve set up a temporary site over at http://www.dannykopping.co.za/. Check it out…
Using PHP to Backup and Save your MySQL Database
Apr 17th
While working on a recent project, i needed to set up a cron job that would run every X amount of hours and backup a database. I’ve done this a few times before, but only this time (while Googling for a better solution) i found David Walsh‘s blog post on how to Backup Your MySQL Database Using PHP. David’s done a great job and i definitely recommend you check his blog out in its entireity for some really neat web dev articles.
I decided to go and put David’s script into class form (my case of OCD is genuine
) and i added one or two things to it. Basically it allows you to backup one or many tables in a database, include or exclude the data from the backup and i also included Rick Ellis’ (of CodeIgniter fame) Zip class to allow you to zip up the backup and stash it somewhere.
Thanks to David Walsh and Rick Ellis!
Here’s the class: DatabaseDump.php
…and here’s how to use it:
$dump = new DatabaseDump("host", "user", "password", "database", "destination/"); $dump->backup();
Be sure to check out the DatabaseDump.php class for the documentation!
Making your constructors more useful
Mar 30th
The constructor function in Object-Oriented languages is an incredibly useful mechanism to use, and we’ve all used them for a wide variety of solutions.
When i use PHP with Flex, i like taking advantage of AMFPHP’s amazingly nifty feature of being able to send whole objects as binary data to and from the server. This can be vastly useful because you could send a whole object from Flex as an ActionScript class and PHP will receive it and use it as the same class! Here’s a practical example:
You are making an application to control a university’s student details. One logical step would be to go and create a Student class in ActionScript 3.0:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package { public class Student { public var ID:uint; public var firstName:String; public var surname:String; public var age:uint; public var accountPaid:Boolean; public function Student() { } } } |
Now, if you’re using AMFPHP, it’d be a complete ball-ache to have to send the Student class like this (say you were inserting a student into the database):
var student:Student = new Student(); student.firstName = "Jacob"; student.surname = "Zuma"; student.age = 67; student.accountPaid = false; service.addStudent(student.firstName, student.surname, student.age, student.accountPaid);
Wouldn’t it be so much easier to simply do this:
service.addStudent(student);
…and receive the object in PHP, and serialize it into this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php class Student { public $ID; public $firstName; public $surname; public $age; public $accountPaid; public function __construct() { } } ?> |
Well, it’s possible! I covered this is some detail in one of my previous tutorials, and Flex-to-PHP remoting is not in the scope of this particular post. The focus of this post, however, is to give you a couple of tips as to making your constructors more intelligent in the realm of Flex remoting. My two main areas of web development are ActionScript 3.0 and PHP5, but you can easily apply the same logic that follows to all other Object-Oriented languages.
Below is the improvement on my earlier Student class, with the more remoting-friendly constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package { import flash.utils.describeType; public class Student { public var ID:uint; public var firstName:String; public var surname:String; public var age:uint; public var accountPaid:Boolean; public function Student(values:Object=null) { if(values) { var props:XMLList = describeType(this)..accessor; for each(var prop:XML in props) this[prop.@name] = values[prop.@name]; } } } } |
* More information on the describeType function here.
And the PHP equivalent:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php class Student { public $ID; public $firstName; public $surname; public $age; public $accountPaid; public function __construct($properties=null) { if($properties) { $properties = new ArrayObject($properties); foreach(get_object_vars($properties) as $prop => $value) $this->$prop = $value; } } } ?> |
Now, you can easily pass an object (with no type) to either of these constructors, and they will automatically transform that object into a strongly typed, meaningful and above all else – easy to work with – variable.
Working with LAMPP on Linux Mint
Jan 26th
Following from my previous post, i decided to try and mimick my development environment at work on my laptop. The quickest and easiest way to do this is to install XAMPP for Linux. The folks over at apachefriends have given us one of the greatest gifts a web-developer geek could want… A pre-configured, fully open and customizable, versatile setup of LAMPP (Linux, Apache, MySQL, PHP and Perl). Here’s what the package includes:
Apache 2.2.11, MySQL 5.1.30, PHP 5.2.8 & PEAR + SQLite 2.8.17/3.3.17 + multibyte (mbstring) support, Perl 5.10.0, ProFTPD 1.3.1, phpMyAdmin 3.1.1, OpenSSL 0.9.8i, GD 2.0.1, Freetype2 2.1.7, libjpeg 6b, libpng 1.2.12, gdbm 1.8.0, zlib 1.2.3, expat 1.2, Sablotron 1.0, libxml 2.7.2, Ming 0.3, Webalizer 2.01, pdf class 009e, ncurses 5.3, mod_perl 2.0.4, FreeTDS 0.63, gettext 0.11.5, IMAP C-Client 2004e, OpenLDAP (client) 2.3.11, mcrypt 2.5.7, mhash 0.8.18, eAccelerator 0.9.5.3, cURL 7.19.2, libxslt 1.1.8, phpSQLiteAdmin 0.2, libapreq 2.08, FPDF 1.6, XAMPP Control Panel 0.6, bzip 1.0.5, PBXT 1.0.07-rc
Now stick that in your pipe and serve it!
This package must NOT be used on production server. NEVER! Please view this page for information about LAMPP’s inherent security flaws and why it benefits you on a local machine, but not on a production server.
The LAMPP package is simple enough to install, but there is one thing i noticed that is a bit of a ball-ache… By default, you cannot use LAMPP without an active internet connection. However, there is a quick fix:
- Open the httpd.conf file (sudo nano /path/to/httpd.conf)
- Look for the line that says Listen 80
- Change the line to Listen 127.0.0.1:80
- Save the file and restart apache
- Run http://localhost
- It works!
Integrating Flex, AMFPHP and CodeIgniter (also including Value-Objects)
Jan 5th
Setting up email on localhost with PHP + PostCast Server
Dec 12th
I recently had a great ballache with trying to test emails from my local machine. Testing emails is painful enough when it fucking works, nevermind when it doesn’t! I’d been putting off setting my system up for weeks but tonight i finally decided to give it a proper bash… And it worked!
This is how i did it… In 3 easy steps!
Step 1 – Configure your PHP to allow for SMTP connections
Firstly, you need to edit your php.ini file found in the php folder of your local server. I personally use XAMPP because it’s everything i need in a web server. XAMPP now has versions for Windows, Mac and Linux, so nobody has an excuse not to use it now.
In your PHP config file (php.ini), if you do a search for SMTP, you will find the following code:
[mail function]
; For Win32 only.
;SMTP = localhost
;smtp_port = 25
All you need to do is uncomment the last two lines (take out the semi-colons).
Step 2 – Download, install and configure PostCast Mail Server
PostCast Mail Server is a great, simple to use application that will allow you to send email from your local machine. Download and install the application. Once installed, go to Tools->Settings, and set your host name to localhost with a server port of 25. Once you’ve done that, you’re almost ready to go!
Step 3 – Send a mail
Create a file on your server called testmail.php and use the following code:
$headers = “From:me@someaddress.com\r\n”.
“To:me@myaddress.com\r\n”.
“MIME-Version: 1.0\r\n” .
“Content-Type: multipart/mixed;\r\n”;
mail(“me@myaddress”,”PHP mail test”,”www.ria-coder.com/blog”, $headers);
?>
And you’re done!
Server.Acknowledge.Failed – another AMFPHP ballache
Nov 3rd
While working on a project today, i encountered a serious ballache in the form of this:
mx.rpc.Fault (@14f27331)
[inherited]
faultCode “Server.Acknowledge.Failed”
faultDetail “Was expecting message ’93EDDF20-516F-2C3F-AA63-619BAB9DA793′ but received ”.”
faultString “Didn’t receive an acknowledgement of message”
rootCause null
AMFPHP, for all it’s wonder and usefulness, doesn’t document its errors very well. Very irritating. After much digging, i found that the solution to this isn’t actually anything to do with the AMFPHP framework, nor with Flex! It’s stupid little error in PHP… Make sure that you aren’t trying to return an array or object in PHP that has an undefined index or property. I slapped my forehead when i realised the error of my ways.
Sigh.