phpSANE – a total rewrite
I made a number of improvements to phpSANE a few years ago, but forgot to submit them back to the wider community. I was told by a few users that the new preview function was hard to use, so decided to improve it and share the code.
First of though, what is phpSANE? It is a php front end for the SANE (Scanner Access Now Easy), which is a scanner/image protocol a bit like TWAIN, but for Linux. Its massive advantage is it allows a scanner to be used from any networked computer via a web browser.
After looking at the existing code, I decided that it would be quicker to start from scratch, creating an interface that allowed the following:
- Ability to switch between connected scanners
- Settings based on the scanner selected
- Scalable previews
- Drag and moveable preview selection
- Help fields for each setting
- History of images scanned
After plugging in a new scanner and installing sane
$sudo apt-get install sane
apache2 and php5 were already installed, and so there was no need to install them. If you were to install them, it would be something like this:
$sudo apt-get install php5 php5-gd apache2 apache2-common
The SANE program creates .pnm files (a bit like like .tiff’s) and so there is also a small utility called netpbm which will contains the command pnmtojpeg which is needed.
$sudo apt-get install netpbm
Next job to is to test it all works: This should warm up the scanner, and create the image file /tmp/image.pnm
$scanimage > /tmp/image.pnm
Unless logged in as root, this will return an error as only root (or sudo) has the permission to run scanimage. The apache user, (e.g. www-data, apache, nobody) will need permission to run scanimage.
Create a group called saneusers, and add the apache user to the group.
$sudo visudo
At the end of the file, add this line:
%saneusers ALL=NOPASSWD:/usr/bin/scanimage
Running the next command should now work without any errors:
$scanimage > /tmp/image.pnm
Now for the programming to start.








