2

我需要一種爲圖像管理腳本生成縮略圖(使用PHP5)的方法,並且在我的主機安裝了多個版本的PHP(4和5)時出現問題, PHP4設置爲默認值。這意味着從CLI調用任何PHP都會運行PHP4。我想出了以下內容,希望成爲一個跨平臺解決方案。我在這裏發佈主要是因爲我在使用Google時遇到了很多麻煩,所以這可能會在未來幫助某人,我也有以下問題。安裝了多個PHP版本的共享主機上的後臺腳本

  1. 你看到有什麼明顯的錯嗎?
  2. 是否有任何其他路徑到你知道或知道有一個更好的訂單來優化數組的php5二進制文件?
  3. 如果主機禁用了exec或shell_exec,那麼EGalleryProcessQueue.php腳本能夠作爲獨立的cron作業運行嗎?我沒有權限訪問cron來測試它。我並不太擔心這個問題,因爲我最終會試着去測試它。
  4. 有沒有人有任何建議,以便我可以得到一些反饋意見,通過圖像處理是多遠?查看EGalleryProcessQueue.php的TODO部分我想在管理部分顯示進度條。

主要腳本

/** 
* Writes the array to a text file in /path/to/gallery/needsThumbs.txt for batch processing. 
* Runs the thumbnail generator script in the background. 
* 
* @param array $_needsThumbs the array of images needing thumbnails 
*/ 
private function generateThumbnails($_needsThumbs) 
{ 
    file_put_contents($this->_realpath.DIRECTORY_SEPARATOR.'needsThumbs.txt',serialize($_needsThumbs)); 

    $Command = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'EGalleryProcessQueue.php '.$this->_realpath.' '.$this->thumbnailWidth.' '.$this->thumbnailHeight; 

    if(PHP_SHLIB_SUFFIX == 'so')// *nix (aka NOT windows) 
    { 
     /* 
     * We need to make sure we are using the right PHP version 
     * (problems with shared hosts that have PHP4 and PHP5 installed, 
     * but PHP4 set as default). 
     */ 
     $phpPaths = array('php', '/usr/local/bin/php', '/usr/local/php5/bin/php', '/usr/bin/php', '/usr/bin/php5'); 
     foreach($phpPaths as $path) 
     { 
      exec("echo '<?php echo version_compare(PHP_VERSION, \"5.0.0\", \">=\"); ?>' | $path", $result); 
      if($result) 
      { 
       shell_exec("nohup $path $Command 2> /dev/null > /dev/null &"); 
       break; 
      } 
     } 
    } 
    else // Windows 
    { 
     $WshShell = new COM("WScript.Shell"); 
     $WshShell->Run("php.exe $Command", 0, false); 
    } 
} 

EGalleryProcessQueue.php

#!/usr/bin/php 
<?php 

if ($argc === 4 && strstr($argv[0], basename(__FILE__))) { 
    // File is being called by the CLI and has not been included by another script 

    if(!file_exists($argv[1].DIRECTORY_SEPARATOR.'needsThumbs.txt')) 
    { 
     // Either no thumbnails need to be created or a wrong directory has been supplied 
     exit; 
    } 

    include(realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'EGalleryThumbGenerator.php'); 

    $generator = new EGalleryThumbGenerator; 
    $generator->directory = $argv[1]; 
    $generator->thumbnailWidth = is_int($argv[2]) ? $argv[2] : 128; 
    $generator->thumbnailHeight = is_int($argv[3]) ? $argv[3] : 128; 

    // $generator->processImages() returns the number of images left to process (it does them in blocks of 10) 
    while (($i = $generator->processImages()) > 0) 
    { 
     /* 
     * TODO Can we get some sort of feedback to the user here? 
     * Possibly so that we can display a progress bar in the management section. 
     * Probably have to write $i to a file to be read by the main script. 
     */ 
    } 

    exit; 
} 
?> 
+0

如果我沒有對代碼進行任何重大更改,我會將其轉變爲社區wiki,因爲我發佈該主題的主要原因是爲了幫助未來的人們處理運行後臺腳本與多個PHP版本的主機,其中默認是不需要的版本。 – 2009-12-31 13:17:42

回答

1

你看到任何明顯的錯誤呢?

不,代碼看起來不錯。

是否有任何其他路徑到您知道或知道有更好的數組優化的php5二進制文件?

這是一個很難回答的問題,因爲PHP可以安裝在服務器上的任何地方。您對我的路徑似乎非常合乎邏輯,但可以安裝任意數量的其他位置。

與其提供一堆可能安裝PHP5的目錄,如果用戶不需要在其$ PATH中設置用於提供PHP5可執行文件路徑的參數,那麼該怎麼辦?

如果主機禁用了exec或shell_exec,EGalleryProcessQueue.php腳本是否可以通過cron作業運行?

我還沒有測試過,但我會認爲會阻止腳本運行。

有沒有人有任何建議,我可以得到一些反饋意見,通過圖像處理是多遠?查看EGalleryProcessQueue.php的TODO部分我想在管理部分顯示進度條。

存儲完成某處的文件(文件,數據庫,甚至可能是會話變量)的圖像數量,並且每秒鐘都會有一個AJAX調用觸發到一個函數,該函數提供了done和total。然後使用類似於http://docs.jquery.com/UI/Progressbar

+0

謝謝。我知道PHP可以安裝在任何地方,我試圖覆蓋最可能的位置。關於變量的好主意。 對不起,我的意思是能夠直接從cron調用EGalleryProcessQueue.php腳本(因爲該文件仍然應該寫入,exec/shell_exec調用將失敗,但這應該不重要)。從本質上講,EGalleryProcessQueue.php能夠作爲獨立的cron工作正常運行嗎? 是的,我曾計劃使用jQuery UI進度條。會話變量是一個好主意。 – 2009-12-31 22:48:43

+0

是的,你應該可以直接從cron作業運行EGalleryProcessQueue.php。無論如何,命令行中的PHP通常使用不同的php.ini,並且可以強制它使用帶有-c標誌的不同php.ini。從命令行鍵入php -h以查看不同的標誌。 – bradym 2010-01-01 20:42:02