2012-12-04 45 views
1

我在Codeigniter中使用圖像操作庫,並且需要調整圖像的寬度,使其最大值爲278px,同時保持比例。我還需要確保圖像不超過400像素。第一次調整大小時無法裁切圖像

我試圖通過使用$this->image_lib->resize()然後使用$this->image_lib->crop()再次運行它,但我遇到了調整大小干擾作物的問題。

這裏有兩種型號:

public function create_thumb($path) { 

    $data = $this->upload->data(); 

    if ($data['image_width'] >= 278): 

     $config['image_library'] = 'gd2'; 
     $config['source_image'] = $path; 
     $config['create_thumb'] = FALSE; 
     $config['maintain_ratio'] = TRUE; 
     $config['width'] = 278; 
     $config['height'] = 400; 
     $config['quality'] = '90%'; 
     $config['master_dim'] = 'width'; 

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

     if ($this->image_lib->resize()): 

      $this->image_lib->clear(); 

     endif; 

    endif; 

    $this->crop_image($path); 

    return false; 
} 

// Make max image size 278x400 
public function crop_image($path) { 

    list($width, $height) = getimagesize($path); 

    $config['image_library'] = 'gd2'; 
    $config['source_image'] = $path; 
    $config['x_axis'] = '0'; 
    $config['y_axis'] = '0'; 
    $config['maintain_ratio'] = FALSE; 
    $config['width'] = $width; 
    $config['height'] = 400; 
    $config['quality'] = '100%'; 

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

    if ($this->image_lib->crop()) 
    { 
     return true; 
    } 
    return false; 
} 

如果我叫crop_image()直接從控制器,它作物如預期。但是,從create_thumb()調用它時,出現錯誤Your server does not support the GD function required to process this type of image.由於我能夠先前裁剪圖像,並且根據phpinfo()安裝了GD,所以我很困惑我爲什麼會收到此錯誤。

我認爲問題與加載image_lib兩次有關,但我認爲$this->image_lib->clear();會解決這個問題?

我在做什麼錯?有沒有更好的方法讓我調整圖像的最大寬度爲278px,高度爲400px?

+0

你試過扭轉它嗎?裁剪然後調整大小,或單獨運行任一功能? – Jeemusu

+0

@Jeemusu雅,他們都分開工作。一旦我嘗試它們,它們就會失敗並導致我在問題中發佈的錯誤。 – Motive

回答

1

試試這個: -

  //Resize Image 
      $config = array(); 
      $config['image_library'] = 'gd2'; 
      $config['source_image'] = './assets/uploaded_files/gallery/original/'.$image_name; 
      $config['new_image'] = './assets/uploaded_files/gallery/banner/'.$image_name; 
      $config['create_thumb'] = FALSE; 
      $config['maintain_ratio'] = TRUE; 
      $config['master_dim']= 'width'; 
      $config['quality'] = '100'; 
      $config['width'] = 1260; 
      $config['height']= 645; 
      $this->image_lib->initialize($config); 
      $this->image_lib->resize(); 
      //Crop image 
      $config = array(); 
      $config['image_library'] = 'gd2'; 
      $config['source_image'] = './assets/uploaded_files/gallery/banner/'.$image_name; 
      $config['new_image'] = './assets/uploaded_files/gallery/banner/'.$image_name; 
      $config['create_thumb'] = FALSE; 
      $config['maintain_ratio'] = FALSE; 
      $config['quality'] = '100'; 
      $config['x_axis'] = 0; 
      $config['y_axis'] = 0; 
      $config['width'] = 1260; 
      $config['height']= 645; 
      $this->image_lib->initialize($config); 
      $this->image_lib->crop(); 

根據您的需要調整的寬度和高度。