PHP ImageMagick图像有多种尺寸-无法正常工作

PHP ImageMagick图像有多种尺寸-无法正常工作,php,codeigniter,imagemagick,image-resizing,Php,Codeigniter,Imagemagick,Image Resizing,大家好:)提前感谢您对这个问题的帮助 我正在使用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. $Cop

大家好:)提前感谢您对这个问题的帮助

我正在使用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!";
                }
我总是以正确命名的正确数量的图像结束-缩略图图像只是不重新大小-没有错误报告。它总是说成功了

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

我知道还有其他加载库的方法,但我不明白为什么第一种方法可以重新调整库的大小,而第二种方法不行

有什么想法吗

谢谢。 当做
Ken

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

// 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['new_image'] = '/path/to/new_image.jpg';
我怀疑你甚至可以在不清除的情况下更新
$config['width']
$config['height']
,但我还没有测试过

作为补充说明,您实际上不必首先将图像复制到正确的目录中;如果您这样设置配置,CodeIgniter映像库可以为您创建一个新的映像库:

// 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['new_image'] = '/path/to/new_image.jpg';
这将把图像保存到新路径中,而不必到处创建副本


参考:

在创建缩略图之前放置了这行代码
$this->image_lib->clear()
Hi-Hameed-尝试了一下,我得到了错误“图像处理失败。请验证您的服务器是否支持所选协议以及到图像库的路径是否正确。”奇怪的是,第一个是完全相同的,没有错误-但第二个调用“路径”或“协议”错误。为什么一个通过而另一个不通过?是的,非常感谢你,弗罗迪。非常感谢你,弗罗迪。加载一次库并使用您想要使用的任何配置或更改的配置值初始化库都是很有意义的。再次感谢你的帮助,我很感激。我对您链接到的页面上的示例感到困惑,因为我在第一次尝试时使用了这些示例,不知怎的,我错过了“初始化”。但仍然不清楚第一个文件是如何在不使用“初始化”的情况下复制的。不管怎样-再次感谢!:)另外-似乎不需要“$this->image_lib->clear();”。我尝试了使用“$this->image_lib->clear();”和不使用“$this->image_lib->clear()”,得到了相同的结果。但是,我原以为“$this->image_lib->clear();”会清除配置值?那么,“$this->image_lib->clear();”到底是做什么的呢?你也对了,我只需要改变宽度高度和“新图像”路径;我不介意再问你一个问题Frodd——因为你似乎对此有很好的经验——有没有一种相对简单的方法来显示使用codeigniter和imagemagick上传图像的进度?感谢
clear()
方法可能只清除配置值,因此,如果覆盖所有配置值,或者重复使用类似的配置值,那么实际上不需要它。关于文件上传进度:这完全是另一个问题!最好的选择是使用jQuery插件,比如