为什么不画一幅画? (PHP)

时间:2012-07-16 17:37:46

标签: php draw

为什么我的班级不画画? 如果我使它成为一个唯一的功能它运行,但在课堂上它不起作用:(。 我是PHP类的新手(Java类对我来说并不新鲜)。

<?php

class Schild
{

    public function __construct(){
        $text = $_GET['text'];
        $picture = imagecreatefrompng("bild.png");
        $pika = imagecreatefromjpeg("pika.jpg");
        $pika_size = getimagesize("pika.jpg");
    }

    public function drawPicture()
    {
        $im = imagecolorallocate ($picture, 255, 0, 255);
        imagettftext($picture, 111, 0, 100, 100,$im , "Marmellata(Jam)_demo.ttf", $text);

        # int ImageCopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
        imagecopy($picture, $pika, 50, 50, 0, 0, $pika_size[0], $pika_size[1]);

        $zufall = rand(1,99999999);

        #header("Content-Type: image/jpeg");
        imagejpeg($picture);
        imagedestroy($picture);

    }
}

$schild1 = new Schild();
$schild1->drawPicture();
?>

1 个答案:

答案 0 :(得分:3)

<?php

class Schild
{
    protected $picture;
    protected $pika;
    protected $pika_size;
    protected $text;

    public function __construct(){
        $this->text = $_GET['text'];
        $this->picture = imagecreatefrompng("bild.png");
        $this->pika = imagecreatefromjpeg("pika.jpg");
        $this->pika_size = getimagesize("pika.jpg");
    }

    public function drawPicture()
    {
        $im = imagecolorallocate ($this->picture, 255, 0, 255);
        imagettftext($this->picture, 111, 0, 100, 100,$im , "Marmellata(Jam)_demo.ttf", $this->text);

        # int ImageCopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
        imagecopy($this->picture, $this->pika, 50, 50, 0, 0, $this->pika_size[0], $this->pika_size[1]);

        $zufall = rand(1,99999999);

        #header("Content-Type: image/jpeg");
        imagejpeg($this->picture);
        imagedestroy($this->picture);

    }
}

$schild1 = new Schild();
$schild1->drawPicture();
?>

问题是你在__construct中声明了变量,但它们是局部变量。只要__construct()完成执行,它就会删除所有局部变量。您必须使用$this关键字将它们声明为CLASS变量,以便您的其他函数可以访问它们。