Skip to content

Instantly share code, notes, and snippets.

@sshongru
Created June 6, 2014 06:26
Show Gist options
  • Save sshongru/9c40e70137f0e1635586 to your computer and use it in GitHub Desktop.
Save sshongru/9c40e70137f0e1635586 to your computer and use it in GitHub Desktop.
PHP Script that generates a JPG
<?php
//
// Creates a JPG of the given size and with the given text in
// the center.
//
// parameters
// TODO: Is this secure enough?
$text = $_REQUEST['t'];
$width = $_REQUEST['w'];
$height = $_REQUEST['h'];
$fontSize = 5;
// add a little variety
$adjust = intval($text) * 8;
//Create the image resource
$image = ImageCreate($width, $height);
//We are making three colors, white, black and gray
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$blue = ImageColorAllocate($image, 0, 51, 0 + $adjust);
$grey = ImageColorAllocate($image, 204, 204, 204);
// draw a black background
ImageFill($image, 0, 0, $blue);
// draw the text in the center
$textWidth = imagefontwidth($fontSize)*strlen($text);
$textHeight = imagefontheight($fontSize);
$vCenter = ceil($height / 2);
$hCenter = ceil($width / 2);
$x = $hCenter - (ceil($textWidth/2));
$y = $vCenter - (ceil($textHeight/2));
ImageString($image, $fontSize, $x, $y, $text, $white);
// draw an offset border
ImageRectangle($image, 1, 1, $width - 2, $height - 2, $white);
// generate the image
header("Content-Type: image/jpeg");
ImageJpeg($image);
ImageDestroy($image);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment