![]() |
![]() |
![]()
Post
#1
|
|
Grupa: Zarejestrowani Postów: 422 Pomógł: 0 Dołączył: 14.12.2005 Skąd: Wałbrzych Ostrzeżenie: (0%) ![]() ![]() |
Klasa tokena. Najważniejsze cechy: - Proste tworzenie nowych efektów tła, - duża konfigurowalność działania klasy. Przykładowe efekty tła:
Sposób użycia: Kod <?php // ... tutaj klasa tokena // ... tutaj klasa efektu $token = new Token(20, new TokenBackground_Dots ); $token->getToken(); ?> . Ten post edytował kwiateusz 19.05.2007, 16:19:57 |
|
|
![]() |
![]()
Post
#2
|
|
Grupa: Zarejestrowani Postów: 184 Pomógł: 6 Dołączył: 23.02.2008 Skąd: Katowice Ostrzeżenie: (0%) ![]() ![]() |
Kod <?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 */ var $_fontFiles = array( 'georgia.TTF' ); /** Typ obrazka */ var $_imgType = 'gif'; /** Długość kodu na tokenie */ var $_length; /** Kolor tła */ var $_backColor = '#ffffff'; /** Kolor czcionki */ var $_fontColor = '#A61D09'; /** 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 */ var $_allowedTypes = array( 'gif', 'jpg', 'png' ); /** Komunikaty błędów */ var $_errorMsg = array( '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 randcolor() { $r = rand(10,195); $g = rand(10,195); $b = rand(10,195); return imagecolorallocate( $this->_image, $r, $g, $b); } function Token( $length, $bgObject ){ if($bgObject=='rand') { $Rand = rand(1,3); switch ($Rand) { case 1: $bgObject = new TokenBackground_Dots; break; case 2: $bgObject = new TokenBackground_Slashes; break; case 3: $bgObject = new TokenBackground_Grid; break; } } $this->_length = $length; $this->_tokenText = substr(md5(uniqid(time())), 0 - $length); $this->_width = $length * 15 + 10; $this->_image = imagecreate( $this->_width, $this->_height); if( !is_object( $bgObject ) ) trigger_error( $this->_errorMsg['wrong_object'], E_USER_WARNING ); $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; if( !in_array( $this->_imgType, $this->_allowedTypes ) ) trigger_error( sprintf( $this->_errorMsg['wrong_img_type'], $this->_imgType ), E_USER_ERROR ); 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, 18 + rand(-3, 3), $this->randcolor(), $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 ) ) trigger_error( sprintf( $this->_errorMsg['wrong_img_type'], $this->_imgType ), E_USER_ERROR ); call_user_func( 'image' . $this->_imgType, $this->_image ); } /** * Dodaje czcionki to tablicy. Są one potem losowo używane w tokenie. */ function assignFonts(){ $fonts = func_get_args(); foreach( $fonts as $font ){ if( !file_exists( $this->getFontDir() . $font ) ) trigger_error( sprintf( $this->_errorMsg['wrong_file'], $font ), E_USER_ERROR ); 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 ){ if( !preg_match('/^[#]{0,1}[a-f0-9]{6}$/i', $color ) ) trigger_error( sprintf( $this->_errorMsg['wrong_color'], $color ), E_USER_ERROR ); $color = str_replace('#', '', $color); $return = array( 'r' => hexdec(substr($color, 0, 2)), 'g' => hexdec(substr($color, 2, 2)), 'b' => hexdec(substr($color, 4, 2)) ); return imagecolorallocate( $img, $return['r'], $return['g'], $return['b']); } } class TokenBackground_Dots { function process( $image, $params ){ $pts = array(); for($i = 0; $i < round($params['_width'] / 1.5); $i++) { $x = rand(0, $params['_width'] ); $y = rand(0, $params['_height'] ); if( !in_array( $x.'_'.$y, $pts ) ){ 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'] ); } } } $token = new Token(6, 'rand' ); $token->getToken(); ?> Małe udoskonalenie kodu. Losuje tło oraz kolor czcionki. |
|
|
![]() ![]() |
![]() |
Aktualny czas: 7.10.2025 - 13:39 |