Skip to content

Instantly share code, notes, and snippets.

@mlconnor
Created January 6, 2012 21:12
Show Gist options
  • Save mlconnor/1572426 to your computer and use it in GitHub Desktop.
Save mlconnor/1572426 to your computer and use it in GitHub Desktop.
PHP Grid Generator
<?php
/**
* This script will generate a grid overlay for
* a 960 style grid given three params, column_width, gutter_width, height.
* and columns. These will default if not provided. The result
* will be a PNG file. The height specifies the height of the image which
* defaults to 1.
*/
$expires = 60*60*24* 30;
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
header("Content-type: image/png");
$column_width = 60;
$gutter_width = 20;
$columns = 12;
$image_height = 1000;
if ( isset($_GET['column_width']) ) {
$column_width = $_GET['column_width'];
}
if ( isset($_GET['gutter_width']) ) {
$gutter_width = $_GET['gutter_width'];
}
if ( isset($_GET['columns']) ) {
$columns = $_GET['columns'];
}
if ( isset($_GET['height']) ) {
$image_height = $_GET['height'];
}
$grid_width = $columns * ($column_width + $gutter_width);
$image_height = 1;
$image_margin = 0;
$image_width = $grid_width + $image_margin * 2;
$image = @imagecreatetruecolor($image_width, $image_height);
imagealphablending($image, false);
imagesavealpha ($image, true);
//$grid_column_color = imagecolorresolve($image,255,229,229);
$grid_column_color = imagecolorallocatealpha($image, 255, 255, 255, 60);
$grid_margin_color = imagecolorallocatealpha($image, 74, 255, 255, 60);
$grid_colEdge_color = imagecolorallocatealpha($image, 202, 127, 127, 60);
$background_color = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefilledrectangle($image, 0, 0, $image_width, $image_height, $background_color);
$margin = $gutter_width / 2;
for ( $i = 0; $i < $columns; $i++ ) {
$left_margin = $i * ($gutter_width + $column_width) + $image_margin;
$left_col = $left_margin + $gutter_width / 2;
$right_col = $left_col + $column_width - 1;
$right_margin = $right_col + $gutter_width / 2;
//print "col $i : $left_margin <br/>\n";
imageline($image, $left_margin, 0, $left_margin, $image_height, $grid_margin_color);
imagefilledrectangle($image, $left_col, 0, $right_col, $image_height, $grid_column_color);
imageline($image, $left_col, 0, $left_col, $image_height, $grid_colEdge_color);
imageline($image, $right_col, 0, $right_col, $image_height, $grid_colEdge_color);
imageline($image, $right_margin, 0, $right_margin, $image_height, $grid_margin_color);
}
imagepng($image);
imagedestroy($image);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment