[PHP] Zufällige Signatur und Avatar


Autor Nachricht
 Betreff des Beitrags: [PHP] Zufällige Signatur und Avatar
BeitragVerfasst: 03.01.2010, 15:10 
Offline   Zorniger Feuersturm
Zorniger Feuersturm
Benutzeravatar

Registriert: 10.10.2007, 00:08
Beiträge: 219
Wohnort: 137 Vozdovac
Also ich habe aus langeweile ein kleines PHP script geschrieben, und um meinen Post count bisschen zu steigern poste ich das script mal hier für euch.
Es liest die bilder aus dem vor gegebenen ordner = SIG_DIR..
Ihr hab auch die möglichkeit eine text zeile auf das bild zu generieren und auch noch mit verschiedenen fonts. FONT_DIR
Die font dateien müssen .ttf sein.
Das ganze wird durch das array $config gesteuert, die namen sind offensichtlich das es keine grossen erklärungs worte braucht...

Nur wenn ihr es zum avatar beutzen wollt müsst ihr das script per GET method aufrufen....Danach könnt ihr auch über die GET methode alle $config mitglieder ändern..

Hoffe ihr habt spass mit dem ding... :D

Code:
rand_sig.php?generate=avatar&resize=1&width=120&height=120&generate_text=1&avatar_dir=ava&text=Rise&text_size=14&text_pos_x=5&text_pos_y=110


Code:

<?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

?>



Support Forums


Nach oben
 Profil E-Mail senden  
 
 Betreff des Beitrags: Re: [PHP] Zufällige Signatur und Avatar
BeitragVerfasst: 06.01.2010, 19:47 
Offline   Administrator
Administrator
Benutzeravatar

Registriert: 02.03.2006, 19:56
Beiträge: 4571
Wohnort: Bayern
Hört sich cool an! Mal was neues! :)
Mal sehne, ob ich irgendwann dazu kommen werde das Ding auszuprobieren, die Idee finde ich allerdings sehr gut ;)


Nach oben
 Profil E-Mail senden  
 
 Betreff des Beitrags: Re: [PHP] Zufällige Signatur und Avatar
BeitragVerfasst: 06.01.2010, 22:06 
Offline   Zorniger Feuersturm
Zorniger Feuersturm
Benutzeravatar

Registriert: 10.10.2007, 00:08
Beiträge: 219
Wohnort: 137 Vozdovac
Super, freut mich das es dir gefällt....
Für fragen bin ich immer da ;)



Support Forums


Nach oben
 Profil E-Mail senden  
 

Beiträge der letzten Zeit anzeigen:   Sortiere nach  

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast
cron

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Deutsche Übersetzung durch phpBB.de