我需要一種爲圖像管理腳本生成縮略圖(使用PHP5)的方法,並且在我的主機安裝了多個版本的PHP(4和5)時出現問題, PHP4設置爲默認值。這意味着從CLI調用任何PHP都會運行PHP4。我想出了以下內容,希望成爲一個跨平臺解決方案。我在這裏發佈主要是因爲我在使用Google時遇到了很多麻煩,所以這可能會在未來幫助某人,我也有以下問題。安裝了多個PHP版本的共享主機上的後臺腳本
- 你看到有什麼明顯的錯嗎?
- 是否有任何其他路徑到你知道或知道有一個更好的訂單來優化數組的php5二進制文件?
- 如果主機禁用了exec或shell_exec,那麼EGalleryProcessQueue.php腳本能夠作爲獨立的cron作業運行嗎?我沒有權限訪問cron來測試它。我並不太擔心這個問題,因爲我最終會試着去測試它。
- 有沒有人有任何建議,以便我可以得到一些反饋意見,通過圖像處理是多遠?查看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;
}
?>
如果我沒有對代碼進行任何重大更改,我會將其轉變爲社區wiki,因爲我發佈該主題的主要原因是爲了幫助未來的人們處理運行後臺腳本與多個PHP版本的主機,其中默認是不需要的版本。 – 2009-12-31 13:17:42