Skip to content

Instantly share code, notes, and snippets.

@andreaganduglia
Created April 30, 2019 12:06
Show Gist options
  • Save andreaganduglia/ed334f95c5f66a62b174428eb50c8a9e to your computer and use it in GitHub Desktop.
Save andreaganduglia/ed334f95c5f66a62b174428eb50c8a9e to your computer and use it in GitHub Desktop.
Create image with solid background and random polygons with PHP and GD Library
<?php
// set image width and height
$w = 400;
$h = 300;
// randoms coords for polygons
$coords = [];
foreach(range(0,127) as $p){
$coords[] = rand(0,$w);
$coords[] = rand(0,$h);
}
// create image
$image = imagecreatetruecolor($w, $h);
// fill the background
imagefilledrectangle($image, 0, 0, $w, $h, imagecolorallocate($image, mt_rand(0,255) , mt_rand(0,255) , mt_rand(0,255)));
// draw some polygons
imagefilledpolygon($image, $coords, 48, imagecolorallocate($image, mt_rand(0,255) , mt_rand(0,255) , mt_rand(0,255)));
imagefilledpolygon($image, $coords, 24, imagecolorallocate($image, mt_rand(0,255) , mt_rand(0,255) , mt_rand(0,255)));
header('Content-type: image/png');
imagepng($image);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment