0

みなさん、こんにちは:)この質問についてご協力いただきありがとうございます。

私はCodeIgniterを使用しており、画像のアップロード、回転、サイズ変更に成功しています。画像から複数のサイズを作成しようとすると、少し迷ってしまいます。

これが私のコードです:

// First I make two copies of the origional image - there is no problem here - these always work. I always have the successfully copied images.

                $Copy_Mid_Size_File = "Some_Path_For_The_Newly_Copied_Mid_Size_Image"

                if(!copy($source_image, $Copy_Mid_Size_File)){
                    echo "failed to copy $Copy_Mid_Size_File - Please contact the system administrator. \n";
                    die();
                }

                $Copy_Thumbnail_Size_File = "Some_Path_For_The_Newly_Copied_Thumbnail_Size_Image"

                if(!copy($source_image, $Copy_Thumbnail_Size_File)){
                    echo "failed to copy $Copy_Thumbnail_Size_File - Please contact the system administrator. \n";
                    die();
                }

                // Now I resize for the mid size image
                $config['image_library'] = 'imagemagick';           
                $config['library_path'] = '/usr/bin';

                $config['source_image'] = $Copy_Mid_Size_File;
                $config['maintain_ratio'] = TRUE;
                $config['quality'] = '100%';
                $config['master_dim'] = 'auto';
                $config['width'] = 200;
                $config['height'] = 200;

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

                if(!$this->image_lib->resize()){
                    echo $this->image_lib->display_errors();// This has never reported an error yet
                    echo "Image Re-size for Mid Size Image FAILED!";// This has never reported an error yet
                    die();
                }else{
                    echo" Mid-Size Re-size Worked!";
                }


                // Now I try to resize for the Thumbnail
                $config['image_library'] = 'imagemagick';           
                $config['library_path'] = '/usr/bin';

                $config['source_image'] = $Copy_Thumbnail_Size_File;

                $config['maintain_ratio'] = TRUE;
                $config['quality'] = '100%';
                $config['master_dim'] = 'auto';
                $config['width'] = 50;
                $config['height'] = 50;

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

                if(!$this->image_lib->resize()){
                    echo $this->image_lib->display_errors();// This has never reported an error yet
                    echo "Image Re-size for Thumbnail Size Image FAILED!";// This has never reported an error yet
                    die();
                }else{
                    echo" Thumbnail Re-size Worked!";
                }

私はいつも正しい名前の画像の適切な数になってしまいます-サムネイル画像はサイズが変更されません-エラーは報告されません。それはいつも成功していると言っています。

サムネイルのサイズ変更コードを最初に配置すると(サムネイルは正しくサイズ変更されます)、中サイズの画像は正しくサイズ変更されません。

ライブラリをロードする方法は他にもあると思いますが、最初のサイズ変更が機能する理由はわかりませんが、2番目のサイズ変更は機能しません。

何か案は?

ありがとう。よろしく、ケン

4

1 に答える 1

2

ライブラリをロードする必要があるのは1回だけですが、そのたびに新しい構成でライブラリを初期化する必要があります。したがって、コードは次のようになります。

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

// first image:
// set some config
$config['image_library'] = 'imagemagick';
...

// initialize the library with this config
$this->image_lib->initialize($config);
// Do the resize:
$this->image_lib->resize();

// clear the config:
$this->image_lib->clear();

// second image:
// set some config
$config['image_library'] = 'imagemagick';
...

// re-initialize the library with the new config
$this->image_lib->initialize($config);
// Do the resize:
$this->image_lib->resize();

$config['width']クリアし$config['height']なくても更新できると思いますが、テストはしていません。

ちなみに、最初に画像を正しいディレクトリにコピーする必要はありません。次のように構成を設定すると、CodeIgniterイメージライブラリで新しいイメージライブラリを作成できます。

$config['new_image'] = '/path/to/new_image.jpg';

これにより、あちこちにコピーを作成しなくても、画像が新しいパスに保存されます。

参照:http ://codeigniter.com/user_guide/libraries/image_lib.html

于 2012-09-26T17:03:54.383 に答える