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!";
                }

我总是以正确命名的正确数量的图像结束 - 缩略图图像不会重新调整大小 - 没有报告错误。它总是说成功了。

如果我首先放置缩略图重新调整大小的代码 - 缩略图重新调整大小正确 - 但中等大小的图像没有。

我意识到还有其他方法可以加载库 - 但我不明白为什么第一次调整大小有效但第二次没有。

有任何想法吗?

谢谢。问候,肯

4

1 回答 1

2

您只需要加载一次库,但每次都需要使用新配置对其进行初始化。因此,您的代码将如下所示:

// 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 回答