Skip to content

Commit

Permalink
Styling / installation script
Browse files Browse the repository at this point in the history
  • Loading branch information
sbs20 committed Mar 17, 2018
1 parent 1e67b84 commit ab9eed4
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 79 deletions.
7 changes: 4 additions & 3 deletions classes_php/Config.php
Expand Up @@ -12,15 +12,16 @@ class Config {

// Use an empty filter by default. The spirit of the default implementation
// is to create non-lossy scans with no post processing. Should you wish to
// override this behaviour then change the filter which will have the scanimage
// output piped to it.
// override this behaviour then change the filter which will have the
// scanimage output piped to it.
//const OutputFilter = "/opt/bin/convert 2>/dev/null - -normalize -sharpen 0x1 ";
const OutputFilter = self::Convert . " - ";

// As with the output filter, the default implementation prefers non-lossy
// output. Should you wish you override this then you can change the output
// type below
// TIFF is supported by most scanners by default and it is a good option if no filters are used
// TIFF is supported by most scanners by default and it is a good option if
// no filters are used
const OutputExtension = Format::TIFF;

// Only useful for development debugging
Expand Down
15 changes: 10 additions & 5 deletions classes_php/FileInfo.php
Expand Up @@ -11,18 +11,23 @@ function FileInfo($fullname) {
$this->fullname = $fullname;

$bytes = filesize($this->fullname);
if ($bytes >= 1000000000) { $bytes = number_format($bytes / 1000000000, 1) . ' GB'; }
elseif ($bytes >= 1000000) { $bytes = number_format($bytes / 1000000, 1) . ' MB'; }
elseif ($bytes >= 1000) { $bytes = number_format($bytes / 1000, 1) . ' KB'; }
else { $bytes = $bytes . ' B'; }
if ($bytes >= 1000000000) {
$bytes = number_format($bytes / 1000000000, 1) . ' GB';
} elseif ($bytes >= 1000000) {
$bytes = number_format($bytes / 1000000, 1) . ' MB';
} elseif ($bytes >= 1000) {
$bytes = number_format($bytes / 1000, 1) . ' KB';
} else {
$bytes = $bytes . ' B';
}

$info = pathinfo($this->fullname);
$this->name = $info['basename'];
$this->path = $info['dirname'];
$this->extension = $info['extension'];
$this->size = $bytes;
$this->lastModified = date("Y-m-d @ H:i:s",filemtime($this->fullname));
}
}

