Raketenwilli: Wer hat Linux, PHP und einen Scanner? (Ich brauch mal Hilfe....)

Beitrag lesen

Ich schreibe gerade ein PHP-Skript zum Einscannen von Dokumenten/Bildern und brauche dazu ein wenig Hilfe.

Helfen kann, wer Linux, PHP und einen Scanner hat.

Das Skript stellt fest, welche Scanner erreichbar sind und ermittelt deren Eigenschaften. Diese müssen aber „geparst“ werden, wozu ich leider nur die Daten meines Scanners habe.

  • Ich hätte gerne weitere Scanner damit getestet.

Wer es ausführt muss Mitglied der Gruppe „sane“ scanner sein (also scannen können. Das Skript ändert nichts am System.

Bitte in einem Terminal ausführen, die Ausgaben posten.

Das Skript läuft, ist aber nicht fertig (Es fehlen z.B. auch noch Fehlerbehandlungen). Deshalb habe ich es als „bad“ markiert

<?php

class allScanners {
	
	public $scanners;
	
	function __construct() {
		
		$output = `/usr/bin/scanimage -L 2> /dev/null`;
		$lines  = explode ("\n", $output);
		foreach ( $lines as $line ) {
			$line = trim( $line );
			if ( $line ) {
				$device = str_replace( 'device `', '', $line );
				$device = preg_replace('/\'.*$/', '', $device );
				$name   = preg_replace('/^.* is a /', '', $line );
				$name   = preg_replace('/ scanner$/', '', $name );
				$this->scanners[$device]['name'] = $name;
			}
		}
		foreach ( array_keys( $this->scanners ) as $device ) {
			
			$output = `scanimage --help -Av -d "$device" 2> /dev/null| grep -P 'All options specific to device|^ +-'`;
			echo '-----------------------------------------------------------' . PHP_EOL;
			echo $output. PHP_EOL;
			echo '-----------------------------------------------------------' . PHP_EOL;
			$isHelp = true;
			foreach ( explode("\n", $output ) as $line ) {
				$line = trim( $line );
				if ( $line ) {
					if ( $isHelp ) {
						if ( 0 === strpos( $line, 'All options' ) ) {
							$isHelp = false;
						}
					} else {
						list( $optionName, $optionString ) = explode( ' ', $line, 2 );
						$optionName=trim($optionName, '-');
						switch ( $optionName ) {
							case "l":
								$this->scanners[$device]['options'][$optionName] = $this->parseValueFromTo( $optionName, $optionString );
								break;
							case "t":
								$this->scanners[$device]['options'][$optionName] = $this->parseValueFromTo( $optionName, $optionString );
								break;
							case "x":
								$this->scanners[$device]['options'][$optionName] = $this->parseValueFromTo( $optionName, $optionString );
								break;
							case "y":
								$this->scanners[$device]['options'][$optionName] = $this->parseValueFromTo( $optionName, $optionString );
								break;
							case "resolution":
								$this->scanners[$device]['options'][$optionName] = $this->parseValueList( $optionName, $optionString );
								break;
							case "mode":
								$this->scanners[$device]['options'][$optionName] = $this->parseValueList( $optionName, $optionString );
								break;
							case "source":
								$this->scanners[$device]['options'][$optionName] = $this->parseValueList( $optionName, $optionString );
								break;
							default:
								user_error( "Unbekannte Option: '${optionName}' mit String '${optionString}'", E_USER_NOTICE );
						}
					}
				}
			}
		}
	}
	
	function parseValueFromTo( $optionName, $s ) {
		$ret = [];
		$FromTo = false;
		$default = false;
		list( $FromTo, $default) = explode( ' ', trim( $s ), 2 );
		if ( $FromTo and $default) {
			list( $ret['min'], $ret['max'] ) = explode( '..', $FromTo, 2 );
			$ret['type'] = 'FromTo';
			$ret['min'] = floatval( $ret['min'] );
			$ret['max'] = floatval( $ret['max'] );
			$ret['default'] = floatval( trim('[', trim( ']', $default) ) );
			return $ret;
		} else {
			user_error( "Werte für Option ${optionName} sind nicht feststellbar", E_USER_NOTICE );
			return false;
		}
		
	}
	
	function parseValueList( $optionName, $s ) {
		$ret = [];
		$s = trim( $s );
		
		$ret['type'] = 'ValueList';
		$ret['default'] = false;
		$ret['options'] = false;

		$o = preg_replace( '/ \[.*\]$/', '', $s );
		$o = preg_replace( '/dpi$/', '', $o );
		$ret['options'] = explode('|', $o);
		
		$ret['default'] = preg_replace( '/^.*\[/', '', $s );
		$ret['default'] = trim( $ret['default'], ']' );		
		
		return $ret;
	}
}

$sallScanners = new allScanners();
print_r ($sallScanners);

Ziel ist übrigens, einen Raspi oder dergleichen mit einem nicht netzwerkfähigen Scanner zu verknüpfen und dann das Scannen via http[s] anzubieten.