<?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 " × ";
// 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.