2014-03-25 229 views
0

我需要根據寬度只有,高度不:( 來調整我的圖片,因爲我使用** ** $config['maintain_ratio'] = TRUE; ,笨自動解決它(那問題)有時我得到一個黑色固體作爲一個額外的寬度或高度,調整圖像大小,笨

我已經看過了所有與codegniter的形象 庫的問題,我找不到與我的問題:(

什麼

這裏是最後的結果說明... I need to resize according to width only , not height :(

上傳完成後我有這個...

$this->load->library('md_image'); 
     $source='assets/img/hotellist/'.$data1['hotel_pictures']; 
     $width=746; 
     $height=400; 
     $dest = FALSE; 
     //$this->md_image->resize_image($source, $width, $height, $source); 
      $config['image_library'] = 'gd2';//imagemagik 
      $config['source_image'] = 'assets/img/hotellist/'.$data1['hotel_pictures']; 
      //$config['image_library'] = 'imagemagick'; 
      //$config['library_path'] = '/usr/X11R6/bin/'; 
      $config['create_thumb'] = FALSE; 
      $config['maintain_ratio'] = TRUE; 
      $config['width']  = 746; 
      $config['height'] = 400; 
      $config['quality'] = 75; 
      $config['encrypt_name'] = TRUE; 
      $config['remove_spaces'] = TRUE; 
      //$config['x_axis'] = 100; 
      //$config['y_axis'] = 300; 
      $img =$config['source_image']; 
      //var_dump('check problem',$config['source_image']); 

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

      $this->image_lib->resize(); 

那麼我走裁剪部分

$new_img =$this->md_image->crop_to_ratio($source, $width, $height, $x = 17, $y = 9, $dest = FALSE); 

的關懷總結一下,有人上傳一張圖片,它根據寬度調整大小隻有 Step 1 然後它就會冒出Step 2

PS:櫃面有人需要它md_image

+1

問題出在哪裏發生,在調整或作物? – jcorry

+0

對不起,延遲迴復, 有時,當圖像的高度很大,即+ 700px的作物沒有調整大小,如果高度稍大一點,即+ 100px,它會在調整大小後留下黑色的實體...希望此信息有助於@jcorry,謝謝。 – Fahad

+0

如果我得到image_magick只根據高度調整大小,它對我來說是一個很大的成就,裁剪不是問題...如果我給它正確的尺寸width-> 700px高度 - >(*大於> 400px *)它莊稼棒! – Fahad

回答

2
$this->load->library('md_image'); 
     $source='assets/img/hotellist/'.$data1['hotel_pictures']; 
     $width=746; 
     $height=400; 
     $size = getimagesize($source); 
     $resize_height=($size[1]*746)/$size[0]; 
     $dest = FALSE; 
     //$this->md_image->resize_image($source, $width, $height, $source); 
      $config['image_library'] = 'gd2';//imagemagik 
      $config['source_image'] = 'assets/img/hotellist/'.$data1['hotel_pictures']; 
      //$config['image_library'] = 'imagemagick'; 
      //$config['library_path'] = '/usr/X11R6/bin/'; 
      $config['create_thumb'] = FALSE; 
      $config['maintain_ratio'] = TRUE; 
      $config['width']  = 746; 
      $config['height'] = $resize_height; 
      $config['quality'] = 75; 
      $config['encrypt_name'] = TRUE; 
      $config['remove_spaces'] = TRUE; 
      //$config['x_axis'] = 100; 
      //$config['y_axis'] = 300; 
      $img =$config['source_image']; 
      //var_dump('check problem',$config['source_image']); 

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

      $this->image_lib->resize(); 
+2

請考慮使用代碼註釋來詳細說明此解決方案,或者解釋您更改的內容以及它如何/爲什麼會起作用。簡單地發佈代碼將無助於原始海報或任何其他人學到很多東西。我建議閱讀[如何寫出一個好答案](http://stackoverflow.com/help/how-to-answer)。 – Daniel

+0

謝謝你回覆user3641704,我不知道,但由於某種原因,我有我的**圖像扭曲**第一次在裁剪之前我失去了圖像分辨率,_可能是你應該考慮詳細說明你的答案,就像@丹尼爾說的:) – Fahad

+0

謝謝@ user3641704 #Cumali :) – Fahad

1
function resize_than_crop($path, $new_path, $width = 400, $height = 746) { 

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

    $this->image_lib->initialize(array(
     'image_library' => 'gd2', 
     'source_image' => $path, 
     'new_image' => $new_path, 
     'maintain_ratio' => true, 
     'master_dim' => 'width', 
     'width' => $width 
    )); 
    $this->image_lib->resize(); 

    $this->image_lib->clear(); 

    $this->image_lib->initialize(array(
     'image_library' => 'gd2', 
     'source_image' => $new_path, 
     'height' => $height 
    )); 
    $this->image_lib->crop(); 
} 
+0

感謝您花時間寫這篇文章,但@ user3641704已經回答了,我會考慮在未來的項目中測試您的解決方案 – Fahad