<?php $znaki="abcdef"; $test01=imagecreatetruecolor(400,30); $wb2=imagecolorallocate($test01,255,33,66); imagefill($test01,0,0,$wb2); $text_color=imagecolorallocate($test01,0,0,20); imagettftext($test01 , 33 , 0 , 20 , 20 , $text_color, "font.ttf" , $znaki ); imagepng($test01); ?>
przeglądarka internetowa prawidłowo wyświetla obrazek.
Jeśli natomiast utworzę następujący plik o nazwie captcha1.php:
<?php require("captcha2.php"); $x01=new captcha; ?>
oraz definicję klasy captcha w pliku captcha2.php:
[/php]
<?php class captcha { private $image_handle; private $image_token; private $color_background; private $color_text; private $font_loc; //1. Tworzenie rysunku private function new_image($w,$h) { $this->image_handle=imagecreatetruecolor($w,$h); } //2. Deklaracja koloru private function declare_color($r,$g,$b) { $test01=imagecolorallocate($this->image_handle,$r,$g,$b); return $test01; } //3. Tło obrazka private function add_background($color_handle) { imagefill($this->image_handle,0,0,$color_handle); } //4. Lokalizacja czcionki private function font_localise() { $this->font_loc="font.ttf"; } //5. Pisanie tekstu private function write($text,$font_size,$x,$y,$color_handle,$font_loc) { imagettftext($this->image_handle,$font_size,0,$x,$y,$color_handle,$font_loc,$text); } //6. Kończenie pracy z rysunkiem private function finish() { imagepng($this->image_handle); } //7. Generowanie łańcucha znaków private function generate_chain() - ta funkcja generuje losowy łańcuch znaków i zapisuje go we właściwości image_token //8. Deklaracja kolorów private function declare_color_array() { $this->color_background=$this->declare_color(255,33,66); $this->color_text=$this->declare_color(99,00,204); } //9. Tworzenie obrazka private function create_image_1() { $this->new_image(200,50); $this->declare_color_array(); $this->add_background($this->color_background); $this->generate_chain(); $_SESSION[captcha] = $this->image_token; $this->font_localise(); $this->write($this->image_token,34,20,10,$this->color_text,$this->font_loc); } //10. Tworzenie obrazka #2 private function create_image_2() { $this->create_image_1(); $this->finish(); } //11. Konstruktor public function __construct(){$this->create_image_2();} }
po wywołaniu pliku captcha1.php obrazek się nie pojawia.
Dlaczego?
Czy instrukcje generujące obrazek muszą koniecznie być zapisane bezpośrednio, nie mogą zostać ukryte w klasie lub bibliotece funkcji?