2010-12-01 12 views
3

私はドキュメントの例に基づいてこの作業をしようとしましたが、何か不足しているか、まあまあです!CodeIgniterアップロードとサイズ変更の問題

私は非常に固定レイアウトで表示するためにユーザーが画像をアップロードするCMSアプリケーションを持っています。アップロードした画像のファイルサイズを制限するのではなく、画像が到着した後で「処理」することになります。

画像の幅は615pxである必要がありますが、デジタルカメラから直接アップロードされる画像の一部は2500X2000以上であるため、これは危険です。

マニュアルのコードをまとめて、CMSアプリ内のフォルダに画像が正常にアップロードされています。ただし、画像のサイズが変更されていません。

私がこれまでにサイズを変更したのであれば、jCropを使用してクロップするためのイメージをユーザーに提示する予定です(最終イメージは615X275になるはずで、サイズ変更後に高さを切り取る必要があります)オリジナルの名前を使用して、画像をサイトの施設のフォルダにFTPするにはcodeigniterを使用します。

この件についてお手伝いをいたします。ここで

は私のコードです:

 
function do_feature_upload() { 
     $imageName = $this->uri->segment(3); 
     //echo $imageName; 

     // Where the file is going to be placed 
     $config['upload_path'] = "./uploads/".$_SESSION['dbPropNumber']; 
     $config['allowed_types'] = 'jpg|jpeg'; 
     $config['max_size'] = '0'; 
     $config['file_name'] = $imageName.'.jpg'; 
     $config['overwrite'] = 'TRUE'; 

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

     if (! $this->upload->do_upload()) { 
      $error = array('error' => $this->upload->display_errors()); 

      $error['propertyDropdown'] = $_SESSION['propertyDropdown']; 
      $error['username'] = $_SESSION['username']; 
      $error['dbPropNumber'] = $_SESSION['dbPropNumber']; 
      $error['propertyName'] = $this->content->getPropertyName($_SESSION['dbPropNumber']); 

      $this->load->view('upload_AmenityImage', $error); 
     } else { 
      $image_data = $this->upload->data(); 

      $origWidth = $image_data['image_width']; 
      $origHeight = $image_data['image_height']; 
      $newWidth = 615; 
      $newHeight = $newWidth*$origHeight/$origWidth; 

      $resize = array(
       'image_library'=>'gd2', 
       'source_image'=>base_url().'uploads/'.$_SESSION['dbPropNumber'].'/'.$imageName.'.jpg', 
       'new_image'=>base_url().'uploads/'.$_SESSION['dbPropNumber'].'/'.$imageName.'1.jpg', 
       'create_thumb' => FALSE, 
       'maintain_ratio'=>FALSE, 
       'width'=>$newWidth, 
       'height'=>$newHeight 
      ); 

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

      $data = array('upload_data' => $this->upload->data()); 
      $data['propertyDropdown'] = $_SESSION['propertyDropdown']; 
      $data['username'] = $_SESSION['username']; 
      $data['dbPropNumber'] = $_SESSION['dbPropNumber']; 
      $data['propertyName'] = $this->content->getPropertyName($_SESSION['dbPropNumber']); 

      //Present jCrop option after image is resized 

      // FTP to final destination 

      $this->load->view('upload_success', $data); 
     } // end if 
    } // end function 
+0

リサイズ機能でbase_url()を削除しようとしていますが、正しいパス(source_imageとnew_image)が相対的ではない絶対値である必要があります。 – trix

+0

チャームのように働いています! !!!!ありがとう!!! – jgravois

答えて

6

私はあなたのコードが間違って何が起こっているのか全くわからないんだけど、ここで私は、正確なターゲットの高さターゲット幅に合わせて画像のサイズを変更するために書いたモデル関数です。それを読んで、解決策を見つけられないかどうかを確認してください。

$this->prefixは私のクラスのプロパティで、ファイルのディレクトリを書き留めておく必要はありません。それは次のようになります。

