Monday, February 14, 2011

MAC: Creating a basic barcode web service with php

A new part time project for me is to  learn and use Apache and PHP.

First project is to generate barcodes from supplied url parameters.

Here is version 0.1, with no error checking or user input validation - maybe in the next version.
My Mac = Imac running snow leopard 10.6.6

Get Barcode Writer in Pure Postscript here.

1) Install Xcode and MacPorts

Links to both can be found here

Then install both ghostscript and imagemagick with macports.

2) Install ghostscript
sudo port install ghostscript

check its installed with the command gs -h

3) Install imagemagick

sudo port install imagemagick

check its installed with the command convert -h

4) Apache activate PHP and start Apache

uncomment out the php5_module line.

cd /etc/apache2
sudo vi httpd.conf

LoadModule alias_module libexec/apache2/
LoadModule rewrite_module libexec/apache2/
LoadModule bonjour_module     libexec/apache2/
LoadModule php5_module        libexec/apache2/
#LoadModule fastcgi_module     libexec/apache2/

Start Apache web server - system preferences - sharing - web sharing

5) php code (really simple :) run a bash script to generate the barcodes from the supplied url parameters.
I called the php c.php





//echo $bt;

system("/Users/robert/Sites/barc.bash $bc $bt");


//echo $_GET["texto"];

echo "<img src=\"$barcpath\">";


6) BASH script (really simple :) no error checking nor options for the barcodes at the moment so text etc will be checked in the future.
I called the bash script barc.bash



#build postscript code

echo "gsave" >/Users/robert/Sites/www/

echo "50 50 translate" >>/Users/robert/Sites/www/

echo "2 2 scale" >>/Users/robert/Sites/www/

echo "50 50 moveto (${*}) () /${BC} / findresource exec" >> /Users/robert/Sites/www/

echo "grestore" >> /Users/robert/Sites/www/

echo "showpage" >> /Users/robert/Sites/www/

#run ghostscript to generate EPS

/opt/local/bin/gs -dBATCH -dNOPAUSE -q -sDEVICE=epswrite -sOutputFile=/Users/robert/Sites/www/$$.eps /Users/robert/Sites/www/ /Users/robert/Sites/www/

#imagemagick to convert from EPS to jpg

/opt/local/bin/convert -density 72 /Users/robert/Sites/www/$$.eps /Users/robert/Sites/www/$$.jpg

#copy the file to barcode.jpg

cp /Users/robert/Sites/www/$$.jpg /Users/robert/Sites/www/barcode.jpg

7) Before running create www directory in the users home directory, here the home dir is ~robert

mkdir www
cd www

move/copy to the www directory.

change the owner and group of and www directory
sudo chown _www:_www
himbleton:Sites robert$ ls -ld www
drwxr-xr-x  49 _www  _www  1666 Feb 14 23:26 www


No comments:

Post a Comment

Google +