2012-09-25 16 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