<?php
/***********************************************************************
* FILE: rand_sig.php *
* AUTHOR: NinjaGeek *
* LICENSE: GNU Public License http://www.gnu.org/licenses/gpl.html *
***********************************************************************/
// define the directory where the signature images are being stored
define("SIG_DIR", "signatures");
// define fonts directory for custom text
// only .ttf Fonts supported
define("FONT_DIR", "fonts");
// define the default font for custom text strings
define("DEFAULT_FONT", "");
// configuration array
$config = array(
'resize' => TRUE, // resize all images to given size
'width' => 650, // resize widtht
'height' => 200, // resize height
'text' => "NinjaGeek", // custom string
'generate_text' => TRUE, // generate custom string
'use_custom_font' => TRUE, // generate cusomt string with custom font
'random_font' => TRUE, // use random font
'text_size' => 22, // font size
'text_pos_x' => 50, // font position left
'text_pos_y' => 50, // font position top
'text_angle' => 0 // string rotation angle
);
// check if generate attribute was changed with the GET method
if(isset($_GET['generate']) && $_GET['generate'] == "avatar") {
$config["generate"] = $_GET['generate']; // add new config member
foreach($_GET as $index => $define) {
// for each per GET defined member redeclare the old one
$config[$index] = (isset($_GET[$index])) ? $_GET[$index] : $config[$index];
}
// define new path form images to load
$new_path = (!array_key_exists("avatar_dir", $config)) ? SIG_DIR : $_GET['avatar_dir'];
define("AVA_DIR", $new_path);
}
// scan signatures directory
if(isset($new_path)) {
// if AVA_DIR was defined update path for use
$new_path = AVA_DIR;
}else{
$new_path = SIG_DIR;
}
$signatures = scandir($new_path);
$clear_array = array();
// loop through signatures and clean it form unneeded entries
foreach($signatures as $value) {
// if entry is file
if(is_file($new_path."/".$value)) {
$clear_array[] = $value; // add it to a clean array
}
}
$signatures = $clear_array; // update signatures list
unset($clear_array); // delete $clear_array
//set random signature number to signatures amount
$random_max = (count($signatures) - 1);
$random = rand(0, $random_max);
// define path to the signature
$image_path = $new_path."/".$signatures[$random];
// get image data
if(!@$size = getimagesize($image_path)) {
// if getimagesize failed kill the script
die("Error, getimagesize() failed!");
}
// look what image resource to create
switch($size['mime']) {
case "image/gif":
// create from gif
$image = imagecreatefromgif($image_path);
break;
case "image/jpeg":
// create from jpeg
$image = imagecreatefromjpeg($image_path);
break;
case "image/png":
// create from png
$image = imagecreatefrompng($image_path);
break;
default:
// if mime is not one of the 3 above kill the script
die("Not supported image format: {$size['mime']}");
}
// if resize is TRUE
if($config['resize']) {
// create new resource with new sizes
$resize_im = imagecreatetruecolor($config['width'], $config['height']);
// copy original image to new size
imagecopyresampled($resize_im, $image, 0, 0, 0, 0, $config['width'], $config['height'], $size[0], $size[1]);
$image = $resize_im; // redeclare $image
}
if($config['generate_text']) {
// create rgb color resource
$color = imagecolorallocate($image, 255,0,0);
// if random fonts are enabled
$fonts = array(); // new array for fonts
// if random font is enabled
if($config['random_font']) {
// scan fonts directory
foreach(scandir(FONT_DIR) as $font) {
// if is file add it to font list
if(is_file(FONT_DIR."/".$font)) {
$fonts[] = $font;
}
}
// get max number for random font
$font_max = (count($fonts) - 1);
// random font id
$random_font = rand(0, $font_max);
}
// if use_custom_font is enabled
if($config['use_custom_font']) {
// define font path
$font = (@$random_font) ? $fonts[$random_font] : DEFAULT_FONT;
$font = FONT_DIR."/".$font; // update font path
// create custom string
imagettftext($image, $config['text_size'], $config['text_angle'], $config['text_pos_x'], $config['text_pos_y'], $color, $font, $config['text']);
}else{
// create custom string
imagestring($image, $config['text_size'], $config['text_pos_x'], $config['text_pos_y'], $config['text'], $color);
}
}
// define header as image/png
header("Content-type: image/png");
imagepng($image); // output image
imagedestroy($image); // clear memory
?>