Category Archives: Code

Generate QR Code for URLs using Google API

Since I want to implement QR codes into the URL shortener, I wrote a function in PHP that generates the link usin’ Google API.

 


/**
 * Class for QR Codes 
 *
 */
class QR {
	/**
	 * Function returns URL to Google API QR Generated image
	 * 
	 * @param url - The URL to be turned to QR Code
	 * @param size - Size of the QR image
	 * @param errorCorrection - Amount of reduntant information the QR code has (L - default, M, Q, H)
	 * @return Google API QR Generated image (to be used directly in img tag)
	 */
	public static function getQRforURL($url, $size, $errorCorrection = "L") {
		$link = "https://chart.googleapis.com/chart?" . 
				"cht=qr" . "&" .
				"chs=$size" . "x$size" . "&" .
				"chl=" . urlencode($url) . "&" .
				"choe=UTF-8" . "&" .
				"chld=$errorCorrection";
		return $link;
	}
}

Tagged , ,

How to make an URL Shortener (with code in PHP and MySQL database)


Considering the number of URL Shortening services, I decided to make a short review of the subject. URL Shorteners were made popular by micro-blog services which used limited number of characters for a message e.g. Twitter.

So, here’s what’s covered:

 
Update (2011-07-29):
Reorganized code. It’s now divided into classes, easier for understanding and reuse. Using PDO for database connection instead of mysql methods. Added QR Code for generated shortlinks. This tutorial won’t change anymore, since it’s on the basic level it needs to be. For more information, you can follow the URL Shortening project I started, named BooRL.

Continue reading

Tagged , , ,

Singleton Pattern


Design patterns are reusable solutions to common software engineering problems. They are best-practice in software design. It is not easy to apply them, and it often happens that pattern misuse may lead to bigger problems, a good design supported by patterns will last and give you less headaches. Examples in PHP and Java are given.

Singleton is a creational pattern (creational patterns deal with object creation mechanisms) which restricts instantiation of a class to one (and only one) object. We’ll discuss it in 3 chapters:

Tagged , , ,

Number converter

Since it’s needed for URL Shorteners, and that’s what I’m writing a tutorial about, here’s a number converter from decimal, to whatever base you need (if you supply a base array long enough). Written in PHP.

Update (2011-07-27):

Changed method to static and added an exception.

 

/**
* Class with static methods for converting numbers from decimal to a given base number system
*
*/
class NumberConverter {
/**
* Array to be used for conversion
*/
private static $base = “0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+=”;

/**
* Get’s the $number character from $base array
*
* @param number – the position of the character needed
* @return Returns a character from $base for a given $number
*/
private static function getCharacter($number) {
return self::$base{$number};
}

/**
* Converts a decimal number to a given base
*
* @param number – Number to be converted
* @param toBase – Number base to be converted to
* @throws Exception
*/
public static function fromDecimalToBase($number, $toBase){

// Check if the base is greater then the max possible base
if ($toBase > strlen(self::$base)) {
throw new Exception(‘Base too big.’);
}
// Convert number to integer (better safe than sorry)
$number = intval($number);

// Set a flag if the number is negative, do a positive conversion then just add the minus sign
$negative = false;
if ($number <= 0) { $negative = true; $number = $number * -1; } $converted = ""; while ($number != 0) { // Get a number in the new base $modulus = $number % $toBase; // Get the number representation in that base $converted = self::getCharacter($modulus) . $converted; // Remove the extracted number and continue until the number is 0 $number = intval(($number - $modulus) / $toBase); } // Add the minus sign if it was negative if ($negative) { $converted = "-" . $converted; } return $converted; } } [/php]

Tagged , ,

Extracting pixel values from videos using OpenCV

I have been associated with an image processing group for some time that is doing research on background/foreground segmentation using neural networks. I might soon add a publication (depending if we get it done on time) on pixel-wise based segmentation. Here’s a function which takes an array of video files, and exports their pixels, one each line, into a file with a given separator. It is interesting to apply unsupervised learning alghoritms on the resulting set. Link to source. Written in C.
Continue reading

Tagged , , , ,