You are here: Re: Class to generate images « PHP Programming Language « IT news, forums, messages
Re: Class to generate images

Posted by e_matthes on 04/18/07 04:15

On Apr 17, 8:28 am, ZeldorBlat <zeldorb...@gmail.com> wrote:
> 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>


I think this is what I tried. When I put the class in a separate
file, this is what I have:

<?php

// simple_phptests.php
include_once("Image.php");
header("Content-type: image/png");
$imageRes = new Image(200, 300);
$imageRes->colorImage(255,0,0);
$image = $imageRes->getImage();
imagepng($image);

?>

<?php

// Image.php
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

?>

This works for all the other classes I have created, but not for this
image class. The message I get when I load simple_phptests.php is:

"The image simple_phptests.php cannot be displayed, because it
contains errors."

Any other ideas? Thanks

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация