<?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 */ class Token { /** Kod na tokenie */ var $_tokenText; /** Adres folderu czcionek */ var $_fontDir = ''; /** Nazwy czcionek */ 'georgia.TTF' ); /** Typ obrazka */ var $_imgType = 'gif'; /** Długość kodu na tokenie */ var $_length; /** Kolor tła */ var $_backColor = '#ffffff'; /** Kolor czcionki */ var $_fontColor = '#466900'; /** Kolor elementów tłą */ var $_elemsColor = '#E1E1E1'; /** Szerokość tokena */ var $_width; /** Wyskość tokena */ var $_height = 25; /** Obiekt tła */ var $_bgObject; /** Obrazek tokena */ var $_image; /** Nazwa klucza sesji z tokenem */ var $_tokenId = 'token'; /** Dozwolone rozszerzenia */ '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 */ function Token( $length, $bgObject ){ $this->_length = $length; $this->_width = $length * 15 + 10; $this->_image = imagecreate( $this->_width, $this->_height); $this->_bgObject = $bgObject; } /** * Tworzy token, zapisuje go w sesji i wyświetla. */ function getToken(){ // kolorki $this->_backColor = $this->MakeColor( $this->_image, $this->_backColor ); $this->_fontColor = $this->MakeColor( $this->_image, $this->_fontColor ); $this->_elemsColor = $this->MakeColor( $this->_image, $this->_elemsColor ); $this->Background(); $this->Text(); $_SESSION[$this->_tokenId] = $this->_tokenText; $this->Display(); } /** * Tworzy tekst na tokenie */ function Text(){ } 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(){ call_user_func( 'image' . $this->_imgType, $this->_image ); } /** * Dodaje czcionki to tablicy. Są one potem losowo używane w tokenie. */ function assignFonts(){ foreach( $fonts as $font ){ else $this->_fontsFiles[] = $font; } } /** * 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 ){ ); return imagecolorallocate( $img, $return['r'], $return['g'], $return['b']); } } class TokenBackground_Dots { function process( $image, $params ){ $pts[] = $x.'_'.$y; } else { $i--; } } } } class TokenBackground_Slashes { function process( $image, $params ){ $step = 5; imageline( $image, $i, 0, 0, $i, $params['_elemsColor'] ); } } } class TokenBackground_Grid { function process( $image, $params ){ // poziome linie imageline( $image, 0, $i, $params['_width'], $i, $params['_elemsColor'] ); // pionowe linie imageline( $image, $i, 0, $i, $params['_height'], $params['_elemsColor'] ); } } } $token = new Token(32, new TokenBackground_Dots ); $token->getToken(); ?>
Klasa tokena.
Najważniejsze cechy:
- Proste tworzenie nowych efektów tła,
- duża konfigurowalność działania klasy.
Przykładowe efekty tła:
<?php /** Ładne kółeczka - zgapiłem z jakiegoś innego tokena ;) */ class TokenBackground_Dots { function process( $image, $params ){ $pts[] = $x.'_'.$y; } else { $i--; } } } } /** Ukośne paski */ class TokenBackground_Slashes { function process( $image, $params ){ $step = 5; imageline( $image, $i, 0, 0, $i, $params['_elemsColor'] ); } } } /** Najzwyklejsza kratka */ class TokenBackground_Grid { function process( $image, $params ){ // poziome linie imageline( $image, 0, $i, $params['_width'], $i, $params['_elemsColor'] ); // pionowe linie imageline( $image, $i, 0, $i, $params['_height'], $params['_elemsColor'] ); } } } ?>
Sposób użycia:
Kod
<?php
// ... tutaj klasa tokena
// ... tutaj klasa efektu
$token = new Token(20, new TokenBackground_Dots );
$token->getToken();
?>
// ... tutaj klasa tokena
// ... tutaj klasa efektu
$token = new Token(20, new TokenBackground_Dots );
$token->getToken();
?>
.