Cytat(my salsa @ 20.05.2007, 22:53:51 )

ciekawa klasa, ale mi brakuje dwoch rzeczy:
- nie mozna powiekszyc czcionki w prosty sposob
- kolor czcionki moglby byc w tablicy i kolor kazdej literki losowo (IMG:
http://forum.php.pl/style_emoticons/default/smile.gif)
-----
jeszcze skoro mozna podac folder z czcionkami to po co je wymieniac? mozna by przeciez automatycznie pobierac do tablicy wszystkie z danego katalogu
hmmm dzięki za pomysł ; Jak to uruchomić ?
nie działa mi ten skrypt !) będzie chwila to dorobie takie funkcje, bo pomysł nie jest zły (IMG:
http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
Cytat
Jak to uruchomić ?
nie działa mi ten skrypt !
A to zrobiłeś ? Drugi argument dla konstruktora to obiekt tła
<?php
$token = new Token(20, new TokenBackground_Dots );
$token->getToken();
?>
Dodałem wszystko o czym pisaliście prócz wielkości czcionek (IMG:
http://forum.php.pl/style_emoticons/default/winksmiley.jpg) Dzisiaj wieczorem jak będzie czas to to zrobię (IMG:
http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
<?php
/**
* License
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**/
/**
* @author Krzysztof (Balon) Jagiełło <balonyo@gmail.com>
* @copyright 2007 Krzysztof Jagiełło.
* @version 1.0
* @license <a href="http://opensource.org/licenses/lgpl-license.php" target="_blank">http://opensource.org/licenses/lgpl-license.php</a> GNU Lesser General Public License
*/
class Token {
/** Kod na tokenie */
var $_tokenText;
/** Adres folderu czcionek */
var $_fontDir = '';
/** Nazwy czcionek */
var $_fontFiles = array();
/** Automatyczne ładowanie czcionek */
var $_autoFont = true;
/** Typ obrazka */
var $_imgType = 'gif';
/** Długość kodu na tokenie */
var $_length;
/** Kolor tła */
var $_backColor = '#ffffff';
/** Kolor czcionki */
'#466900'
);
/** Kolor elementów tłą */
var $_elemsColor = '#E1E1E1';
/** Szerokość tokena */
var $_width;
/** Wyskość tokena */
var $_height = 30;
/** Obiekt tła */
var $_bgObject;
/** Obrazek tokena */
var $_image;
/** Nazwa klucza sesji z tokenem */
var $_tokenId = 'token';
/** Dozwolone rozszerzenia */
var $_allowedTypes = array( 'gif', 'jpg', 'png'
);
/** Komunikaty błędów */
'wrong_color' => 'Format koloru (%s) jest nieprawidłowy.',
'wrong_object' => 'Podany obiekt nie istnieje.',
'wrong_img_type'=> 'Podany typ ("%s") jest nieprawidłowy.',
'wrong_file' => 'Czcionka "%s" nie istnieje .'
);
/**
* Konstruktor klasy
*
* @param integer $length - długość tekstu na tokenie
* @param object $bgObject - obiekt efektu tła
* @param bool $autoFont - automatyczne pobieranie czcionek z katalogu
*/
function Token( $length, $bgObject, $autoFont = false ){
$this->_length = $length;
$this->_width = $length * 15 + 10;
$this->_image = imagecreate( $this->_width, $this->_height);
trigger_error( $this->_errorMsg
['wrong_object'], E_USER_WARNING
); $this->_bgObject = $bgObject;
$this->_autoFont = $autoFont ? true : false;
}
/**
* Tworzy token, zapisuje go w sesji i wyświetla.
*/
function getToken(){
// kolorki
$this->_backColor = $this->MakeColor( $this->_image, $this->_backColor );
$this->_elemsColor = $this->MakeColor( $this->_image, $this->_elemsColor );
for( $i = 0; $i < count( $this->_fontColor
); $i++ ) $this->_fontColor[$i] = $this->MakeColor( $this->_image, $this->_fontColor[$i] );
$this->Background();
if( $this->_autoFont )
$this->autoLoadFonts();
$this->Text();
$_SESSION[$this->_tokenId] = $this->_tokenText;
if( !in_array( $this->_imgType
, $this->_allowedTypes
) ) header( 'Content-type: image/' . $this->_imgType
); $this->Display();
}
/**
* Tworzy tekst na tokenie
*/
function Text(){
for($i = 0; $i < strlen($this->_tokenText
); $i++){ imagettftext
( $this->_image
, rand(14
, 16
), rand(-10
, 10
), rand(3
, 5
) + $i * 15
, 20
+ rand(-3
, 3
), $this->_fontColor
[rand(0
,count($this->_fontColor
)-1
)], $this->getFontDir() . $this->_fontFiles
[rand(0
,count($this->_fontFiles
)-1
)], $this->_tokenText
{$i}); }
return true;
}
/**
* Tworzy tło korzystając z podanej klasy tła
*
* @return true
*/
function Background(){
$this->_bgObject->process( &$this->_image, get_object_vars( $this ) );
return true;
}
/**
* Wyświetla token w wybranym formacie
*/
function Display(){
if( !in_array( $this->_imgType
, $this->_allowedTypes
) ) call_user_func( 'image' . $this->_imgType, $this->_image );
}
/**
* Dodaje czcionki do tablicy. Są one potem losowo używane w tokenie.
*/
function assignFonts(){
foreach( $fonts as $font ){
else
$this->_fontFiles[] = $font;
}
}
/**
* Dodaje kolory czcionek do tablicy. Są one potem losowo używane w tokenie.
*/
function assignFontsColors(){
$this->_fontColor
= array(); foreach( $colors as $color ){
if( !preg_match('/^[#]{0,1}[a-f0-9]{6}$/i', $color ) ) else
}
}
/**
* Automatycznie ładuje pliki czcionek
* z określonego folderu
*/
function autoLoadFonts(){
$dir = dir( $this->_fontDir
); while( false !== ( $font = $dir->read() ) ){
}
$dir->close();
}
/**
* Zwraca katalog z czcionkami
*/
function getFontDir(){
return $this->_fontDir == '' ? '' : $this->_fontDir . '/';
}
/**
* Zwraca kolor dla obrazka
*
* @param resource $img
* @param string $color - kolor w formacie hexagonalny
* @return integer
*/
function MakeColor( $img, $color ){
if( !preg_match('/^[#]{0,1}[a-f0-9]{6}$/i', $color ) )
);
return imagecolorallocate( $img, $return['r'], $return['g'], $return['b']);
}
}
class TokenBackground_Dots {
function process( $image, $params ){
for($i = 0; $i < round($params['_width'] / 1
.5
); $i++) { $x = rand(0
, $params['_width'] ); $y = rand(0
, $params['_height'] );
imageellipse
( $image, $x, $y, rand(2
, 7
), rand(3
, 6
), $params['_elemsColor'] ); $pts[] = $x.'_'.$y;
}
else {
$i--;
}
}
}
}
class TokenBackground_Slashes {
function process( $image, $params ){
$step = 5;
for($i = 0; $i < round($params['_width']) + ($step*10
); $i+= $step) { imageline( $image, $i, 0, 0, $i, $params['_elemsColor'] );
}
}
}
class TokenBackground_Grid {
function process( $image, $params ){
for($i = 0; $i < round($params['_width']); $i+= 5) { // poziome linie
imageline( $image, 0, $i, $params['_width'], $i, $params['_elemsColor'] );
// pionowe linie
imageline( $image, $i, 0, $i, $params['_height'], $params['_elemsColor'] );
}
}
}
?>
Przykład użycia różnych fontów oraz kolorów czcionek:
Kod
require_once( 'token.class.php' );
$token = new Token(20, new TokenBackground_Dots, true );
$token->_fontDir = 'fonts/';
$token->assignFontsColors( '#466900', '#000000', '#777777' );
$token->getToken();
Cytat
Ale prosił bym o dodanie np jakiś 4 przykładowych (z tłem/bez) tokenów wygenerowanych i na
http://imageshack.us/ przyjemnie było by zobaczyć i uniknie się wielu "pytań".
Nie zauważyłem Twojego posta (IMG:
http://forum.php.pl/style_emoticons/default/winksmiley.jpg) Oczywiście, proszę bardzo !
http://bambo.pl/tmp/token/example.php
Ten post edytował Balon 23.05.2007, 12:57:40