2012-07-16 32 views
2

喜而有一些建議,我們可以從圖像笨根據您可以調整與image_lib圖像CI文件調整圖像和創建縮略圖

create_thumb FALSE TRUE/FALSE (boolean) Tells the image processing function to create a thumb. R 
thumb_marker _thumb None Specifies the thumbnail indicator. It will be inserted just before the file extension, so mypic.jpg would become mypic_thumb.jpg R 

所以在這裏創造更多的縮略圖選項是我的代碼

 $config_manip = array(
      'image_library' => 'gd2', 
      'source_image' => "./uploads/avatar/tmp/{$this->input->post('new_val')}", 
      'new_image'  => "./uploads/avatar/{$this->input->post('new_val')}", 
      'maintain_ratio'=> TRUE , 
      'create_thumb' => TRUE , 
      'thumb_marker' => '_thumb' , 
      'width'   => 150, 
      'height'  => 150 
     ); 

     $this->load->library('image_lib', $config_manip); 
     $this->image_lib->resize(); 

我會承擔這個代碼重新調整我的形象,也創建縮略圖,但我只能獲得指定的尺寸和_tump後綴一個圖像

我阿爾斯Ø嘗試添加該代碼手動創建第二圖像,但仍然不工作,我只能得到一個圖像

  $this->image_lib->clear(); 

$config_manip['new_image'] = 
"./uploads/avatar/thumbnail_{$this->input->post('new_val')}"; 

      $config_manip['width']  = 30 ; 
      $config_manip['height'] = 30 ; 
      $this->load->library('image_lib', $config_manip); 
      $this->image_lib->resize(); 
+0

你想要什麼結果?你想修改原始尺寸,然後創建一個30x30的拇指圖像? – 2012-07-16 12:31:11

+0

實際上我的原始圖像是在temp文件夾中出於安全原因,我想在我的頭像文件夾中創建一個已調整大小的縮略圖和縮略圖 – max 2012-07-16 12:34:27

+0

如果您刪除'create_thumb',那麼這兩部分代碼看起來會訣竅。我不知道你可以在'「{}」'裏面運行函數嗎? – 2012-07-16 12:36:45

回答

17

看來路徑是在你的代碼的問題。我修改並測試了它自己的工作原理。

public function do_resize() 
{ 
    $filename = $this->input->post('new_val'); 
    $source_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/tmp/' . $filename; 
    $target_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/'; 
    $config_manip = array(
     'image_library' => 'gd2', 
     'source_image' => $source_path, 
     'new_image' => $target_path, 
     'maintain_ratio' => TRUE, 
     'create_thumb' => TRUE, 
     'thumb_marker' => '_thumb', 
     'width' => 150, 
     'height' => 150 
    ); 
    $this->load->library('image_lib', $config_manip); 
    if (!$this->image_lib->resize()) { 
     echo $this->image_lib->display_errors(); 
    } 
    // clear // 
    $this->image_lib->clear(); 
} 

希望這可以幫助你。謝謝!!

+0

thanx,但我不能使用$ _SERVER ['DOCUMENT_ROOT'],即時在本地運行應用程序,這是我得到的地址:C:/wamp/www/uploads/avatar/tmp/500419bfe5d98.jpg這是錯誤的,它應該是C:/ wamp/www/mywebsitefolder/uploads /。 .. – max 2012-07-16 13:42:52

+0

@max,它也可以在localhost中工作,如果你的安裝虛擬主機,並使網站文件夾作爲根目錄。它可以在沒有任何設置的情況下在Web服務器中100%運行目前www是你的根目錄,所以項目文件夾是隱藏的。手動添加項目文件夾名稱或設置爲您的解決方案的虛擬主機。謝謝!! – 2012-07-16 19:02:02

+0

我的縮略圖服從了寬度和高度,但是我的source_image沒有調整大小......我期待着兩件事情會發生:1)源圖像將被調整大小,2)將創建一個帶有_thumb標記的縮略圖。 .. ...但是這將需要2套寬度/高度值... – dsdsdsdsd 2016-03-26 04:10:57

2

創建縮略圖的簡單方法。

function _create_thumbnail($fileName, $width, $height) 
{ 
    $this->load->library('image_lib'); 
    $config['image_library'] = 'gd2'; 
    $config['source_image'] = $_SERVER['DOCUMENT_ROOT']. $fileName;  
    $config['create_thumb'] = TRUE; 
    $config['maintain_ratio'] = TRUE; 
    $config['width']   = $width; 
    $config['height']   = $height; 
    $config['new_image']  = $_SERVER['DOCUMENT_ROOT']. $fileName;    
    $this->image_lib->initialize($config); 
    if (! $this->image_lib->resize()) { 
     echo $this->image_lib->display_errors(); 
    }   
} 
+0

謝謝,這個ans對我來說很完美.. :) – 2017-09-28 04:46:40

2

你的代碼是好的,但你需要做一個小的變化。

$this->load->library('image_lib'); 
$this->image_lib->initialize($config_manip);