<?php
/*
EAN Generator, a MediaWiki extension for graphical
illustration of EAN-13, EAN-8 and UPC-A barcodes.
Copyright (C) 2008 Markus Szumovski
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Contact author by e-mail: webmaster@wikitimescale.com
Contact author by mail:
Markus Szumovski
Hirtenbergerstrasse 33
2551 Enzesfeld
Austria
*/
function EAN_getEANodd()
{
return array( array(3,2,1,1), array(2,2,2,1), array(2,1,2,2), array(1,4,1,1), array(1,1,3,2), array(1,2,3,1), array(1,1,1,4), array(1,3,1,2), array(1,2,1,3), array(3,1,1,2) );
}
function EAN_getEANeven()
{
return array( array(1,1,2,3), array(1,2,2,2), array(2,2,1,2), array(1,1,4,1), array(2,3,1,1), array(1,3,2,1), array(4,1,1,1), array(2,1,3,1), array(3,1,2,1), array(2,1,1,3) );
}
function EAN_getEANoddoreven()
{
return array( array(0,0,0,0,0,0 ,0,0,0,0,0,0), array(0,0,1,0,1,1 ,0,0,0,0,0,0), array(0,0,1,1,0,1 ,0,0,0,0,0,0), array(0,0,1,1,1,0 ,0,0,0,0,0,0), array(0,1,0,0,1,1 ,0,0,0,0,0,0), array(0,1,1,0,0,1 ,0,0,0,0,0,0), array(0,1,1,1,0,0 ,0,0,0,0,0,0), array(0,1,0,1,0,1 ,0,0,0,0,0,0), array(0,1,0,1,1,0 ,0,0,0,0,0,0), array(0,1,1,0,1,0 ,0,0,0,0,0,0) );
}
function EAN_getUPCEoddoreven()
{
return array( array(0,0,0,1,1,1 ,0,0,0,0,0,0), array(0,0,1,0,1,1 ,0,0,0,0,0,0), array(0,0,1,1,0,1 ,0,0,0,0,0,0), array(0,0,1,1,1,0 ,0,0,0,0,0,0), array(0,1,0,0,1,1 ,0,0,0,0,0,0), array(0,1,1,0,0,1 ,0,0,0,0,0,0), array(0,1,1,1,0,0 ,0,0,0,0,0,0), array(0,1,0,1,0,1 ,0,0,0,0,0,0), array(0,1,0,1,1,0 ,0,0,0,0,0,0), array(0,1,1,0,1,0 ,0,0,0,0,0,0) );
}
function EAN_getCodabarBars()
{
return array( '0' => array( 0, 0, 0, 0, 0, 1, 1 ), '1' => array( 0, 0, 0, 0, 1, 1, 0 ), '2' => array( 0, 0, 0, 1, 0, 0, 1 ), '3' => array( 1, 1, 0, 0, 0, 0, 0 ), '4' => array( 0, 0, 1, 0, 0, 1, 0 ), '5' => array( 1, 0, 0, 0, 0, 1, 0 ), '6' => array( 0, 1, 0, 0, 0, 0, 1 ), '7' => array( 0, 1, 0, 0, 1, 0, 0 ), '8' => array( 0, 1, 1, 0, 0, 0, 0 ), '9' => array( 1, 0, 0, 1, 0, 0, 0 ), '- ' => array( 0, 0, 0, 1, 1, 0, 0 ), '$' => array( 0, 0, 1, 1, 0, 0, 0 ), ':' => array( 1, 0, 0, 0, 1, 0, 1 ), '/' => array( 1, 0, 1, 0, 0, 0, 1 ), '.' => array( 1, 0, 1, 0, 1, 0, 0 ), '+ ' => array( 0, 0, 1, 0, 1, 0, 1 ), 'A' => array( 0, 0, 1, 1, 0, 1, 0 ), 'B' => array( 0, 1, 0, 1, 0, 0, 1 ), 'C' => array( 0, 0, 0, 1, 0, 1, 1 ), 'T' => array( 0, 0, 1, 1, 0, 1, 0 ), 'N' => array( 0, 1, 0, 1, 0, 0, 1 ), '*' => array( 0, 0, 0, 1, 0, 1, 1 ) );
}
function EAN_getCode39Bars()
{
return array( '*' => array(0,3,0,2,1,2,1,2,0), '-' => array(0,3,0,2,0,2,1,2,1), '$' => array(0,3,0,3,0,3,0,2,0), '%' => array(0,2,0,3,0,3,0,3,0), ' ' => array(0,3,1,2,0,2,1,2,0), '.' => array(1,3,0,2,0,2,1,2,0), '/' => array(0,3,0,3,0,2,0,3,0), '+' => array(0,3,0,2,0,3,0,3,0), '0' => array(0,2,0,3,1,2,1,2,0), '1' => array(1,2,0,3,0,2,0,2,1), '2' => array(0,2,1,3,0,2,0,2,1), '3' => array(1,2,1,3,0,2,0,2,0), '4' => array(0,2,0,3,1,2,0,2,1), '5' => array(1,2,0,3,1,2,0,2,0), '6' => array(0,2,1,3,1,2,0,2,0), '7' => array(0,2,0,3,0,2,1,2,1), '8' => array(1,2,0,3,0,2,1,2,0), '9' => array(0,2,1,3,0,2,1,2,0), 'A' => array(1,2,0,2,0,3,0,2,1), 'B' => array(0,2,1,2,0,3,0,2,1), 'C' => array(1,2,1,2,0,3,0,2,0), 'D' => array(0,2,0,2,1,3,0,2,1), 'E' => array(1,2,0,2,1,3,0,2,0), 'F' => array(0,2,1,2,1,3,0,2,0), 'G' => array(0,2,0,2,0,3,1,2,1), 'H' => array(1,2,0,2,0,3,1,2,0), 'I' => array(0,2,1,2,0,3,1,2,0), 'J' => array(0,2,0,2,1,3,1,2,0), 'K' => array(1,2,0,2,0,2,0,3,1), 'L' => array(0,2,1,2,0,2,0,3,1), 'M' => array(1,2,1,2,0,2,0,3,0), 'N' => array(0,2,0,2,1,2,0,3,1), 'O' => array(1,2,0,2,1,2,0,3,0), 'P' => array(0,2,1,2,1,2,0,3,0), 'Q' => array(0,2,0,2,0,2,1,3,1), 'R' => array(1,2,0,2,0,2,1,3,0), 'S' => array(0,2,1,2,0,2,1,3,0), 'T' => array(0,2,0,2,1,2,1,3,0), 'U' => array(1,3,0,2,0,2,0,2,1), 'V' => array(0,3,1,2,0,2,0,2,1), 'W' => array(1,3,1,2,0,2,0,2,0), 'X' => array(0,3,0,2,1,2,0,2,1), 'Y' => array(1,3,0,2,1,2,0,2,0), 'Z' => array(0,3,1,2,1,2,0,2,0));
}
function EAN_UPCTagsRender( $input, $args, $parser )
{
$tmpargs=$args;
$tmpargs['code']='ean8';
return EAN_TagsRender($input,$tmpargs,$parser);
}
function EAN_8TagsRender( $input, $args, $parser )
{
$tmpargs=$args;
$tmpargs['code']='ean8';
return EAN_TagsRender($input,$tmpargs,$parser);
}
function EAN_TagsRender( $input, $args, $parser )
{
global $wgUploadDirectory, $wgUploadPath, $IP, $wgTimelineSettings, $wgArticlePath, $wgTmpDirectory;
global $wgEANBarWidth;
global $wgHashedUploadDirectory;
$barwidth=$wgEANBarWidth;
$size=0;
$codelen=12;
$isnumeric=true;
$prefix='EAN13';
if(isset($args['code']))
{
$args['code']=strtolower($args['code']);
if($args['code']=='ean13' || $args['code']=='ean-13' || $args['code']=='ean 13' || $args['code']=='ean')
{
$codelen=12;
$prefix='EAN13';
}
else if($args['code']=='ean8' || $args['code']=='ean-8' || $args['code']=='ean 8')
{
$codelen=7;
$prefix='EAN8';
}
else if($args['code']=='upca' || $args['code']=='upc-a' || $args['code']=='upc a' || $args['code']=='upc')
{
$codelen=11;
$prefix='UPCA';
}
else if($args['code']=='code39' || $args['code']=='code 39' || $args['code']=='code-39' || $args['code']=='code 3of9' || $args['code']=='code 3 of 9')
{
$codelen=0;
$prefix='Code39';
$isnumeric=false;
$input = '*' . strtoupper($input) . '*';
}
else if($args['code']=='codabar' || $args['code']=='code 2 of 7' || $args['code']=='code 2of7' )
{
$codelen=0;
$prefix='Codabar';
$isnumeric=false;
$input = strtoupper($input);
}
}
if(isset($args['size']) && is_numeric($args['size']))
{
$size=$args['size'];
}
$barwidth=$barwidth+$size;
$input=$parser->recursiveTagParse($input);
if($isnumeric)
{
$tmp='';
for($i=0;$i<strlen($input);$i++)
{
if(is_numeric($input[$i]))
$tmp.=$input[$i];
}
$input=$tmp;
}
$filename = md5( $prefix . 'x' . $size . 'x' . $input ) . '.png';
$filename=strtoupper(substr($filename,0,1)) . substr($filename,1);
if($wgHashedUploadDirectory==true)
{
$md5hash=md5($filename);
$hashx=substr($md5hash,0,1);
$hashxy=substr($md5hash,0,2);
$dirname=$wgUploadDirectory . '/' . $hashx . '/';
$urlname=$wgUploadPath . '/' . $hashx . '/';
if (!is_dir( $dirname ))
{
mkdir($dirname, 0755);
}
$dirname=$dirname . $hashxy . '/';
$urlname=$urlname . $hashxy . '/';
if (!is_dir( $dirname ))
{
mkdir($dirname, 0755);
}
}
else
{
$dirname=$wgUploadDirectory . '/';
$urlname=$wgUploadPath . '/';
}
$fdname = $dirname . $filename;
if(strlen($input)<$codelen && $codelen!=0)
{
$len=strlen($input);
for($i=$len;$i<$codelen;$i++)
$input.='0';
}
if(strlen($input)>=$codelen && (!$isnumeric || is_numeric($input)))
{
if($codelen>0)
$input=substr($input,0,$codelen);
else
$codelen=strlen($input);
for($i=0;$i<$codelen;$i++)
{
if($isnumeric)
$numbers[$i]=intval(substr($input,$i,1));
else
$numbers[$i]=substr($input,$i,1);
}
if($isnumeric)
{
$numbers[$codelen]=0;
$mult=3;
for($i=$codelen-1;$i>=0;$i--)
{
$numbers[$codelen]+=$numbers[$i]*$mult;
if($mult==3)
$mult=1;
else
$mult=3;
}
$numbers[$codelen]=10-$numbers[$codelen]%10;
if($numbers[$codelen]==10)
$numbers[$codelen]=0;
}
if (!file_exists($fdname))
{
$dbw = wfGetDB(DB_MASTER);
$dbw->insert('image',array('img_name' => $filename, 'img_metadata' => '', 'img_description' => '', 'img_user_text' => '', 'img_timestamp' => '0', 'img_sha1' => ''));
if($prefix=='EAN13')
EAN_drawEAN13( $fdname, $numbers, $barwidth, $size );
else if($prefix=='EAN8')
EAN_drawEAN8( $fdname, $numbers, $barwidth, $size );
else if($prefix=='UPCA')
EAN_drawUPCA( $fdname, $numbers, $barwidth, $size );
else if($prefix=='Code39')
EAN_drawCode39( $fdname, $numbers, $barwidth, $size );
else if($prefix=='Codabar')
EAN_drawCodabar( $fdname, $numbers, $barwidth, $size );
}
if(isset($args['options']))
return $parser->recursiveTagParse('[[Image:' . $filename . '|' . $args['options'] . ']]');
else
return $parser->recursiveTagParse('[[Image:' . $filename . ']]');
}
else
return '';
}
function EAN_drawUPCA( $fdname, $numbers, $barwidth, $size )
{
$digit_odd_weight=EAN_getEANodd();
$digit_even_weight=EAN_getEANeven();
$odd_or_even=EAN_getEANoddoreven();
$imgwidth=($barwidth*84)+($barwidth*11)+28;
$img = imagecreate ($imgwidth, 100+(30*$size));
$pixel=14;
$white_color = imagecolorallocate ($img, 255, 255, 255);
$black_color = imagecolorallocate ($img, 0, 0, 0);
if($barwidth==1)
imagestring ($img, 2, 5, 72+(30*$size), $numbers[0], $black_color);
else
imagestring ($img, 5, 3, 72+(30*$size), $numbers[0], $black_color);
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$pixel+=2;
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$black=FALSE;
for($i=0;$i<12;$i++)
{
if($i>0 && $i<6)
{
if($barwidth==1)
imagestring ($img, 2, $pixel-($barwidth*3), 72+(30*$size), $numbers[$i], $black_color);
else
imagestring ($img, 5, $pixel-($barwidth*3), 72+(30*$size), $numbers[$i], $black_color);
}
if($i>=6 && $i<11)
{
if($barwidth==1)
imagestring ($img, 2, $pixel+($barwidth*4), 72+(30*$size), $numbers[$i], $black_color);
else
imagestring ($img, 5, $pixel+($barwidth*4), 72+(30*$size), $numbers[$i], $black_color);
}
for($u=0;$u<4;$u++)
{
$weight=$digit_odd_weight[$numbers[$i]][$u];
if($black==TRUE)
{
$pixel=EAN_drawBar( $img, $pixel, $weight, $black_color, $barwidth, 70+(30*$size) );
$black=FALSE;
}
else
{
$pixel+=$weight*$barwidth;
$black=TRUE;
}
}
if($i==5)
{
$pixel+=$barwidth;
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$pixel+=$barwidth;
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$pixel+=$barwidth;
$black=TRUE;
}
}
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$pixel+=$barwidth;
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
if($barwidth==1)
imagestring ($img, 2, $imgwidth-imagefontwidth(5)-2, 72+(30*$size), $numbers[11], $black_color);
else
imagestring ($img, 5, $imgwidth-imagefontwidth(5)-2, 72+(30*$size), $numbers[11], $black_color);
imagepng ($img,$fdname);
}
function EAN_drawEAN8( $fdname, $numbers, $barwidth, $size )
{
$digit_odd_weight=EAN_getEANodd();
$digit_even_weight=EAN_getEANeven();
$odd_or_even=EAN_getEANoddoreven();
$img = imagecreate (($barwidth*56)+($barwidth*11)+24, 100+(30*$size));
$pixel=14;
$white_color = imagecolorallocate ($img, 255, 255, 255);
$black_color = imagecolorallocate ($img, 0, 0, 0);
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$pixel+=2;
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$black=FALSE;
for($i=0;$i<8;$i++)
{
if($barwidth==1)
imagestring ($img, 2, $pixel+$barwidth, 72+(30*$size), $numbers[$i], $black_color);
else
imagestring ($img, 5, $pixel+$barwidth, 72+(30*$size), $numbers[$i], $black_color);
for($u=0;$u<4;$u++)
{
$weight=$digit_odd_weight[$numbers[$i]][$u];
if($black==TRUE)
{
$pixel=EAN_drawBar( $img, $pixel, $weight, $black_color, $barwidth, 70+(30*$size) );
$black=FALSE;
}
else
{
$pixel+=$weight*$barwidth;
$black=TRUE;
}
}
if($i==3)
{
$pixel+=$barwidth;
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$pixel+=$barwidth;
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$pixel+=$barwidth;
$black=TRUE;
}
}
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$pixel+=$barwidth;
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
imagepng ($img,$fdname);
}
function EAN_drawEAN13( $fdname, $numbers, $barwidth, $size )
{
$digit_odd_weight=EAN_getEANodd();
$digit_even_weight=EAN_getEANeven();
$odd_or_even=EAN_getEANoddoreven();
$img = imagecreate (($barwidth*84)+($barwidth*11)+24, 100+(30*$size));
$white_color = imagecolorallocate ($img, 255, 255, 255);
$black_color = imagecolorallocate ($img, 0, 0, 0);
$pixel=14;
if($barwidth==1)
imagestring ($img, 2, 5, 72+(30*$size), $numbers[0], $black_color);
else
imagestring ($img, 5, 3, 72+(30*$size), $numbers[0], $black_color);
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$pixel+=2;
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$black=FALSE;
for($i=1;$i<13;$i++)
{
if($barwidth==1)
imagestring ($img, 2, $pixel+$barwidth, 72+(30*$size), $numbers[$i], $black_color);
else
imagestring ($img, 5, $pixel+$barwidth, 72+(30*$size), $numbers[$i], $black_color);
for($u=0;$u<4;$u++)
{
if($odd_or_even[$numbers[0]][$i-1]==0)
{
//odd
$weight=$digit_odd_weight[$numbers[$i]][$u];
}
else
{
//even
$weight=$digit_even_weight[$numbers[$i]][$u];
}
if($black==TRUE)
{
$pixel=EAN_drawBar( $img, $pixel, $weight, $black_color, $barwidth, 70+(30*$size) );
$black=FALSE;
}
else
{
$pixel+=$weight*$barwidth;
$black=TRUE;
}
}
if($i==6)
{
$pixel+=$barwidth;
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$pixel+=$barwidth;
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$pixel+=$barwidth;
$black=TRUE;
}
}
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
$pixel+=$barwidth;
$pixel=EAN_drawBar( $img, $pixel, 1, $black_color, $barwidth, 75+(30*$size) );
imagepng ($img,$fdname);
}
function EAN_drawCode39( $fdname, $numbers, $barwidth, $size )
{
$code_bars=EAN_getCode39Bars();
$img = imagecreate (($barwidth*13*count($numbers))+28, 100+(30*$size));
$white_color = imagecolorallocate ($img, 255, 255, 255);
$black_color = imagecolorallocate ($img, 0, 0, 0);
$pixel=14;
$black=FALSE;
for($i=0;$i<count($numbers);$i++)
{
if($barwidth==1)
imagestring ($img, 2, $pixel+$barwidth, 72+(30*$size), $numbers[$i], $black_color);
else
imagestring ($img, 5, $pixel+$barwidth, 72+(30*$size), $numbers[$i], $black_color);
if(array_key_exists($numbers[$i],$code_bars))
{
for($u=0;$u<9;$u++)
{
if($code_bars[$numbers[$i]][$u]==0)
{
//odd
$weight=1;
$pixel=EAN_drawBar( $img, $pixel, $weight, $black_color, $barwidth, 70+(30*$size) );
$black=TRUE;
}
else if($code_bars[$numbers[$i]][$u]==1)
{
//even
$weight=2;
$pixel=EAN_drawBar( $img, $pixel, $weight, $black_color, $barwidth, 70+(30*$size) );
$black=TRUE;
}
else if($code_bars[$numbers[$i]][$u]==2)
{
$weight=1;
$pixel+=$weight*$barwidth;
$black=FALSE;
}
else
{
$weight=2;
$pixel+=$weight*$barwidth;
$black=FALSE;
}
}
$pixel+=1*$barwidth;
}
}
imagepng ($img,$fdname);
}
function EAN_drawCodabar( $fdname, $numbers, $barwidth, $size )
{
$code_bars=EAN_getCodabarBars();
$img = imagecreate (($barwidth*10*count($numbers))+28, 100+(30*$size));
$white_color = imagecolorallocate ($img, 255, 255, 255);
$black_color = imagecolorallocate ($img, 0, 0, 0);
$pixel=14;
for($i=0;$i<count($numbers);$i++)
{
$black=TRUE;
if($barwidth==1)
imagestring ($img, 2, $pixel+$barwidth, 72+(30*$size), $numbers[$i], $black_color);
else
imagestring ($img, 5, $pixel+$barwidth, 72+(30*$size), $numbers[$i], $black_color);
if(array_key_exists($numbers[$i],$code_bars))
{
for($u=0;$u<7;$u++)
{
if($code_bars[$numbers[$i]][$u]==0)
{
$weight=1;
}
else
{
$weight=2;
}
if($black==TRUE)
{
$pixel=EAN_drawBar( $img, $pixel, $weight, $black_color, $barwidth, 70+(30*$size) );
$black=FALSE;
}
else
{
$pixel+=$weight*$barwidth;
$black=TRUE;
}
}
$pixel+=1*$barwidth;
}
}
imagepng ($img,$fdname);
}
function EAN_drawBar( $img, $pixel, $weight, $color, $width, $height )
{
global $wgEANBarWidth;
for($i=0;$i<$weight; $i++)
{
for($u=0;$u<$width;$u++)
{
imageline($img,$pixel,10,$pixel,$height,$color);
$pixel++;
}
}
return $pixel;
}
?>