我有一台相机连接到笔记本电脑.使用遥控拍摄,当摄影师拍照时,它会被保存到笔记本电脑硬盘上的文件夹中.在文件夹上设置了Automator(Mac OS X)操作,无论何时出现新文件,它都会调整大小并使用Transmit将其推送到FTP.
这是代码的来源.
我有一个显示最近拍摄照片的网页.它使用ajax重复检查是否已上传新文件,如果有,则加载新照片并将其与旧照片交叉淡入淡出.这是页面上运行的Javascript.
(function() { var delay,refreshLoop; // Alias to setTimeout that reverses the parameters,making it much cleaner in code (for CoffeeScript) delay = function(time,callback) { return setTimeout(callback,time); }; // Function that drives the loop of refreshing photos refreshLoop = function(currentFolderState,refreshRate) { // Get the new folder state $.get("ajax/getFolderState.PHP",function(newFolderState) { // If the new folder state is different if (newFolderState !== currentFolderState) { // Get the newest photo $.get("ajax/getNewestPhoto.PHP",function(photoFilename) { var img; // Create the new image element img = $('<img class="new-photo"/>') // Append the src attribute to it after so it can BG load .attr('src',"/events/mindsmack/booth/cinco-de-mindsmack-2012/" + photoFilename) // When the image is loaded .load(function() { // Append the image to the photos container $('#photos').append(img); // Crossfade it with the old photo $('#photos .current-photo').fadeOut(); $('#photos .new-photo').fadeIn().removeClass("new-photo").addClass("current-photo"); }); }); } // Wait for the refresh rate and then repeat delay(refreshRate,function() { refreshLoop(newFolderState,refreshRate); }); }); }; // Document Ready $(function() { var refreshRate; // Load the first photo $.get("ajax/getNewestPhoto.PHP",function(photoFilename) { $('#photos').append("<img src='/events/mindsmack/booth/cinco-de-mindsmack-2012/" + photoFilename + "' class='current-photo' />"); }); refreshRate = 2000; // After the timeout delay(refreshRate,function() { // Get the initial folder state and kick off the loop $.get("ajax/getFolderState.PHP",function(initialFolderState) { refreshLoop(initialFolderState,refreshRate); }); }); }); }).call(this);
getFolderState.PHP
<?PHP $path = $_SERVER['DOCUMENT_ROOT'] . "/events/mindsmack/booth/cinco-de-mindsmack-2012/"; // Get a directory listing of the path where the photos are stored $dirListing = scandir( $path ); // Echo an md5 hash of the directory listing echo md5(serialize($dirListing));
getNewestPhoto.PHP
<?PHP $path = $_SERVER['DOCUMENT_ROOT'] . "/events/mindsmack/booth/cinco-de-mindsmack-2012/"; // Get a directory listing of the path where the photos are stored $listing = scandir($path); $modTime = 0; $mostRecent = ""; // Find the most recent file foreach ( $listing as $file ) { if ( is_file($path.$file) && $file !== ".DS_Store" && filectime($path.$file) > $modTime) { $modTime = filectime($path.$file); $mostRecent = $file; } } // Echo the most recent filename echo $mostRecent;
所有这些工作大多完美无瑕.我认为,问题是当文件处于上传过程中时触发循环.有时会拍摄一张照片,它只会在页面上显示.根本没有抛出错误,脚本继续运行得很好,图像文件实际上是在该状态下缓存的,这使我相信我的代码正在捕获正在进行的文件上传并且只显示文件的一部分已经上传的那一刻.
我不介意改变我的解决方案,如果我需要为了克服这个问题,我只是不确定该做什么.
编辑
根据下面的一条建议,我在我的getNewestPhoto.PHP中添加了代码,用于检查照片的文件大小,等待一下,然后再次检查.如果它们不同,它会返回并再次检查,直到文件大小相同.我希望这会抓住中上传的文件,因为文件大小会在循环之间发生变化,但即使照片出现部分呈现,文件大小的检查也没有抓住它.
$currFilesize = filesize($path . $mostRecent); $newFilesize; while ( true ) { sleep(.5); $newFilesize = filesize($path . $mostRecent); if ( $newFilesize == $currFilesize) { break; } else { $currFilesize = $newFilesize; } }
我正在考虑(通过另一个建议)我需要在上传时添加某种锁定文件,以阻止代码刷新照片,并在上传完成后删除,但看到我没有运行任何类型的网页计算机上的服务器拴在相机上,我不知道如何实现这一目标.我会喜欢建议.
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。