public function Delete() {
if(is_readable($this->fullname)) {
Expand Down
6 changes: 4 additions & 2 deletions classes_php/ScanRequest.php
Expand Up @@ -22,8 +22,10 @@ public function Validate() {
$scannerOptions = $scanner["options"];

foreach ($this->options as $key => $value) {
// If option is not present in device interface or its default value marked "inactive", then remove it
if (!array_key_exists($key, $scannerOptions) || $scannerOptions[$key]->defaultValue === "inactive") {
// If option is not present in device interface or its default
// value marked "inactive", then remove it
if (!array_key_exists($key, $scannerOptions) ||
$scannerOptions[$key]->defaultValue === "inactive") {
unset($this->options[$key]);
// Otherwise, validate the selected value
} else if (!$scannerOptions[$key]->isValidValue($value)) {
Expand Down
28 changes: 20 additions & 8 deletions classes_php/Scanimage.php
Expand Up @@ -8,31 +8,42 @@ private function CommandLine($scanRequest) {
$cmd .= " --format='" . Config::OutputExtension . "'";

// Set device
if (isset($scanRequest->device)) $cmd .= " --device-name='" . $scanRequest->device . "'";
if (isset($scanRequest->device)) {
$cmd .= " --device-name='" . $scanRequest->device . "'";
}

// Set device-specific options
$scanner = ScannerOptions::get($scanRequest->device);
$scannerOptions = $scanner["options"];
foreach ($scanRequest->options as $key => $value) {
// Apparently, on some HP All-In-One (OfficeJet?), as long as --source flag is set,
// the quality will automatically drop to ADF (lower), even though flatbed is selected
if (strtolower($key) == "source" && strtolower($value) == "flatbed" &&
strstr(strtolower($scanner["name"]),"hp") && strstr(strtolower($scanner["description"]),"officejet"))
continue;
if (strtolower($key) == "source" &&
strtolower($value) == "flatbed" &&
strstr(strtolower($scanner["name"]),"hp") &&
strstr(strtolower($scanner["description"]),"officejet")) {
continue;
}

$cmd .= " " . $scannerOptions[$key]->name . " '" . $value . "'";
}

// Make PDF a bit lighter
$cmd2 = Config::OutputFilter;
if ($scanRequest->format == Format::PDF) $cmd2 .= " -compress JPEG -quality 50 ";

if ($scanRequest->format == Format::PDF) {
$cmd2 .= " -compress JPEG -quality 50 ";
}

// No output filter or default output format which is handled by scanimage directly
if (empty(Config::OutputFilter) || $scanRequest->format == Config::OutputExtension)
if (empty(Config::OutputFilter) || $scanRequest->format == Config::OutputExtension) {
$cmd = $cmd . ' > "' . $scanRequest->outputFilepath . '"';
else
} else {
$cmd = $cmd . ' | ' . $cmd2 . ' "' . $scanRequest->outputFilepath . '"';
}

return $cmd;
}

public function Execute($scanRequest) {
$scanResponse = new ScanResponse();
$scanResponse->errors = $scanRequest->Validate();
Expand All @@ -41,6 +52,7 @@ public function Execute($scanRequest) {
System::Execute($scanResponse->cmdline, $scanResponse->output, $scanResponse->returnCode);
$scanResponse->image = $scanRequest->outputFilepath;
}

return $scanResponse;
}
}
Expand Down
22 changes: 14 additions & 8 deletions classes_php/ScannerOptions.php
Expand Up @@ -22,8 +22,9 @@ final class ScannerOptions {
private static $options = NULL;

public static function getAll() {
if (self::$options == NULL)
self::$options = self::parseOptions();
if (self::$options == NULL) {
self::$options = self::parseOptions();
}

return self::$options;
}
Expand Down Expand Up @@ -89,25 +90,30 @@ private static function parseOptions($device = "") {
$option->name = $matched[1];
$option->defaultValue = preg_replace("/(\[|\])/","",$matched[3]);

if (strstr($matched[2],"|")) { // Fixed set of enumerated values, separated with "|"
if (strstr($matched[2],"|")) {
// Fixed set of enumerated values, separated with "|"
$option->values = explode("|",$matched[2]);
$option->isRange = false;
} else if (strstr($matched[2],"..")) { // Values ranging from low to high, i.e., lo ".." hi
} else if (strstr($matched[2],"..")) {
// Values ranging from low to high, i.e., lo ".." hi
$option->values = explode("..",$matched[2]);
$option->isRange = true;
} else { // Single element, manually create array
} else {
// Single element, manually create array
$option->values = array($matched[2]);
$option->isRange = false;
}

// Floor all numerical option values... no reason found to keep them as floats
// Floor all numerical option values... no reason found to keep
// them as floats
if (is_numeric($option->defaultValue)) {
$option->defaultValue = floor($option->defaultValue);
for ($n = 0; $n < count($option->values); ++$n) $option->values[$n] = floor($option->values[$n]);
}

// Check whether interface accepts a range for resolution and enumerate instead
// This is to avoid having to make a slider for resolution in frontend
// Check whether interface accepts a range for resolution and
// enumerate instead. This is to avoid having to make a slider
// for resolution in frontend
if ($option->isRange && $key == "resolution") {
$option->isRange = false;
$increasing = array();
Expand Down
114 changes: 61 additions & 53 deletions install.md
@@ -1,52 +1,21 @@
# installation
Make sure your scanner is [working with SANE](install-sane.md).

## QNAP
```
cd ~
wget --no-check-certificate https://github.com/sbs20/scanserv/archive/master.zip
cd /share/Qweb
sudo unzip ~/master.zip
sudo mv scanserv-master/ scanserv
```

Set variables correctly

```
/opt/bin/nano /share/Qweb/classes_php/Config.php
```

Then set the Scanimage and Convert lines - mine were as follows

```
<?php
class Config {
const IsTrace = false;
const TraceLineEnding = "<br>\n";
const Scanimage = "/opt/bin/scanimage";
const Convert = "/usr/local/sbin/convert";
const BypassSystemExecute = false;
const OutputDirectory = "./output/";
const PreviewDirectory = "./preview/";
const MaximumScanWidthInMm = 215;
const MaximumScanHeightInMm = 297;
}
?>
```
## Debian / Raspbian / Ubuntu

### Test
* You may need to set the permissions of your new directory: `chmod 775 /share/Qweb/scanserv`
* Ensure your QNAP web server is running
* Open your browser and navigate to http://YOUR_QNAP:PORT/scanserv/

## Raspberry Pi / Debian
### Prerequisites

```
sudo apt-get update
sudo apt-get install apache2 apache2-utils libapache2-mod-php5 php5 sane-utils imagemagick
```
On newer debians you will need
```
sudo apt-get install apache2 apache2-utils libapache2-mod-php sane-utils imagemagick
```

Check apache can use SANE. If you know about saned and permissions then you may not need to worry about this.
Check apache can use SANE. If you know about saned and permissions then you may
not need to worry about this.

```
sudo su -m www-data -c 'scanimage --test'
Expand All @@ -58,17 +27,25 @@ if not then try
sudo gpasswd -a www-data scanner
```

### Download and configure
Download and install scanserv (note, this will download a file called master.zip to the current user's home
directory).
### Install
Here's a one liner to install
```
wget -O ~/install.sh https://raw.githubusercontent.com/sbs20/scanserv/master/install.sh && chmod +x ~/install.sh && sudo ~/install.sh
```

### Manual install
Download and install scanserv (note, this will download a file called master.zip
to the current user's home directory).

```
cd ~
sudo wget https://github.com/sbs20/scanserv/archive/master.zip
```
Note, older versions of raspbian install web pages in /var/www, we are assuming /var/www/html as that is
what newer versions use. We are going to install scanserv so that you can access it with the url
http://my.pi.example.com/scanserv
Note, older versions of raspbian install web pages in `/var/www`, we are
assuming `/var/www/html` as that is what newer versions use. We are going to
install scanserv so that you can access it with the url

http://my.example.com/scanserv

```
cd /var/www/html
Expand All @@ -90,19 +67,50 @@ sudo chmod 775 /var/www/html/scanserv/output/
sudo chmod 775 /var/www/html/scanserv/preview/
```

Now configure scanserv to point at the binaries
If you want to change any configuration then look in
`/var/www/html/scanserv/classes_php/Config.php`

## References
* http://forum.qnap.com/viewtopic.php?f=182&t=8351
* http://sourceforge.net/p/phpsane/wiki/FreeBSD/

## QNAP
```
sudo nano /var/www/html/scanserv/classes_php/Config.php
cd ~
wget --no-check-certificate https://github.com/sbs20/scanserv/archive/master.zip
cd /share/Qweb
sudo unzip ~/master.zip
sudo mv scanserv-master/ scanserv
```
* Change /opt/bin/scanimage to /usr/bin/scanimage
* Change /opt/bin/convert to /usr/bin/convert

Edit anything else you think is interesting, though the other defaults should be okay.
Set variables correctly

## References
* http://forum.qnap.com/viewtopic.php?f=182&t=8351
* http://sourceforge.net/p/phpsane/wiki/FreeBSD/
```
/opt/bin/nano /share/Qweb/classes_php/Config.php
```

Then set the Scanimage and Convert lines - mine were as follows

```
<?php
class Config {
const IsTrace = false;
const TraceLineEnding = "<br>\n";
const Scanimage = "/opt/bin/scanimage";
const Convert = "/usr/local/sbin/convert";
const BypassSystemExecute = false;
const OutputDirectory = "./output/";
const PreviewDirectory = "./preview/";
const MaximumScanWidthInMm = 215;
const MaximumScanHeightInMm = 297;
}
?>
```

### Test
* You may need to set the permissions of your new directory: `chmod 775 /share/Qweb/scanserv`
* Ensure your QNAP web server is running
* Open your browser and navigate to http://YOUR_QNAP:PORT/scanserv/

## QNAP NAS install OLD (Pre QTS version 4.0?)
* [Install IPKG](http://wiki.qnap.com/wiki/Install_Optware_IPKG)
Expand Down
28 changes: 28 additions & 0 deletions install.sh
@@ -0,0 +1,28 @@
#!/bin/sh

ROOTUID="0"
if [ "$(id -u)" -ne "$ROOTUID" ] ; then
echo "Error: This script must be executed with root privileges. Try sudo."
exit 1
fi

if [ -e ~/scanserv.zip ] ; then
rm ~/scanserv.zip
fi

wget -O ~/scanserv.zip "https://github.com/sbs20/scanserv/archive/master.zip"

if ! cd /var/www/html; then
echo "Is apache installed? Maybe:"
echo " sudo apt install apache2 apache2-utils libapache2-mod-php"
echo
echo "You may also want: sudo apt install sane-utils imagemagick"
exit 1
fi

sudo unzip ~/scanserv.zip
sudo mv scanserv-master scanserv
sudo chown -R root:www-data scanserv/output/
sudo chown -R root:www-data scanserv/preview/
sudo chmod 775 scanserv/output/
sudo chmod 775 scanserv/preview/

0 comments on commit ab9eed4

Please sign in to comment.