Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > PHP: Общие вопросы > ООП: код вызывает ошибку


Автор: NNaarreekk 23.12.2007, 15:31
Код

<?PHP
class Button
{
    private $image;
    public function __construct($width, $height, $label, $font)
    {
        $this -> image = imagecreate($width, $height);
        $colorBody = imagecolorallocate($this -> image, 0x99, 0x99, 0x99);
        $colorShadow = imagecolorallocate($this -> image, 0x33, 0x33, 0x33);
        $colorHighlight = imagecolorallocate($this -> image, 0xCC, 0xCC, 0xCC);

        imagefilledrectangle($this -> image, 1, 1, $width-2, $height-2, $colorBody);
        imageline($this -> image, 0, $height-1, $width-1, $height-1, $colorShadow);
        imageline($this -> image, $width-1, 1, $width-1, $height-1, $colorShadow);
        imageline($this -> image, 0, 0, $width-1, 0, $colorHighlight);
        imageline($this -> image, 0, 0, 0, $height-2, $colorHighlight);

        $labelHeight = imagefontheight($font);
        $labelWidth = imagefontwidth($font)*strlen($label);
        $labelX = ($width - $labelWidth)/2;
        $labelY = ($height - $labelHeight)/2;
        imagestring($this -> image, $font, $labelX+1, $labelY+1, $label, $colorShadow);
        imagestring($this -> image, $font, $labelX, $labelY, $label, $colorHighlight);
    }
    public function drawPNG(){
        header("Content-type: image/png");
        imagepng($this -> image);
    }
    public function drawJPEG(){
        header("Content-type: image/jpeg");
        imagejpeg($this -> image);
    }
}
if(!isset($_REQUEST['width']))$_REQUEST['width'] = 100;
if(!isset($_REQUEST['height']))$_REQUEST['height'] = 30;
if(!isset($_REQUEST['label']))$_REQUEST['label'] = mt_rand(10000, 99999);
if(!isset($_REQUEST['font']))$_REQUEST['font'] = 5;

$b = new Button ($_REQUEST['width'], $_REQUEST['height'], $_REQUEST['label'], $_REQUEST['font']);
$b -> drawPNG();
?>


В книге нашел такой код!
Дает ошибку
Цитата

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in d:\appserv\www\forum\createnums.php on line 4


В чем проблема????

 ! 
skyboy
Каждому вопросу - отдельную тему. С вменяемым названием.

Автор: gta4kv 23.12.2007, 15:44
У меня все нормально:

http://89.169.190.239/test.php

Вот ваш код в действии.

Автор: flashaa 23.12.2007, 15:45
Цитата(NNaarreekk @  23.12.2007,  15:31 Найти цитируемый пост)
private $image;

Судя по всему версия PHP 4.х, модификатор private введен только в PHP5.

По поводу ф-ции - http://ru2.php.net/manual/ru/function.strip-tags.php

Автор: gta4kv 23.12.2007, 15:52
Да кстати, ИМХО использовать лучше $b->drawJpeg(); ( причем установить ему низкой качество ) ибо качество картинки становиться ниже, и текст опознается ботами хуже. хотя может это и бред %)

upd

Немного модифицировал этот скрипт:

Код

<?PHP
class Button
{
    var $image;
    public function __construct($width, $height, $label, $font)
    {
        $this -> image = imagecreate($width, $height);
        $colorBody = imagecolorallocate($this -> image, 0x99, 0x99, 0x99);
        $colorShadow = imagecolorallocate($this -> image, 0x33, 0x33, 0x33);
        $colorHighlight = imagecolorallocate($this -> image, 0xCC, 0xCC, 0xCC);
        $colorBalck = imagecolorallocate( $this->image, 0, 0, 0 );
        
        imagefilledrectangle($this -> image, 1, 1, $width-2, $height-2, $colorBody);
        imageline($this -> image, 0, $height-1, $width-1, $height-1, $colorShadow);
        imageline($this -> image, $width-1, 1, $width-1, $height-1, $colorShadow);
        imageline($this -> image, 0, 0, $width-1, 0, $colorHighlight);
        imageline($this -> image, 0, 0, 0, $height-2, $colorHighlight);
        imageline( $this->image, imagesx( $this->image ), 0, 0, imagesy( $this->image ), $colorBalck );
        $labelHeight = imagefontheight($font);
        $labelWidth = imagefontwidth($font)*strlen($label);
        $labelX = ($width - $labelWidth)/2;
        $labelY = ($height - $labelHeight)/2;
        imagestring($this -> image, $font, $labelX+1, $labelY+1, $label, $colorShadow);
        imagestring($this -> image, $font, $labelX, $labelY, $label, $colorHighlight);
        imageline( $this->image, 0, 1, imagesx( $this->image ) , imagesy( $this->image ), $colorBalck );
        
        return $label;
    }
    
    public function drawPNG()
    {
        header("Content-type: image/png");
        imagepng($this -> image);
    }
    public function drawJPEG()
    {
        header("Content-type: image/jpeg");
        imagejpeg($this -> image, '', 80 );
    }
    public function drawGIF()
    {
        header("Content-type: image/gif");
        imagegif($this -> image);
    }
}
$alphavit = 'qwertyuiopasdfghjklzxcvbnm';

for( $i = 0; $i<6; $i++) 
{
    $word .= $alphavit{ rand(0, strlen( $alphavit )-1 ) };
}

