Snippets
Lottery Demo
A simple demonstration of how unlikely it might be to win the jackpot of the UK National Lottery.
» download
Lottie 1.1 (Win 98/2k/XP/Vista; 60kB ZIP)
» download
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.
'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
- Download and install 7-Zip from 7-Zip.org.
- Copy the batch script above into a text editor.
- Change the password (Pa55Word) to something more secure (line 9).
- If you have not installed 7-Zip into its default installation directory (c:\program files\7-Zip) on your PC, change this path (line 8).
- Create a temporary directory on your hard drive for copied files: c:\t-backup\
- Change the path(s) of the directories that require backing up (at present, the script copies all files changed in two locations: c:\temp and c:\documents and settings\joe.bloggs\my documents\reference).
- Change the backup location directory path (\\ServerName\users\joe.bloggs\dbs) - ideally this should be an external drive/network location (line 10).
- Save the file in your text editor as 'dailybackup.bat'. Move this file to your chosen directory location, then add it to Windows Scheduled Tasks (in Control Panel) and set the time you wish the script to run (e.g. 17:25 every day).
- Thoroughly test your batch file (for testing, you need at least one file that has been created/changed today in the scanned directories; double click on the batch file in Windows Explorer to execute it) to make sure it runs without errors (test in command prompt window) and a .7z file appears in your backup location.
- When finalised, @echo off added at the start of the script will suppress the output of the command line window at run time (if you find the output distracting).
- Large files placed in the scanned directories may result in a large .7z file in the backup location - some file types such as PNGs hardly compress at all.
- If you are running Windows 2000 or XP Professional, encrypt the .bat file (right click > Properties > Advanced > Encrypt Contents... > OK) to make it more difficult for snoopers to view the password of your .7z backup files.
(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.)
'DrawString' Image PHP Class
A tiny PHP class to create images from text, using TTF fonts of choice.
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. € 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);
?>
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).