Archive for the ‘PHP’ Tag

Scribbler

example

Scribbler is a WordPress plugin that allows you to show an animation of handwritten text on your site. See an example on deschrijfcoacharuba.com

Why use Scribbler?

Handwriting is very personal, this can give a personal touch to your logo, your site, product, app or videos.

How to use Scribbler

  1. Download the plugin  (we’re not on the WordPress directory yet).
  2. Go to your admin section in WordPress and choose Plugins.
  3. Choose ‘upload plugin’
  4. Press ‘choose file’ and then install
    • Alternatively, unzip the plugin. Use FTP to go to your WordPress/wp-content/plugins folder and upload the directory there. Activate the Scribbler plugin
  5. Go to Settings > Scribbler
  6. Enter the text you’d like to see animated on your home page

Options

There is currently only one option: text. This defines what text to show on the page.

Customization

You will probably want to customize the CSS file, which is in the same folder as the plugin. By default the text appears in div#pen, which will be placed right below the content of the post.

Limitations

  • The plugin is programmed to only work on a home page (using is_frontpage), because having a handwriting animation on every page gives bad usability.
  • The speed is fixed to 400 ms per character
  • As already said, it has only one font: ‘Tangerine’ (a Google Font).

Caveats

The plugin hasn’t been tested on every website known to man, and it relies heavily on javascript, in particular the Raphael, jQuery and Cufon libraries.

There is only one font (Tangerine). If you want to use another font, you’ll have to do the preparations yourself: create a cufonized font and modify the default in Scribbler.php.

Known Problems

There are no known problems as of yet.

Contribute

If you want to help me, I’d appreciate to be sent cufonized fonts. I will then implement the use of that font in the plugin.

I want this too on my site!

If you’re not technically inclined I can customize it for you on your site. Just leave a comment or contact me (http://about.me/michiel) and we can agree on a price.

How to create a Class

It’s three articles (1 2) now, it’s almost a book! Here’s how to create a class in my three favorite languages:

PHP

Method 1: standard

The advantage of this method is it’s obvious portability to other (e.g. compiled) languages. If you add getters and setters the link to java is easily made.

class Cube {
  var $colors;
  var $size;
  var $brand;
}
$c->colors = 'red,green,blue,yellow,white,orange';
$c->size = 4;
$c->brand = 'rubiks';

foreach (get_object_vars($c) as $property=>$value)
	print_r($property . ' = ' . $value . ', ');
?>

Method 2: instant class

The advantage of this method is the fact that properties (any method or attribute) can me created on the fly, even using variables, e.g. ‘price’, ‘new’ . ‘price’ or even ‘new’ . ‘price’ . $date. So in the example below we are defining the class, but not even giving it any properties (though it could have some of course).

class Cube {}
$c = new Cube;

$prop = $colors;
$c->$prop = 'red,green,blue,yellow,white,orange';

$prop = 'size';
$c->$prop = 4;

$prop = 'brand';
$c->$prop = 'rubiks';

foreach (get_object_vars($c) as $property=>$value)
	print_r($property . ' = ' . $value . '
');
?>

Method 3: instant object

The advantage of this method is that the object doesn’t actually have a class. Because of that properties can be invented at will, and they don’t have to be variables, shortening the code to a form that looks like the class actually was defined. Be careful with this though, other people who read your code will frantically look for the class definition and won’t find it if they don’t know about the use of stdClass. An equivalent, but not as nice looking, is to cast null to an object, as in “$c = (object) null;”

$c = new stdClass();
$c->colors = 'red,green,blue,yellow,white,orange';
$c->size = 4;
$c->brand = 'rubiks';

foreach (get_object_vars($c) as $property=>$value)
	print_r($property . ' = ' . $value . '
');
?>

JavaScript

In javascript there are plenty of possibilities for creating objects. Classes however do not exist (not in a practical sense anyway). Any object you create is a ‘class’ in itself, and can be copied or extended on the fly.

Note how javascript’s for..in actually iterates over all property names (not the values as you might expect).

var c = new Object;
c.colors = 'red,green,blue,yellow,white,orange';
c.size = 4;
c.brand = 'rubiks';

for (property in c)
	alert(property + "=" + c[property]);

Though it is correct that arrays and functions are both objects, and it seems an easy way of providing php’s foreach construct, the for..in is not suitable for this. An array with 10 elements, with index 0..9 cannot be iterated this way, since the array is also a ‘Class’ and all its properties, e.g. the length property, will also be iterated. In the example below, if we added a method ‘show’, we would see the definition of the function, converted to a string. That’s right, the full source code of the method.

r = Array('red','green','blue');
r.show = function() { alert(this); }
for (property in r)
    alert(property + "=" + r[property]);

(Visual) Basic

I will not consider VB.Net, since in .NET every program is object oriented (supposedly) and thus classes are at the base of the language. Anyway, it’s not more difficult than choosing “add item > class”, and almost all the code is created for you.

In Visual Basic it gets more interesting, since it’s not an OO language in itself. Again, just like in JavaScript, objects are more considered ‘containers for variables and functions’. The concept of subclassing does not exist in VB. OK, here it goes. I am assuming you have created a Standard Exe project. First, choose Project > Add Class Module. You’ll see a new module created named ‘Class1’. You can just rename that in the properties pane to Cube. Now add the code:

' method 1: public members
Public colors As String
Public size As Integer
Public brand As String

This is not the preferred way, we should actually create getters and setters for these. This is easy using Tools > Add Procedure. Name it colors e.g. and you will get the following code

Public Property Get colors() As Variant
End Property
Public Property Let colors(ByVal vNewValue As Variant)
End Property

To this you’d still have to add a private variable, such as m_colors, which would be used in your Get and Let methods. You can create a new Cube object from within a form or a module using “set c=new Cube”.

In VBA (Visual Basic for Applications) the procedure is exactly the same, though you don’t have to create a new executable. You can start by e.g. pressing ALT+F11 in Excel and you will be able to add a class module.

Finally, in VBScript you could either create a class definition as in VB (without the type names though)… or use the ExecuteGlobal method to evaluate code and, again, generate classes on the fly.

How to find the length of a string

Just out of being bored I decided to share some really basic source code. Here’s how to find the lenght of a text in my three favorite languages:

PHP

$s = "simple php programming";
$length = strlen($s);

JavaScript

var s = "simple javascript programming";
var length = s.length;

(Visual) Basic

dim s as string
dim length as integer

s = "simple basic programming";
length = len(s);