Created
June 6, 2014 06:26
-
-
Save sshongru/9c40e70137f0e1635586 to your computer and use it in GitHub Desktop.
PHP Script that generates a JPG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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