2011-08-01 104 views
4

我想用兩種功能創建兩個具有不同大小的縮略圖。上傳圖片() 使uploadIhumb($ name){它在第一個函數內調用}創建小縮略圖。 第一個功能創建縮略圖但第二個不起作用。 請解決問題並突出顯示問題出在哪裏?CodeIgniter圖像縮略圖問題

enter code here: 
function uploadImage() 
     { 
            $imageName2=$_FILES['file']['tmp_name']; 


       $config['image_library'] = 'gd2'; 
       //$config['source_image'] = $imageName2;// this is temporary folder where image place on uploading time 
       $config['create_thumb'] = TRUE; 
       $config['maintain_ratio'] = false; 
       $config['width']  = 696; 
       $config['height'] = 241; 
       $config['new_image'] = 'images/uploads'; 
       $this->load->library('image_lib', $config); 
       $this->image_lib->resize(); 
       $file_name= basename($imageName2, ".tmp"); 
       $filename=$file_name.'_thumb.tmp'; 


     ////////////////////Rename code/////////////////////////// 
       $name= $this->rand_string(6); 

       $dir=$_SERVER['DOCUMENT_ROOT'].'/images/uploads/'; 

       rename($dir . $filename, $dir . $name); 
       echo $name; 
       $this->uploadIhumb($name); //FUNCTION CALL 
} 

function uploadIhumb($name) 
    { 

       $config['image_library'] = 'gd2'; 
       $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/images/uploads/'.$name; 
       $config['create_thumb'] = TRUE; 

       $config['width']  = 80; 
       $config['height'] = 80; 
       $config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'images/uploads/thumbs/'; 
       $this->load->library('image_lib', $config); 
       $this->image_lib->resize(); 


} 

回答

5

:-( 問題所需的任何其他圖書館沒有需要可如果我們載入的圖片庫只有一次,解決在函數開始 只有初始化配置爲第二拇指成功的代碼作爲下運輸。: -

{ 功能createThumb1($ imageName)//文件名通過 {

 // this thumbnail created 
    $config['image_library'] = 'gd2'; 
    $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/'.$imageName; 

    $config['create_thumb'] = TRUE; 
    $config['maintain_ratio'] = false; 
    $config['width']  = 80; 
    $config['height'] = 80; 
    $config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'/images/uploads/thumbs/'.$imageName; 
    $this->load->library('image_lib', $config); 
    if (! $this->image_lib->resize()){echo $this->image_lib->display_errors();} 

    $this->image_lib->clear(); 

    // unable to create this this thumbnail 
    $config['image_library'] = 'gd2'; 
    $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/'.$imageName; 
    $config['create_thumb'] = FALSE; 
    $config['maintain_ratio'] = false; 
    $config['width']  = 696; 
    $config['height'] = 241; 
    $config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'/images/uploads/'.$imageName; 
    //$this->load->library('image_lib', $config); // this line cause problem 
    $this->image_lib->initialize($config); // with this problem resolved 
    if (! $this->image_lib->resize()){echo $this->image_lib->display_errors();} 


    $this->image_lib->clear(); 






    $this->load->view('admin/upload_form',array('error' => ' ')); 





} 

}

2

試着在你函數的頂部清除你的配置使用:

$this->image_lib->clear(); 

此外,您還可以查看使用任何錯誤:

if (! $this->image_lib->resize()) { 
    echo $this->image_lib->display_errors(); 
    } 

希望這有助於。

+0

尊敬的先生;發生以下錯誤: - 您的服務器不支持處理此類圖像所需的GD功能。 –

+0

你有寫權限的目錄嗎?你也可以嘗試改變'$ config ['image_library'] ='gd2'; 'to'$ config ['image_library'] ='GD'; ' – smilledge

+0

另外,請確保「source_image」和「new_image」的位置正確...並且您想要上傳什麼樣的格式圖像?它與JPEG圖像一起工作嗎? – smilledge

0

嘿感謝您的回答

是的,這適用於結束 $ this-> image_lib-> clear();

and for the 2nd thambnail // $ this-> load-> library('image_lib',$ config);

 $this->image_lib->initialize($config); 

註釋掉第1行並替換第2行。 您的查詢將被解決。

+0

如果你不添加新的東西,請不要復活舊的帖子。 – fancyPants