How to update the Windows wallpaper using batch

Dion Moult

2011-01-22

Over Christmas one of my more humourous gifts to my parents was to allow them to remotely nag each other electronically. Since my dad is often overseas, this actually has some practical use.

The idea was to create a remotely synchronised dynamic wallpaper with text that could be set by another person remotely. Person A would type in some text, a wallpaper with the text formatted would be generated, Person B's computer would detect that there is an update, download the wallpaper and set it immediately. (I originally wanted to make a pop up message, but realised that having "Go and exercise!" pop up during a powerpoint presentation with your boss wasn't the best thing)

The system would operate as such: I would create a html form on my webserver to allow somebody to type in text. PHP would take the text and use GD to generate a .jpg file of an image with the text overlayed on top. Batch file on Windows computer would download the .jpg file (either on startup, or via cronw) via URL2FILE, which you can download URL2File here. Batch file will call imagemagick installed on the Windows computer to convert .jpg to .bmp because apparently that's what Windows likes for wallpaper formats and converting on the server would mean a ultra big file download. Finally, batch file will tweak the registry to change the wallpaper and "refresh" it such that it changes immediately.

Here's an example :)

An example of a nagging wallpaper

PHP code:

<?php
if (isset($_POST['submit']) && isset($_POST['nag']) && !empty($_POST['nag'])) {
    $width = 1280;
    $height = 800;
    $imgname = "wallpaper_blank.jpg"; # The empty blue background template
    $im = imagecreatefromjpeg ($imgname);
    $text = $_POST['nag'];
    $textcolor = ImageColorAllocate($im, 255, 255, 255);
    $font = 20;
    $font_width = ImageFontWidth($font);
    $font_height = ImageFontHeight($font);
    $font_width = 10;
    $text_width = $font_width * strlen($text);
    $position_center = ceil(($width - $text_width) / 2);
    $text_height = $font_height;
    $position_middle = ceil(($height - $text_height) / 2);
    imagettftext ($im, 15, 0, $position_center, $position_middle, $textcolor,'/path/to/ttf/fontfile/AllOverAgainAllCaps.ttf', $text); # We're offsetting this a little to give space for desktop icons
    Imagejpeg($im, '/path/to/final/image/wallpaper.jpg', 100);
    chmod('/path/to/final/wallpaper.jpg', 0644);
    echo 'Nag done!';
} else {
    echo '<form action="" method="post">';
    echo '<textarea name="nag" rows="10" cols="50"></textarea><br />';
    echo '<input type="submit" name="submit" value="Nag!">';
    echo '</form>';
}

Batchfile code:

C:\path\to\URL2FILE.EXE http://mysite.com/wallpaper.jpg C:\path\to\save\wallpaper.jpg
C:\path\to\imagemagick\convert.exe C:\path\to\save\wallpaper.jpg C:\path\to\save\wallpaper.bmp
REG ADD "HKCU\Control Panel\Desktop" /V Wallpaper /T REG_SZ /F /D "C:\path\to\save\wallpaper.bmp"
REG ADD "HKCU\Control Panel\Desktop" /V WallpaperStyle /T REG_SZ /F /D 2
REG ADD "HKCU\Control Panel\Desktop" /V TileWallpaper /T REG_SZ /F /D 0%SystemRoot%\System32\RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters

Have fun!

Comments

If you have any comments, please send them to dion@thinkmoult.com.