Class to generate images

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • e_matthes@hotmail.com

    #1

    Class to generate images

    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($wi dthIn, $heightIn) {
    $this->imageRes = imageCreate($wi dthIn, $heightIn);
    }

    function colorImage($rIn ,$gIn,$bIn) {
    $red = imagecoloralloc ate( $this->imageRes, $rIn,$gIn,$bIn );
    }

    function getImage() {
    return $this->imageRes;
    }

    } // END class

    ?>

    Thanks for any help.
    -Eric

  • ZeldorBlat

    #2
    Re: Class to generate images

    On Apr 17, 11:55 am, e_matt...@hotma il.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($wi dthIn, $heightIn) {
    $this->imageRes = imageCreate($wi dthIn, $heightIn);
    }
    >
    function colorImage($rIn ,$gIn,$bIn) {
    $red = imagecoloralloc ate( $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>

    Comment

    • e_matthes@hotmail.com

      #3
      Re: Class to generate images

      On Apr 17, 8:28 am, ZeldorBlat <zeldorb...@gma il.comwrote:
      On Apr 17, 11:55 am, e_matt...@hotma il.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($wi dthIn, $heightIn) {
      $this->imageRes = imageCreate($wi dthIn, $heightIn);
      }
      >
      function colorImage($rIn ,$gIn,$bIn) {
      $red = imagecoloralloc ate( $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("I mage.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($wi dthIn, $heightIn) {
      $this->imageRes = imageCreate($wi dthIn, $heightIn);
      }
      function colorImage($rIn ,$gIn,$bIn) {
      $red = imagecoloralloc ate( $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

      Comment

      • Toby A Inkster

        #4
        Re: Class to generate images

        e_matthes wrote:
        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."
        Is there anything outside your <?php ... ?block? Any whitespace?

        Could you provide a URL?

        --
        Toby A Inkster BSc (Hons) ARCS
        Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

        Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

        * = I'm getting there!

        Comment

        • e_matthes@hotmail.com

          #5
          Re: Class to generate images

          On Apr 18, 1:10 am, Toby A Inkster <usenet200...@t obyinkster.co.u k>
          wrote:
          e_matthes wrote:
          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."
          >
          Is there anything outside your <?php ... ?block? Any whitespace?
          >
          Could you provide a URL?
          >
          --
          Toby A Inkster BSc (Hons) ARCShttp://tobyinkster.co. uk/
          Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
          >
          * = I'm getting there!

          Thank you for the whitespace suggestion. I thought whitespace only
          made a problem at the beginning of a file, so I hadn't noticed the two
          blank lines at the end of each file. Works now! Thanks again to both
          of you.

          Comment

          Working...