Snippets

Lottery Demo

A simple demonstration of how unlikely it might be to win the jackpot of the UK National Lottery.

Lottery Demo

» download Lottie icon Lottie 1.1 (Win 98/2k/XP/Vista; 60kB ZIP)
» download Lottie icon Lottie 1.1 (Linux; 50kB ZIP)

Generates more than a million 'draws' per second on most PCs over 2GHz.
Developed in FreeBASIC utilising a C library (thanks to Simon Nash for the fast integer sort function).
Windows: uses the Mersenne Twister algorithm to generate less predictable pseudo-random numbers.
Type 'Q' to exit the program at the start or the end of a lottery draw cycle, and use the close icon (Windows: also Alt-F4) to exit during the cycle.

DISCLAIMER: This software is supplied as is. The author accepts no liability for damages, direct or consequential, which may result from the use of this software.

top

'Daily Backup' Batch Script

A backup script for Windows PCs with the 7-Zip open source application installed.

Losing just 25% of a hard drive's contents in 2002 impressed upon me the need for consistent backup methods. PowerArchiver possesses a nice utility to create a password-protected .zip archive of the day's created/changed files. However, PowerArchiver is commercial software and I have only a single licence.

The replacement solution for me was to use the command line options of the open source 7-Zip application, and create superior .7z files. The Windows batch script below copies all files residing in two directories that have changed on the current day, compresses them into a single password-protected .7z file, copies this file to an external drive, and deletes all the temporary files.

This of course is not a complete backup solution - merely an assistive one e.g. run just before the end of a day's work. Important files should also be regularly saved to CDs, secure webspace etc. - never depend on one method.

REM from copysense.co.uk/snippets.php

SET dates="db_%date:~6,6%-%date:~3,2%-%date:~0,2%"
SET dates2=%date:~3,2%-%date:~0,2%-%date:~6,6%
SET dates3=db_%date:~6,6%-%date:~3,2%-%date:~0,2%

IF EXIST "c:\t-backup\%dates%" (RD /s /q "c:\t-backup\%dates%")

XCOPY "c:\temp" "c:\t-backup\%dates%" /s /i /D:%dates2%
XCOPY "c:\documents and settings\joe.bloggs\my documents\reference" "c:\t-backup\%dates%" /s /i /D:%dates2%

CD c:\program files\7-Zip
7z a %dates%.7z -t7z -mx=7 -ms=off -pPa55Word "C:\t-backup\%dates%\*"

MOVE %dates3%.7z \\ServerName\users\joe.bloggs\dbs

RD c:\t-backup\%dates3% /s /q

(The script searches for UK format file stamps. If US file stamps (mm/dd/yyyy) are required, you will need to change the date format in lines 2, 3, and 4 of the script.)

top

'DrawString' Image PHP Class

A tiny PHP class to create images from text, using TTF fonts of choice.

<?php

class drawString {

/*
   'drawString' image generation class
      a tiny class to create graphics from fonts of choice
      ~ M.S. Latter, March 2008 ~ www.copysense.co.uk
      Requires GD library to be enabled

    create a new image object using:
      new drawString($imageString, $fontSize, $width, $height, $foregroundColor, $backgroundColor, $baseX, $baseY, $fontName, $outputFileName, $outputFormat);

    arguments:
         $imageString ~ the text string to be displayed as an image; can include high decimal references e.g. &#8364; and UTF-8 strings - but not HTML entities
         $fontSize ~ unit: points (in GD2)
         $width ~ output image width
         $height ~ output image height
         $foregroundColor ~ array(R,G,B): integer 0-255 e.g. array(0,0,255) for pure blue
         $backgroundColor ~ array(R,G,B): integer 0-255
         $baseX ~ basepoint X coordinates for first character (see imageTTFText on php.net)
         $baseY ~ basepoint Y coordinates for first character
         $fontName ~ font file (must be a TTF file). Presently assumes TTF is in the same directory. Access file directories with / and ../
         $outputFileName ~ output name for image file
         $outputFormat ~ png, gif, or jpg (jpg default is 10% compression)
*/

   
private $blank_image;

   public function
__construct($imageString, $fontSize, $width, $height, $fgArr, $bgArr, $baseX, $baseY, $fontName, $outputFileName, $outputFormat) {

      
$finalName = '';
      
$this -> blank_image = ImageCreate($width, $height);
      
$fgColor = ImageColorAllocate($this -> blank_image, $fgArr[0], $fgArr[1], $fgArr[2]);
      
$bgColor = ImageColorAllocate($this -> blank_image, $bgArr[0], $bgArr[1], $bgArr[2]);
      ;
ImageFill($this -> blank_image, 0, 0, $bgColor);
      
$angle = 0;
      
ImageTTFText($this -> blank_image, $fontSize, $angle, $baseX, $baseY, $fgColor, $fontName, $imageString);
      
$finalName = $outputFileName . '.' . $outputFormat;

      switch (
$outputFormat) {
          case
'png' :
        
      ImagePNG($this -> blank_image, $finalName);
              break;
          case
'jpg' :
          case
'jpeg' :
        
      ImageJPEG($this -> blank_image, $finalName, 90);
              break;
          case
'gif' :
        
      ImageGIF($this -> blank_image, $finalName);
              break;
          default:
              die(
'drawString class error: no image file extension selected.');
      }
    }

    public function
__destruct() {
        
ImageDestroy($this -> blank_image);
    }
}

// end of class


// example usage:

$imageString = sha1(uniqid(rand(), true));
$fontSize = 20;
$width = 600;
$height = 60;
$baseX = 20; // basepoint of first char
$baseY = 37;
$backgroundColor = Array(0,0,255);
$foregroundColor = Array(204,204,204);
$fontName = 'georgia.ttf';
$outputFileName = 'newimage';
$outputFormat = 'gif';

$img = new drawString($imageString, $fontSize, $width, $height, $foregroundColor, $backgroundColor, $baseX, $baseY, $fontName, $outputFileName, $outputFormat);

?>

top

Desktop Flashcards Yahoo! Widget

A simple rotating 'flashcards' widget for the Yahoo! Widget Engine, which can aid the memorisation of lists etc while you work. The example file includes a selection of Linux commands.

» download DesktopFlashcards (2kB ZIP)

Released as a .kon file - easy to edit with a text editor and executes like a .widget file. Tested in the current Yahoo! Widget Engine and version 3.1.4 (2006).

top