Skip to content

Instantly share code, notes, and snippets.

@MidnightLightning
Last active April 28, 2017 04:46
Show Gist options
  • Select an option

  • Save MidnightLightning/1ab7c38e8f79afb9641b to your computer and use it in GitHub Desktop.

Select an option

Save MidnightLightning/1ab7c38e8f79afb9641b to your computer and use it in GitHub Desktop.
Image generating utility functions
<?php
function textImage($msg = "No message") {
$char_size = 3; // Which standard font size?
$char_height = 35; // How tall is a line?
$char_width = 7; // How wide is each character?
// Determine width based on string length
$width = strlen($msg)*$char_width+30;
// Create a blank image to draw on
$im = @imagecreatetruecolor($width, $char_height) or die("Cannot Initialize new GD image stream");
$bg = imagecolorallocate($im, 250, 230, 230);
imagefill($im, 0, 0, $bg);
$text_color = imagecolorallocate($im, 233, 14, 91);
// Write messgae
imagestring($im, 3, 15, 10, $msg, $text_color);
// Export the final image
header ("Content-type: image/png");
imagepng($im);
imagedestroy($im);
}
function debug($msg) {
if (isset($_GET['debug']) || isset($_POST['debug'])) {
echo $msg;
}
}
putenv('GDFONTPATH='.realpath("../fonts/")); // Define where the fonts are
<?php
// Image transparency checker
if (empty($_GET['img'])) exit("-1"); // No file specified
$img_file = $_GET['img'];
if (!file_exists($img_file)) exit("-1"); // File doesn't exist
if (!is_readable($img_file)) exit("-1"); // Cannot read file
$img = imagecreatefrompng($img_file) or exit("-1");
if (!$img) exit("-1");
$x = (!empty($_GET['x']) && is_numeric($_GET['y']) && intval($_GET['x']) > 0)? intval($_GET['x']) : 0;
$y = (!empty($_GET['y']) && is_numeric($_GET['y']) && intval($_GET['y']) > 0)? intval($_GET['y']) : 0;
if ($x > imagesx($img)) exit("-1"); // too wide
if ($y > imagesy($img)) exit("-1"); // too tall
$index = imagecolorat($img, $x, $y);
if (imagecolorsforindex($img, $index) == NULL) {
$alpha = ($index >> 24) & 0xFF;
} else {
$color = imagecolorsforindex($img, $index);
$alpha = $color['alpha'];
}
if ($alpha > 102) {
exit("1");
} else {
exit("0");
}
<?php
// Create rotated text header
$msg = (isset($_GET['msg']) && $_GET['msg'] != "")? $_GET['msg'] : "Text Label";
$w = 6;
$h = 12;
$x = $h+2;
$y = $w*strlen($msg)+2;
$im = imagecreatetruecolor($x, $y);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 1, 1, $white);
imagestringup($im, 2, 0, $y-2, $msg, $black);
header ("Content-type: image/png");
imagepng($im);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment