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 http://www.macports.org/install.php

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/mod_alias.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule bonjour_module     libexec/apache2/mod_bonjour.so
LoadModule php5_module        libexec/apache2/libphp5.so
#LoadModule fastcgi_module     libexec/apache2/mod_fastcgi.so


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

<?php

//phpinfo()

$bc=$_GET["b"];

$bt=$_GET["text"];

//echo $bt;

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

$barcpath="www/barcode.jpg";

//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

BC=$1

shift

#build postscript code

echo "gsave" >/Users/robert/Sites/www/hello1.ps

echo "50 50 translate" >>/Users/robert/Sites/www/hello1.ps

echo "2 2 scale" >>/Users/robert/Sites/www/hello1.ps



echo "50 50 moveto (${*}) () /${BC} /uk.co.terryburton.bwipp findresource exec" >> /Users/robert/Sites/www/hello1.ps

echo "grestore" >> /Users/robert/Sites/www/hello1.ps

echo "showpage" >> /Users/robert/Sites/www/hello1.ps

#run ghostscript to generate EPS

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

#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 barcode.ps to the www directory.

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

END RESULT


No comments:

Post a Comment

Google +