$this->prefix = FCPATH.'uploads'.DIRECTORY_SEPARATOR;

画像のサイズ変更

/** 
* Resizes an image to fit exact dimensions 
* 
* @param string filename 
* @param int  target_width 
* @param int  target_height 
* 
* @return array('success' ? null : 'error') 
*/ 
function resizeImageToDimensions($filename, $target_width=700, $target_height=399) 
{ 
    $file_type = $this->getFileType($this->prefix.$filename); 

    if (!$file_type || $file_type != 'image') 
     return array('success'=>false, 'error'=>"This file doesn't exist or isn't an image"); 

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

    list($width, $height) = getimagesize($this->prefix.$filename); 
    $current_ratio = $width/$height; 
    $target_ratio = $target_width/$target_height; 
    $config['source_image'] = $this->prefix.$filename; 

    if ($current_ratio > $target_ratio) 
    { 
     //resize first to height, maintain ratio 
     $config['height'] = $target_height; 
     $config['width'] = $target_height * $current_ratio; 
     $this->image_lib->initialize($config); 

     if (!$this->image_lib->resize()) 
      return array('success'=>false, 'error'=>"There was an error while resizing this image"); 

     //then crop off width 
     $config['width'] = $target_width; 
     $config['maintain_ratio'] = false; 
     $this->image_lib->initialize($config); 

     if ($this->image_lib->crop()) 
      return array('success'=>true); 
     else 
      return array('success'=>false, 'error'=>"There was an error while cropping this image"); 
    } 
    else if ($current_ratio < $target_ratio) 
    { 
     //resize first to width, maintain ratio 
     $config['width'] = $target_width; 
     $config['height'] = $target_width/$current_ratio; 
     $this->image_lib->initialize($config); 

     if (!$this->image_lib->resize()) 
      return array('success'=>false, 'error'=>"There was an error while resizing this image"); 

     //then crop off height 
     $config['height'] = $target_height; 
     $config['maintain_ratio'] = false; 
     $this->image_lib->initialize($config); 

     if ($this->image_lib->crop()) 
      return array('success'=>true); 
     else 
      return array('success'=>false, 'error'=>"There was an error while cropping this image"); 
    } 
    else { 
     $config['width'] = $target_width; 
     $config['height'] = $target_height; 
     $this->image_lib->initialize($config); 

     if ($this->image_lib->resize()) 
      return array('success'=>true); 
     else 
      return array('success'=>false, 'error'=>"There was an error while resizing this image"); 
    } 
} 
+0

私はこれをモデルに入れ、次のように呼ぶべきです:$ this-> content-> resizeImageToDimensions($ image_data ['full_path'] ']、$ newWidth、$ newHeight); – jgravois

+0

ようなものただし、画像のパスが正しい場所を指していることを確認してください(正しく動作させるにはちょっとした工夫が必要です)。これをモデル自体で保つことは本当に必要ではありませんが、私は物事を整理するのが好きです(私は通常、MVCに全体的なアプローチをとっています...データ/ファイルの格納/操作を扱うものはすべてモデルに入ります)。これがなぜ自分自身のライブラリでも、image_libの拡張でもない本当の理由はありません。どんなことであれ、あなたのために最も効果的です。しかし、すべての場合に薄いコントローラーが必要であるため、これをコントローラーから外すことをお勧めします。 – treeface

1

私は数ヶ月前に同じ問題を抱えていたし、悲しいことに、私はそれを把握することができませんでした。私は同じ質問hereCodeIgniter's forumsに投稿しましたが、誰も私を助けないでしょう。

私はそうtimthumb素晴らしいですが、スクリプト、しかし、どこにも近い理想的な:(

を使用して終了あなたが急いでいる場合は、私は強くtimthumbを使用してお勧めします。あなたが投資するいくつかの時間を持っている場合

関連する問題