1

画像のサイズを変更しようとしましたが、うまくいきません! アップロード機能内でサイズ変更を行っていますが、何が間違っているのかわかりません! 以下のコードは、私のアップロード機能を示しています (そしてその中でサイズを変更します):

function do_upload()
    {
        $config['upload_path']   = './uploads/';        
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '2048';




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


        $this->load->library('upload', $config);
        $fullImagePath = '';
              if (! $this->upload->do_upload())
              {
                            $this->upload->display_errors('<p style="color: maroon; font-size:large;">', '</p>');

                   $error = array('file_error' => $this->upload->display_errors());
                  // print_r($error);

                   $this->load->view('layout/header');
                   $this->load->view('add_gallery', $error);
                   $this->load->view('layout/footer');

              }else{
                    //echo "UPLOAD SUCCESS";
                  // set a $_POST value for 'image' that we can use later
                        /*Image Manipulation Class*/

                    $config['image_library'] = 'gd2';
                    $config['source_image'] = $fullImagePath;
                    echo $fullImagePath;
                    $config['create_thumb'] = FALSE;
                    $config['maintain_ratio'] = TRUE;
                    $config['max_width'] = '480';
                    $config['max_height'] = '640';
                    $this->load->library('image_lib', $config); 
                    $this->image_lib->resize();


                  $upload_data    = $this->upload->data();
                  $fullImagePath = '/uploads/' . $upload_data['file_name']; 
              }



        return $fullImagePath;
    }

アップロードは正常に機能し、データベースに保存する fullImagePath (リンク) を取得します。とにかく、サイズ変更の処理方法がわかりません。

4

1 に答える 1

0

このresize()関数をsource_image呼び出す前に、最初に を構成しておく必要があります。これにより、指定された画像にサイズ変更を適用できます。

空のパスを送信しています。ここでパスを空の文字列として宣言し、ここで$fullImagePath = '';それを構成オプションに定義し、$config['source_image'] = $fullImagePath;その後 を呼び出す$this->image_lib->resize();ため、空の画像にサイズ変更することはできません。

また、必要のないものを 2 回ロードしてimage_libいます。

私はあなたのコードを修正しました。テストして、機能するかどうかを確認します

function do_upload()
{
    $config['upload_path']   = './uploads/';        
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = '2048';
    $this->load->library('upload', $config);
    $fullImagePath = '';
  if (! $this->upload->do_upload()){
        $this->upload->display_errors('<p style="color: maroon; font-size:large;">', '</p>');
        $error = array('file_error' => $this->upload->display_errors());
        $this->load->view('layout/header');
        $this->load->view('add_gallery', $error);
        $this->load->view('layout/footer');
  }else{
        $upload_data = $this->upload->data();
        $fullImagePath = '/uploads/' . $upload_data['file_name']; 
        $config['image_library'] = 'gd2';
        $config['source_image'] = $fullImagePath;
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['max_width'] = '480';
        $config['max_height'] = '640';
        $config['width'] = 75;
        $config['height'] = 50;
        $this->load->library('image_lib', $config); 
        $this->image_lib->resize();
  }

return $fullImagePath;
}
于 2013-05-04T17:26:01.507 に答える