我需要一种方法来为图像管理脚本生成缩略图(使用PHP5),并且我的主机安装了多个版本的PHP(4和5),并将PHP4设置为默认值。这意味着从CLI调用php将会运行PHP4。我想出了以下内容作为我希望成为跨平台解决方案的内容。我在这里发帖主要是因为我在使用谷歌找到任何帮助方面遇到了很多麻烦,所以这对未来的某些人有帮助,我也有以下问题。
主要脚本
/**
* 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 :(得分:1)
你看到它有什么明显的错误吗?
不,代码看起来不错。
你知道或知道更好的顺序是否有任何其他途径到php5二进制文件来进行优化?
这是一个难以回答的问题,因为PHP可以安装在服务器上的任何位置。您拥有的路径似乎对我来说非常符合逻辑,但可以安装任意数量的其他地方。
如果没有提供PHP5可能安装的一堆目录,那么如果不是在$ PATH中,那么用户必须设置一个参数来提供PHP5可执行文件的路径呢?
如果主机禁用了exec或shell_exec,那么EGalleryProcessQueue.php脚本是否能够通过cron作业运行?
我还没有测试过,但我认为这会阻止脚本运行。
有没有人有任何建议可以让我得到一些反馈,知道处理的图像有多远?请参阅EGalleryProcessQueue.php的TODO部分。我想在管理部分显示进度条。
存储在某处完成的图像数量(文件,数据库,甚至是会话变量),并且每隔一秒左右对一个提供完成与总数的函数进行AJAX调用。然后使用类似http://docs.jquery.com/UI/Progressbar
的内容