$b = new Button ( '100', '30', $word, 4     );
$b->drawJpeg();
?>

Автор: flashaa 23.12.2007, 17:20
gta4kv, public в PHP 4.х так же как и private нет. 

Автор: gta4kv 23.12.2007, 17:33
flashaa, я как бы не правил пример под PHP 4.

Вот под PHP 4:

Код

<?PHP
class Button
{
    var $image;
    function __construct($width, $height, $label, $font)
    {
        $this -> image = imagecreate($width, $height);
        $colorBody = imagecolorallocate($this -> image, 0x99, 0x99, 0x99);
        $colorShadow = imagecolorallocate($this -> image, 0x33, 0x33, 0x33);
        $colorHighlight = imagecolorallocate($this -> image, 0xCC, 0xCC, 0xCC);
        $colorBalck = imagecolorallocate( $this->image, 0, 0, 0 );
        
        imagefilledrectangle($this -> image, 1, 1, $width-2, $height-2, $colorBody);
        imageline($this -> image, 0, $height-1, $width-1, $height-1, $colorShadow);
        imageline($this -> image, $width-1, 1, $width-1, $height-1, $colorShadow);
        imageline($this -> image, 0, 0, $width-1, 0, $colorHighlight);
        imageline($this -> image, 0, 0, 0, $height-2, $colorHighlight);
        imageline( $this->image, imagesx( $this->image ), 0, 0, imagesy( $this->image ), $colorBalck );
        $labelHeight = imagefontheight($font);
        $labelWidth = imagefontwidth($font)*strlen($label);
        $labelX = ($width - $labelWidth)/2;
        $labelY = ($height - $labelHeight)/2;
        imagestring($this -> image, $font, $labelX+1, $labelY+1, $label, $colorShadow);
        imagestring($this -> image, $font, $labelX, $labelY, $label, $colorHighlight);
        imageline( $this->image, 0, 1, imagesx( $this->image ) , imagesy( $this->image ), $colorBalck );
        
        return $label;
    }
    
    function drawPNG()
    {
        header("Content-type: image/png");
        imagepng($this -> image);
    }
    function drawJPEG()
    {
        header("Content-type: image/jpeg");
        imagejpeg($this -> image, '', 80 );
    }
    function drawGIF()
    {
        header("Content-type: image/gif");
        imagegif($this -> image);
    }
}
$alphavit = 'qwertyuiopasdfghjklzxcvbnm';
for( $i = 0; $i<6; $i++) 
{
    $word .= $alphavit{ rand(0, strlen( $alphavit )-1 ) };
}
$b = new Button ( '100', '30', $word, 4     );
$b->drawJpeg();
?>

Немного доработал этот класс, получил более менее надежную капчу. 
http://89.169.190.239/project/captcha/?generate

Автор: flashaa 23.12.2007, 17:40
Цитата(gta4kv @  23.12.2007,  17:33 Найти цитируемый пост)
я как бы не правил

Почему тогда private исчезло и вместо него появилось var?  smile 

Автор: NNaarreekk 23.12.2007, 19:03
gta4kv, а теперь дает:

Цитата

<b>Warning</b>:  imagejpeg(): supplied argument is not a valid Image resource in <b>d:\appserv\www\forum\createnums.php</b> on line <b>39</b><br />


Не пойму в чем дело smile 

Автор: gta4kv 23.12.2007, 19:04
Попробуй заменить ее на:

Код

   imagejpeg($this -> image );

Автор: NNaarreekk 23.12.2007, 19:07
Не а!

То же сомое:(

Автор: gta4kv 23.12.2007, 19:09
Попробуйте тогда не $b->drawJpeg(); а $b->drawPNG();

Автор: NNaarreekk 23.12.2007, 19:12
То же самое..
и с PNG и с GIF...

A тут: http://89.169.190.239/project/captcha/?generate

Тот же код что здесь?

Автор: flashaa 23.12.2007, 19:13
Для php4 также слово __construct ничего не говорит smile
Ваша картинка просто не создается... т.к.вызов  new не запускает __construct.
Переименуйте __construct в Button.

Автор: NNaarreekk 23.12.2007, 19:15
А теперь Х показыват!

Автор: gta4kv 23.12.2007, 19:16
Цитата(NNaarreekk @ 23.12.2007,  19:12)
То же самое..
и с PNG и с GIF...

A тут: http://89.169.190.239/project/captcha/?generate

Тот же код что здесь?

За основу взят он ^_^. Там я переделал, скоро выложу ввиде статьи.. Если надо скину линк в личку потом.  smile 

Цитата

А теперь Х показыват! 


Прям "Х"?  smile 

ps. вы бы обновили AppServ, там же есть версия с PHP5. ;)

Автор: NNaarreekk 23.12.2007, 19:20
Все сработало smile  smile  smile 

Просто я там что-то лишнее написал!smile


gta4kv, flashaa, спасибо!!!

Автор: NNaarreekk 25.12.2007, 20:55
Люди, а теперь как достать эту случайную цифру???

Автор: gta4kv 25.12.2007, 21:06
Цитата(NNaarreekk @ 25.12.2007,  20:55)
Люди, а теперь как достать эту случайную цифру???

Я тебе в http://forum.vingrad.ru/act-Msg/CODE-01.html скинул ссылку, там ссылка на мою статью, там про это есть.

Автор: NNaarreekk 25.12.2007, 21:29
Теперь со спокойной душой закрываю тему!!! smile 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)