0
<?php
class Image {

        //class attributes
        private $image;
        private $width;
        private $height;
        private $mimetype;

    function __construct($filename) {

        //read the image file to a binary buffer
        $fp = fopen($filename, 'rb') or die ("Image ". $filename ."not found!"); //file pointer -> open an image file
        $buf =''; //empty string to hold the data
        while(!feof($fp)) //here the whole file will be read 
            $buf .= fgets($fp, 4096); //concatenate the data read with the newly created string contents $buf

        //create image & assign it to variable
        $this->image = imagecreatefromstring($buf); //takes a binary string of data as input

        //extract image info
        $info = getimagesize($filename);
        $this->width = $info[0];
        $this->height = $info[1];
        $this->mimetype = $info['mime'];
    }

    // public function imgWidth() {
    //     echo $this->width;
    // }

    // public function imgHeight() {
    //     echo $this->height;
    // }

    // public function imgMimetype() {
    //     echo $this->mimetype;
    // }

    public function display() { //method to display the image
        header('Content-type: {$this->mimetype}');
        switch ($this->mimetype) {
            case 'image/jpeg': imagejpeg($this->image); break;
            case 'image/png': imagepng($this->image); break;
            case 'image/gif': imagegif($this->image); break;
        }
        // exit;
    }
}
// $image = new Image("me.jpg");
// echo $image->imgWidth();
// echo " &times ";
// echo $image->imgHeight();
// echo '<br/>';
// echo $image->imgMimetype();
// echo "<br/><br/>";
$displayImage = new Image($_GET['image']);
// $image->display();
?>

I open this file in my browser as "localhost:8000/Image.php" or "localhost:8000/Image.php?=me.jpg" I use WAMP and localhost service is working correctly, me.jpg file is located in the folder together with my php file 'Image.php'. It displays this error

Notice: Undefined index: image in C:\wamp64\www\sky1\Image.php on line 57

Warning: fopen(): Filename cannot be empty in 
C:\wamp64\www\sky1\Image.php on line 13
Image not found!

It only works if the code I use outside the class definition is

$image = new Image('me.jpg');
$image->display();

But all spaces around the picture are unusable for other displays like text.

1 Answers1

0

The error message you’re getting in line 57 indicate that $_GET[image] doesn’t exist. This means that you’re not sending the query string ?image=….

The query string could have come from a link or a form with method="get". You will need to check that end of things.

Remember that the $_GET array is case sensitive, so you will only match image= in lower case.

In general, you should never assume that your script is being called with valid data. At the very least, you should include something like if(isset($_GET['image'])) ….

Also, you are using user data to open a file on you system. This can lead to security problems. You should limit the damage by using something like basename to reduce the input to a single filename without path, and only read the actual file from within a safe directory.

Manngo
  • 14,066
  • 10
  • 88
  • 110