Jump to content

Drawing Images in classes


ctechguy

Recommended Posts

I'm quite new to Object Oriented PHP. What I'm trying to do is draw some images but also have other content on the page.

 

include('images.php');
//New Image set
$im=new images();

//Sends headers and images don't work
echo "hello";

//New rect
$us=$ge->rect("Title");
$us->setDimensions(300,300);
$us->setBgColor('#606060');
$us->createImage();

 

There are 2 classes; images which extends rect. In the createImage() it draws an image on index.php?img=0 but only if no other headers are sent yet.

function createImage(){
if ($_GET['img']==$this->id){

...

imagepng($this->im);
ImageDestroy ($im);}

 

Is there anyway to draw this objects and also have content. I tried to see if I could draw the images through the images class like images.php?img=0 but that didn't work. I was also thinking if there was a way to send an if back to index from constructor and destructor. This is probably really confusing sorry, an help is appreciated

Link to comment
https://forums.phpfreaks.com/topic/185388-drawing-images-in-classes/
Share on other sites

You can play around with output buffering but its not really the easiest thing until you've had some experience using it as its not really intuitive. Use the example I give you here. You can output html, create an image and display it and continue on. All without an error.

 

*But if you move the comments up one line you will output the image and stop. There is an implied exit() after the use of imagedestroy() if you use an image header.

 

<?php

echo "hello";

//header("Content-type: image/gif");

$im = imagecreate(200, 200);

$green = imagecolorallocate($im, 0, 255, 0);

$red = imagecolorallocate($im, 255, 0, 0);

$blue = imagecolorallocate($im, 0, 0, 255);

imagerectangle($im, 20, 20, 120, 90, $green);

imagerectangle($im, 50, 60, 160, 120, $red);

imagefilltoborder($im, 61, 61, $red, $blue);

imagegif($im,'c:/home/tester2.gif');

//imagegif($im);

imagedestroy($im);

 

?>

<br>An image<br>

<img src="http://localhost/tester2.gif" border=0>

<br>

We can continue on!

 

 

HTH

Teamatomic

Might want to look into image caching though, as this will hog server resources. An example being:

 

function isCached($filename) {
    if(file_exists($filename) && (time() - filemtime($filename)) < 3600) {
        return true;
    } else {
        return false;
    }
}

if(!isCached('images/tester2.gif')) {
    generateImage('images/tester2.gif');
}

echo "<img src=\"images/tester2.gif\" />";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.