Skip to content

Instantly share code, notes, and snippets.

@andrewwoods
Last active December 1, 2022 04:30
Show Gist options
  • Save andrewwoods/6734588 to your computer and use it in GitHub Desktop.
Save andrewwoods/6734588 to your computer and use it in GitHub Desktop.
Create wallpapers with random patterns for your desktop.
<?php
/**
* The base class for all Wallpaper subclasses
*
* @package Wallpapers
* @access public
*/
class BaseWallpaper {
/** a handle to the image */
protected $image;
/** the color palette */
protected $palette;
/** a buffer of empty space around the image */
protected $margin;
/**
* Constructor
*
* Create a new Wallpaper object
*
* @access public
* @param resource $image a handle to the image resource
* @param int $width the width of the image
* @param int $height the width of the image
* @return void
*/
public function __construct( $image, $width, $height ) {
$this->image = $image;
$this->width = $width;
$this->height = $height;
$this->margin = 0;
}
/**
* Add a palette to the image
*
* @access public
* @param Palette $pallete contains all the color values
* @return void
*/
public function addPalette( $palette ){
$this->palette = $palette;
}
/**
* base draw function. This must be overridden in subclass
*
* @access public
* @return void
*/
public function draw(){
throw new Exception( 'You must override this function.' );
}
/**
* Write the image resource to a JPG file
*
* @access public
* @param string $filename
* @return void
*/
public function save( $filename ) {
imagejpeg( $this->image, $filename );
imagedestroy( $this->image );
}
/**
* Wrapper function around draw() and save()
*
* @access public
* @param string $filename
* @return void
*/
public function generate( $filename ){
$this->draw();
$this->save( $filename );
}
}
<?php
/**
* Class to generate a image of randomized dots
*
* @package Wallpapers
* @access public
*/
class DotsWallpaper extends BaseWallpaper {
/**
* Create the pattern of randomized dots
*
* @return void
*/
public function draw()
{
$margin = $this->margin;
imagefill( $this->image, 0, 0, $this->palette->getColor('black'));
imagefilledrectangle($this->image,
$margin,
$margin,
$this->width,
$this->height - $margin,
$this->palette->getColor('dark_aqua')
);
$dots = array('aqua', 'dark_gold', 'dark_amber', 'dark_purple');
$dots_length = count($dots);
for ( $i = 0, $color_index = 0; $i < two_8; $i++, $color_index++ )
{
if ($color_index >= $dots_length)
{
$color_index = 0;
}
$dotColor = $dots[$color_index];
$radius = rand(4, two_5);
$center = $this->create_center( $radius );
$this->drawDot( $center, $radius, $this->palette->getColor( $dotColor ) );
}
}
/**
* Set the margin, which is representsed as a border around the image
*
* @param int $margin
* @return void
*/
public function setMargin( $margin ){
$this->margin = $margin;
}
/**
* Random create the center of a dot, and account for the use of margins
*
* @param int $radius
* @return array
*/
public function create_center( $radius ){
$xMin = $this->margin + $radius;
$xMax = $this->width - $this->margin - $radius;
$yMin = $this->margin + $radius;
$yMax = $this->height - $this->margin - $radius;
// TODO: replace this array with an object of the Point class
return array(
'x' => rand( $xMin, $xMax ),
'y' => rand( $yMin , $yMax )
);
}
/**
* Draw a dot
*
* @param array $center
* @param int $radius determines how big the circle should be
* @return void
*/
public function drawDot( $center, $radius, $dotColor ){
imagefilledellipse(
$this->image,
$center['x'],
$center['y'],
$radius,
$radius,
$dotColor
);
}
}
<?php
/**
* Class to generate a randomized set of lines
*
* @package Wallpapers
* @access public
*/
class LinesWallpaper extends BaseWallpaper {
/**
* Create a randomized pattern using GD library
*
* @uses drawLine()
* @access public
* @return void
*/
public function draw() {
$margin = $this->margin;
imagefill($this->image, 0, 0, $this->palette->getColor('dark_gray'));
$colors = array('light_gray', 'aqua', 'yellow', 'green', 'pink');
$colorsLength = count( $colors );
for ( $i = 0, $colorIndex=0; $i < two_8; $i++, $colorIndex++ )
{
if ($colorIndex >= $colorsLength)
{
$colorIndex = 0;
}
$color = $colors[ $colorIndex ];
$start = new Point( rand(0, $this->width), 0 );
$stop = new Point( rand(0, $this->width), $this->height );
$this->drawLine( $start, $stop, $this->palette->getColor( $color ) );
}
}
/**
* Draw a line of specified color
*
* @access public
* @param Point $start
* @param Point $stop
* @param resource $color
* @return void
*/
public function drawLine( $start, $stop, $color ){
imageline($this->image, $start->x, $start->y, $stop->x, $stop->y, $color );
}
}
<?php
/**
* Class for managing colors
*
*
*
* @package Wallpapers
* @access public
*/
class Palette {
private $image;
private $color = [];
/**
* Constructor
*
* Creates a new palette object
*
* @param resource $image
* @return void
*/
public function __construct( $image ){
$this->image = $image;
$this->addColor('black', 0, 0, 0);
$this->addColor('dark_gray', 32, 32, 32);
$this->addColor('light_gray', 205, 205, 205);
$this->addColor('red', 168, 0, 0);
$this->addColor('yellow', 168, 168, 0);
$this->addColor('blue', 0, 0, 168);
$this->addColor('green', 0, 168, 0);
$this->addColor('dark_aqua', 4, 39, 49);
$this->addColor('dark_gold', 83, 87, 7);
$this->addColor('dark_amber', 87, 55, 7);
$this->addColor('dark_purple', 55, 14, 87);
$this->addColor('pink', 205, 14, 180);
$this->addColor('aqua', 7, 169, 187);
$this->addColor('white', 230, 230, 230);
}
/**
* Register a color in the palette
*
* @uses public
* @param String $name
* @param int $red
* @param int $green
* @param int $blue
* @return void
*/
public function addColor( $name, $red, $green, $blue ){
$this->color[ $name ] = imagecolorallocate($this->image, $red, $green, $blue);
}
/**
* Returns the resource for a known color
*
* @uses public
* @param String $name
* @return resource
*/
public function getColor( $name ){
return $this->color[ $name ];
}
}
<?php
/**
* Point represents an X and Y coordinate
*
* @package Wallpapers
* @access public
*/
class Point {
/** @var X-coordinate for a pixel in an image */
public $x;
/** @var Y-coordinate for a pixel in an image */
public $y;
/**
* Constructor
*
* Create a new Point object
*
* @param String $one a necessary parameter
* @param String optional $two an optional value
* @return void
*/
public function __construct( $x, $y ){
$this->x = $x;
$this->y = $y;
}
/**
* Magic method. Return a string representing this object
*
* Create a string of this object when it's used in a string object
*
* @return string
*/
public function __toString(){
echo "x={$this->x}, y={$this->y}";
}
}
#!/usr/bin/php
<?php
/**
* Wallpaper Generator - creates wallpapers based on randomness.
*
* @package Wallpapers
* @author Andrew Woods
*/
require "palette.php";
require "point.php";
require "base-wallpaper.php";
require "dots-wallpaper.php";
require "lines-wallpaper.php";
require "web-wallpaper.php";
$prog = array_shift($argv);
$style = array_shift($argv);
$filename = array_shift($argv);
if ( ! $filename ){
echo "No filename specified.";
help();
exit(0);
}
define('two_4', pow(2,4)); // 16
define('two_5', pow(2,5)); // 32
define('two_6', pow(2,6)); // 64
define('two_7', pow(2,7)); // 128
define('two_8', pow(2,8)); // 256
define('two_9', pow(2,9)); // 512
define('two_10', pow(2,10)); // 1024
// dimensions is pixels for a Macbook Pro
$width = 2560;
$height = 1600;
// Create an image
$bgpaper = imagecreate($width, $height);
$palette = new Palette( $bgpaper );
switch ($style)
{
case 'dots':
$dots = new DotsWallpaper( $bgpaper, $width, $height );
$dots->addPalette( $palette );
$dots->generate( $filename );
break;
case 'lines':
$lines = new LinesWallpaper( $bgpaper, $width, $height );
$lines->addPalette( $palette );
$lines->generate( $filename );
break;
case 'vpoint':
$vPoint = new VanishingPointWallpaper( $bgpaper, $width, $height );
$vPoint->addPalette( $palette );
$vPoint->setInterval( 64 );
$vPoint->generate( $filename );
break;
default:
echo 'you must specify a style of wallpaper';
exit(0);
stuff;
}
/*------------------------------------------------------------------------------
* Functions
*------------------------------------------------------------------------------
*/
function help()
{
?>
Help
================================================================================
$ wallpaper.php (dots|lines|vpoint) filename.jpg
<?php
}
<?php
/**
* Wallpaper subclass that creates a vanishing point image
*
* @package Wallpapers
* @access public
*/
class VanishingPointWallpaper extends BaseWallpaper {
/**
* Create the pattern and write it to the resource
*
* @return void
*/
public function draw() {
$margin = $this->margin;
imagefill($this->image, 0, 0, $this->palette->getColor('dark_gray'));
$colors = array('light_gray', 'aqua', 'yellow', 'green', 'pink');
$colorsLength = count( $colors );
$step = floor( $this->width / $this->interval );
for ( $i = 0, $colorIndex=0; $i <= $this->interval; $i++, $colorIndex++ )
{
if ($colorIndex >= $colorsLength)
{
$colorIndex = 0;
}
$color = $colors[ $colorIndex ];
$width = $i * $step;
$start = new Point( $width, 0 );
$stop = new Point( $this->width - $width, $this->height );
$this->drawLine( $start, $stop, $this->palette->getColor( $color ) );
}
}
/**
* Draw line from $start to sto with a known $color
*
* @param Point $start
* @param Point $stop
* @param resource $color
* @return void
*/
public function drawLine( $start, $stop, $color ){
imageline($this->image, $start->x, $start->y, $stop->x, $stop->y, $color );
}
/**
* Determine the number of lines to be draw
*
* @param int $interval
* @return void
*/
public function setInterval( $interval ){
$this->interval = $interval;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment