2012-09-25 20 views
0

大家好:)预先感谢您对此问题的任何帮助。PHP的ImageMagick图像的多个尺寸 - 无法让它工作

我正在使用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!"; 
       } 

我总是最后的图像适当数量的正确命名 - 缩略图只是不重新大小 - 没有报告的错误。它总是说成功了。

如果我先将缩略图重新调整大小代码 - 缩略图重新调整大小 - 但中等大小的图像不会。

我意识到还有其他方法来加载库 - 但我不明白为什么第一次重新大小的作品,但第二次没有。

任何想法?

谢谢。 问候, 肯

+0

放在这行代码创建缩略图 之前'$这个 - > image_lib->明确的()' – chhameed

+0

喜哈米德 - 尝试,而我得到的错误“失败图像处理请确认您的服务器支持所选的协议,并且图像库的路径是正确的。“ 奇怪的是,第一个是完全一样的,没有错误 - 但第二个调用了“路径”或“协议”错误。为什么会通过而不是另一个? – KDawg

回答

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']不必清楚,但我没有测试过!

作为一个方面说明,您并不需要首先将图像复制到正确的目录中;该笨图像库可以为您创建一个新的,如果你设置你的配置是这样的:

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

这将保存图片到新的路径,而不必所有的地方创建副本。

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

+0

是的 - 非常感谢你froddd。 – KDawg

+0

非常感谢你。 一次加载库并使用任何配置或想要使用的更改配置值初始化库都是很有意义的。 再次感谢您的帮助 - 我很感激。 我有些如何与链接到的页面上的例子混淆 - 因为我用这些来进行第一次尝试 - 不知何故,我错过了“初始化”。但仍然困惑如何第一个复制,而不使用“初始化”。 无论如何 - 再次感谢! :) – KDawg

+0

此外 - 它好像“$ this-> image_lib-> clear();”不需要。我试过,没有“$ this-> image_lib-> clear();”并得到了相同的结果。 但是,我期待那个“$ this-> image_lib-> clear();”会清除配置值? 所以很清楚究竟是什么“$ this-> image_lib-> clear();”呢? 而你也是对的,我只需要改变宽度高度和'new_image'路径。 – KDawg