From 85eacaff6e60d19897bf52019d0fac61f7d6994d Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Fri, 26 Jun 2015 03:56:23 +0200 Subject: [PATCH] Initial commit --- code/scans.php | 56 +++++++++++++++++++++++++++++--------------------- code/utils.php | 25 ++++++++++++++++++++++ config.php | 5 +---- 3 files changed, 59 insertions(+), 27 deletions(-) diff --git a/code/scans.php b/code/scans.php index d6f8a02..da8058f 100644 --- a/code/scans.php +++ b/code/scans.php @@ -3,13 +3,35 @@ include_once "config.php"; include_once "utils.php"; +$Commands=array(); + +function ExecCommand($command){ + global $Commands; + $loggedCommand=array(); + $loggedCommand["Command"]=$command; + list($returnCode, $stdout, $stderr)=ExecFull($command); + $loggedCommand["Result"]=$stdout; + $loggedCommand["Error"]=$stderr; + $loggedCommand["ReturnCode"]=$returnCode; + $Commands[]=$loggedCommand; + return $stdout; +} + +function ShowCommandLog(){ + global $Commands; + foreach($Commands as $loggedCommand){ + DrawParagraph($loggedCommand["Command"],"font-weight: bold;"); + DrawParagraph($loggedCommand["Result"]); + DrawParagraph($loggedCommand["Error"],"color: red;"); + } +} + + function Scan($device,$resolution,$format,$destFileBase){ global $PreviewDir; global $ScanImage; global $PNMtoJPEG; global $PNMtoPNG; - global $PNMtoPS; - global $PStoPDF; $DestFile=$PreviewDir.$destFileBase; $Command=$ScanImage." -d ".$device. @@ -17,35 +39,22 @@ function Scan($device,$resolution,$format,$destFileBase){ if($format=="jpg"){ $DestFile.=".jpg"; $Command.=" | {$PNMtoJPEG} --quality=100 > ".$DestFile; - $Scan=shell_exec($Command); } if($format=="png"){ $DestFile.=".png"; $Command.=" | {$PNMtoPNG} > ".$DestFile; - $Scan=shell_exec($Command); - } - if($format=="pdf"){ - $DestFile2=$DestFile.".pnm"; - $Command.=" > {$DestFile2}"; - $Scan=shell_exec($Command); - - $DestFile.=".pdf"; - $Command="cat {$DestFile2} | {$PNMtoPS} | {$PStoPDF} - {$DestFile}"; - $Convert=shell_exec($Command); } + $Scan=ExecCommand($Command); return $DestFile; } function CleanUp(){ + global $Commands; global $PreviewDir; - $Command="rm -rf ".$PreviewDir."*.pnm"; - $Delete=shell_exec($Command); $Command="rm -rf ".$PreviewDir."*.png"; - $Delete=shell_exec($Command); + $Delete=ExecCommand($Command); $Command="rm -rf ".$PreviewDir."*.jpg"; - $Delete=shell_exec($Command); - $Command="rm -rf ".$PreviewDir."*.pdf"; - $Delete=shell_exec($Command); + $Delete=ExecCommand($Command); } function MoveToDest($origFile){ @@ -53,20 +62,20 @@ function MoveToDest($origFile){ $destFile=basename($origFile); $destFile=$FinalDestDir.$destFile; $Command="cp ".$origFile." ".$destFile; - $Copy=shell_exec($Command); + $Copy=ExecCommand($Command); } function CropImage($file){ global $ImageMagik; global $CropFuzz; $Command=$ImageMagik." ".$file.' -fuzz '.$CropFuzz.'% -trim '.$file."\n"; - $Cropping=shell_exec($Command); + $Cropping=ExecCommand($Command); } // Detect scanner $CMD=$ScanImage." --list-devices | grep device"; -$SaneScanner = `$CMD`; +$SaneScanner = ExecCommand($CMD); unset($cmd); $start=strpos($SaneScanner,"`")+1; $laenge=strpos($SaneScanner,"'")-$start; @@ -89,6 +98,7 @@ if(RequestParm("btnScan",false)){ $baseName="Scan-".date("Y-m-d_H_i_s"); $DestFile=Scan($Scanner,$Resolution,$Format,$baseName); CropImage($DestFile); + CropImage($DestFile); }else{ $baseName="Scan-".date("Y-m-d_H_i_s"); $DestFile=Scan($Scanner,$Resolution,$Format,$baseName); @@ -114,5 +124,5 @@ if($DestFile!=null){ } echo "\n"; - +ShowCommandLog(); ?> \ No newline at end of file diff --git a/code/utils.php b/code/utils.php index 9793f3a..a1723cc 100644 --- a/code/utils.php +++ b/code/utils.php @@ -90,6 +90,14 @@ function DrawButton($text,$id){ 'id="'.$idFixed.'" name="'.$idFixed.'" class="button" />'; } +function DrawParagraph($text,$style=null){ + $textFixed=htmlentities($text,ENT_HTML5, "UTF-8"); + if($style==null){ + echo "

".$textFixed."

\n"; + }else{ + echo "

".$textFixed."

\n"; + } +} function RequestParm($name,$defaultValue){ if(isset($_GET[$name])){ @@ -101,5 +109,22 @@ function RequestParm($name,$defaultValue){ return $defaultValue; } +function ExecFull($cmd, $input='') { + $proc = proc_open($cmd, array(array('pipe', 'r'), + array('pipe', 'w'), + array('pipe', 'w')), $pipes); + fwrite($pipes[0], $input); + fclose($pipes[0]); + + $stdout = stream_get_contents($pipes[1]); + fclose($pipes[1]); + + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[2]); + + $return_code = (int)proc_close($proc); + + return array($return_code, $stdout, $stderr); +} ?> \ No newline at end of file diff --git a/config.php b/config.php index 3905529..ee4c77e 100644 --- a/config.php +++ b/config.php @@ -7,8 +7,6 @@ $ScanImage = "/usr/bin/scanimage"; $PNMtoJPEG = "/usr/bin/pnmtojpeg"; $PNMtoPNG = "/usr/bin/pnmtopng"; -$PNMtoPS = "/usr/bin/pnmtops"; -$PStoPDF = "/usr/bin/ps2pdf14"; $ImageMagik = "/usr/bin/convert"; // Destination dirs @@ -31,8 +29,7 @@ $Resolution=200; // Formats $Formats=array( "PNG"=>"png", - "JPEG/JPG"=>"jpg", - "PDF"=>"pdf" + "JPEG/JPG"=>"jpg" ); $Format="png";