Skip to content

Instantly share code, notes, and snippets.

@shgysk8zer0
Created February 10, 2017 06:28
Show Gist options
  • Save shgysk8zer0/d63cd906c202dc232f03895de381ad64 to your computer and use it in GitHub Desktop.
Save shgysk8zer0/d63cd906c202dc232f03895de381ad64 to your computer and use it in GitHub Desktop.
Generate random images in PHP

Generate random images in PHP

sample

Why?

  • Because you can
  • Because you have time to spare
  • Maybe you think they look cool or want to save them for wallpapers or something
  • Maybe it reminds you of that one time... You know which time I'm talking about

Requirements

  • PHP >= 7.0
  • GD image processing library, which you probably already have
  • Some time to waste
<?php
/**
* @author Chris Zuber
* @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3 (GPL-3.0)
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace RandomImageGenerator;
/**
* Image width
* @var integer
*/
const X = 1920;
/**
* Image height
* @var integer
*/
const Y = 1200;
/**
* Number of shapes to draw
* @var integer
*/
const SHAPES = 500;
/**
* Various available styles for arcs
* @var Arary
*/
const ARC_STYLES = [
IMG_ARC_PIE,
IMG_ARC_CHORD,
IMG_ARC_EDGED,
IMG_ARC_NOFILL
];
/**
* Create a random polygon with number of points between 0 & $max_pts
* @param resource $im The image reource
* @param integer $max_pts Max number of point to use
* @return void
*/
function random_polygon($im, Int $max_pts = 20)
{
$color = imagecolorallocatealpha($im, ...random_color_alpha());
$pts = random_pts(random_int(3, $max_pts));
$num = count($pts) / 2;
imagefilledpolygon($im, $pts, $num, $color);
}
/**
* Creates a random arc of a random color
* @param resource $im The image resource
* @return void
*/
function random_arc($im)
{
$cx = random_int(0, X);
$cy = random_int(0, Y);
$w = random_int(0, X);
$h = random_int(0, Y);
$s = random_int(0, 360);
$e = random_int(0, 360);
$col = imagecolorallocatealpha($im, ...random_color_alpha());
$style = ARC_STYLES[random_int(0, 3)];
imagefilledarc($im, $cx, $cy, $w, $h, $s, $e, $col, $style);
}
/**
* Draws a randomly colored pixel at a random location
* @param resource $im The image resource
* @return void
*/
function random_pixel($im)
{
$color = imagecolorallocate($im, ...random_color_alpha());
$x = random_int(0, X);
$y = random_int(0, Y);
imagesetpixel($im, $x, $y, $color);
}
/**
* Generates an array of random alpha color values.
* @return Array [r, g, b, a]
*/
function random_color_alpha(): Array
{
return [
random_int(0, 255),
random_int(0, 255),
random_int(0, 255),
random_int(0, 127)
];
}
/**
* Generates an array of random color values
* @return Array [r, g, b]
*/
function random_color(): Array
{
return [random_int(0, 255), random_int(0, 255), random_int(0, 255)];
}
/**
* Generates a set of random points for a polygon [x1,y1, x2,y2,...]
* @param integer $length Number of sets of points to generate
* @return Array The resulting array of points
*/
function random_pts($length): Array
{
$pts = [];
for($n = 0; $n < $length; $n++) {
$pts[] = random_int(0, X);
$pts[] = random_int(0, Y);
}
return $pts;
}
header('Content-type: image/png');
$im = imagecreatetruecolor(X, Y);
for ($i = 0; $i < SHAPES; $i++) {
switch(random_int(1, 2)) {
case 1:
random_arc($im);
break;
case 2:
random_polygon($im);
break;
}
}
imagepng($im);
imagedestroy($im);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment