<?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();
?>
が、私は "localhostを:8000/Image.php" としての私のブラウザでこのファイルを開くかは "localhost:8000/Image.php = me.jpg?" 私はWAMPを使用して、ローカルホストサービスは、正しく私に取り組んでいます。 jpgファイルは、私のPHPファイル 'Image.php'とともにフォルダ内にあります。 それは私がクラス定義の外で使用したコードはなぜこのイメージは文字列から作成され、ブラウザでの表示は私にとってはうまくいかないのですか?
$image = new Image('me.jpg');
$image->display();
である。しかし、画像の周りのすべてのスペースは、テキストのような他のディスプレイのために使用できない。場合にのみ動作します
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!
このエラーが表示されます
GETパラメータは決して設定しません。 '$ _GET ['image']'!= 'localhost:8000/Image.php'でなく、' localhost:8000/Image.php?= me.jpg'です。 'localhost:8000/Image.php?image = me.jpg'を試してみてください。 – chris85
ライン57とライン13のどちらがハイライトされますか? – Manngo
あなたのコードを使用しましたが、空白の白い画面が表示されます。編集:今すぐ手に入れました:) – XztinctStephen