2017-03-26 8 views
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(); 
?> 

が、私は "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! 

このエラーが表示されます

+0

GETパラメータは決して設定しません。 '$ _GET ['image']'!= 'localhost:8000/Image.php'でなく、' localhost:8000/Image.php?= me.jpg'です。 'localhost:8000/Image.php?image = me.jpg'を試してみてください。 – chris85

+0

ライン57とライン13のどちらがハイライトされますか? – Manngo

+0

あなたのコードを使用しましたが、空白の白い画面が表示されます。編集:今すぐ手に入れました:) – XztinctStephen

答えて

0

57行目のエラーメッセージは、$_GET[image]が存在しないことを示しています。これは、クエリ文字列?image=…を送信していないことを意味します。

クエリ文字列は、method="get"のリンクまたはフォームから来ている可能性があります。あなたは物事の終わりを確認する必要があります。

$_GET配列は大文字と小文字が区別されるので、あなただけimage=下部ケースと一致することを覚えておいてください。

は、有効なデータでスクリプトが呼び出されているとは限りません。少なくとも、if(isset($_GET['image'])) …のようなものを含める必要があります。

また、ユーザーデータを使用してシステム上のファイルを開いています。これにより、セキュリティ上の問題が発生する可能性があります。 basenameのようなものを使用してパスを含まない単一のファイル名への入力を減らし、実際のファイルだけを安全なディレクトリから読み取るようにして、ダメージを制限する必要があります。

関連する問題