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. |
|
|
|
Balon Generator Tokenów 19.05.2007, 14:19:07
hwao Kod wygląda super:)
Ale prosił bym o dodanie np j... 19.05.2007, 22:56:01
grzesio Jak to uruchomić ?
nie działa mi ten skrypt !
... 20.05.2007, 23:26:06
my salsa ciekawa klasa, ale mi brakuje dwoch rzeczy:
- nie ... 20.05.2007, 23:53:51 
Balon Cytat(my salsa @ 20.05.2007, 22:53:51... 23.05.2007, 09:45:56
ChowiX Prosze napisac tak dokładniej ja uży... 28.06.2007, 14:22:08
Spirit86 musisz jeszcze skopiować obiekt tła:
[PHP] pobier... 30.06.2007, 18:53:14
Balon Cytatdo pliku i wiem ze cos zle zrobilem bo nie dz... 2.07.2007, 15:20:07
graft Kurczeeeee, fajnie, tylko że klasy nigdy nie były ... 30.07.2007, 22:29:53
Balon Standardowo poprzez $_SESSION['token... 31.08.2007, 09:33:49
Sedziwoj Na pewno wywalił bym ustawienie długości do set... 1.09.2007, 21:23:02
ActivePlayer [PHP] pobierz, plaintext <?phpclass TokenChecke... 16.12.2007, 22:13:58
Taifun czemu jak dam
[PHP] pobierz, plaintext <?php... 8.03.2008, 15:13:25
MiFlo Nie wyświetla Ci tego co wypisałeś bo nagłówki zos... 19.05.2008, 19:47:41
kacpereczek Mam takie pytanie:
Czy moglibyście pokazać jak wst... 15.08.2008, 17:21:51 
.radex Cytat(kacpereczek @ 15.08.2008, 18:21... 15.08.2008, 21:28:26 
kacpereczek Cytat(.radex @ 15.08.2008, 22:28:26 )... 16.08.2008, 13:58:04
.radex hmmm...
jak masz powiedzmy obrazek o nazwie token... 16.08.2008, 14:42:55
kacpereczek Bardzo poproszę o przykładowy kod, ale nie na bazi... 18.08.2008, 11:06:54
hinduseek Nie polecam tego zabezpieczenia na boty. Przechodz... 19.10.2008, 23:10:46
DonJeday Dobra klasa elegancko działa, ale powiedzcie mi ja... 26.10.2008, 10:35:48
hinduseek [PHP] pobierz, plaintext <?php$token = new Toke... 27.10.2008, 22:56:19
smarcz Nie wiem dlaczego ale nie zapisuje mi się do SESJI... 19.07.2009, 22:02:18
skowron-line @smarcz pokaż kod może czegoś ci brakuje
np. [PHP... 19.07.2009, 22:06:11 ![]() ![]() |
|
Aktualny czas: 28.12.2025 - 02:03 |