2012-08-10 73 views
-1

我的劇本相當多的作品,但是當我運行一個for循環失敗,在視圖中:安全圖像失敗 - 笨

<?php foreach($pics as $index=>$pic){?> 
      <td class='Pictures'> 
       <center> 

       <?php 

       $piece = explode('/',$pic["ThumbImg"]); 
       $string = $piece[5]; 



       ?> 
       <img src='<?php echo base_url(); ?>index.php/Controller/getImage/<?php echo $string; ?>/1' width="100px"> 

       </a> 

Controller類 - 獲取圖像功能:

//function to protect images from being accessed directly by obfuscating the URL 
    function getImage($img_id,$type){ 

    if($this->members->logged_in()){  

      //code to authenticate user goes here then... 

    //code to decide which type of file it is/thumb/full size image 
    if($type==1) 
    { 
    $url = $this->data['base_url'].'system/application/images/pic/thumbs/'; 
    }else if($type==0){ 

    $url = $this->data['base_url'].'system/application/images/pic/'; 

    } 

    $filepath = $url.$img_id; 


    //send image to web browser. 
     header("Content-type: image/jpeg"); 

     //get path 
      $img_handle = imagecreatefromjpeg($filepath) or die(""); 
     //create and send  
     ImageJpeg($img_handle); 

    } 

我懷疑這可能是由於標題被重新發送,如果是的話,我該如何解決這個問題。

乾杯

解決了這一問題 - 用「盜鏈」

+0

所以你的代碼在不在循環中工作?你有什麼錯誤嗎?你怎麼知道它不起作用 – 2012-08-10 13:39:53

+0

基本上Web服務器卡住了。當它最終執行頁面時,不會獲取圖像。 – thejoker 2012-08-10 14:11:03

回答

0

你基本上只需要設置的頭信息header("Content-type: image/jpeg")如果你加載從數據庫中的圖像。但是,既然你從一個文件夾加載它,你可能想跳過這個php文件,並立即從文件夾中獲取圖像:

<img src='<?php echo base_url(); ?>img_folder/<?php echo $string; ?>/1' 
width="100px"> 
+0

我不能那樣做。通過控制器取得它的全部要點是,我可以掩蓋從哪裏獲取圖像。這樣做,將放棄原始圖像路徑。這打破了整個觀點。 – thejoker 2012-08-10 16:03:40