|
Posted by ZeldorBlat on 04/17/07 16:28
On Apr 17, 11:55 am, e_matt...@hotmail.com wrote:
> Hello everyone,
>
> I'm trying to make a class that will generate images. I can get it to
> work if the class and the instance of the class is in the same file.
> However, I can't get it to work if I save the class as its own file,
> and try to make an instance of it in another file. Any thoughts?
>
> Here is the working version:
>
> <?php
>
> header("Content-type: image/png");
> $imageRes = new Image(200, 300);
> $imageRes->colorImage(255,0,0);
> $image = $imageRes->getImage();
> imagepng($image);
>
> class Image {
>
> private $imageRes;
>
> function __construct($widthIn, $heightIn) {
> $this->imageRes = imageCreate($widthIn, $heightIn);
> }
>
> function colorImage($rIn,$gIn,$bIn) {
> $red = imagecolorallocate( $this->imageRes, $rIn,$gIn,$bIn );
> }
>
> function getImage() {
> return $this->imageRes;
> }
>
> } // END class
>
> ?>
>
> Thanks for any help.
> -Eric
Make sure you require/include the file that contains the class
definition. If the class is in a file called Image.php then you might
have a page that looks like this:
require('Image.php');
header("Content-type: image/png");
$imageRes = new Image(200, 300);
$imageRes->colorImage(255,0,0);
$image = $imageRes->getImage();
imagepng($image);
Also look into __autoload() -- it will make things much easier when
you have a lot of classes.
<http://www.php.net/autoload>
Navigation:
[Reply to this message]
|