Why doesn’t my class draw a picture?
If i make it a only function it runs, but in class it doesnt work :(.
I am new in PHP classes (Java classes is not new for me).
<?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();
?>
The problem is you are declaring variables in
__construct, but they are local variables. As soon as__construct()finishes executing, it deletes all of the local variables. You must declare them as CLASS variables using the$thiskeyword, so that they are accessible to your other function.