バーコードを作成するためのWebサービス「バーコード作成」と「QRコード作成」をリリースしました。各種バーコードに対応しています。
「バーコードの作成」を参考にさせていただきました。
今回対応したバーコード
- JAN-13/EAN-13/ISBN-13
- UPC-A
- CODE 39
- Code 25 Interleaved 2 of 5(ITF)
- CODE 128
- PostNet
- 日本郵便 カスタマバーコード
ところで「日本郵便 カスタマバーコード」は需要がないのかライブラリがないのでバーコードに必要な文字情報の抜き出し法を参考にして書いてみたのが以下のPHPコード。漢数字は面倒なので対応していません。もう少しスマートなやり方はないだろうか?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | <?php function validator_imbjp($text){ $text = trim($text); $text = strtoupper($text); $rep_pat= array("&","/","・","."); $text = str_replace($rep_pat,'',$text); $text = preg_replace('/[A-Z]{2,}/','',$text); // /[0-9]F/ -> [0-9]-, 2-3-$ => 2-3 if(preg_match_all('/[0-9]F/',$text, $matches)){ foreach($matches[0] as $key => $value){ $mat[$key] = str_replace('F','-',$value); } $text = str_replace($matches[0],$mat,$text); $text = preg_replace('/-$/','',$text); } if(preg_match_all('/[A-Z0-9\-]+/', $text, $matches)){ $text = implode('-', $matches[0]); } else { return; } // A-2 => A2, L-B => LB if(preg_match_all('/-*[A-Z]-*/',$text, $matches)){ foreach($matches[0] as $key => $value){ $mat[$key] = str_replace('-','',$value); } $text = str_replace($matches[0],$mat,$text); } $text = str_replace('--','-',$text); if(substr($text,3,1)=='-'){ $text = substr_replace($text,'',3,1); } if(substr($text,7,1)=='-'){ $text = substr_replace($text,'',7,1); } //numstring for ($idx = 0; $idx < strlen($text); $idx++) { $char = substr($text, $idx, 1); for ($baridx = 0; $baridx < strlen($this->_coding_map[$char]);) { $numstring[] = substr($this->_coding_map[$char], $baridx, 3); $baridx = $baridx+3; } } //numstring length check(add and remove) if(count($numstring) < 20){ for($idx = count($numstring); $idx < 20; $idx++){ $numstring[] = '432'; } } $numstring = array_slice($numstring,0,20); //Check digit foreach($numstring as $value) { $checksum = $checksum + $this->_checkdg_map[$value]; } if(($checksum % 19) == 0) { $checkdigit = 0; } else { $checkdigit = (19 - ($checksum % 19)); } //leader + number + checkdigit + trailer $numstring = $this->_coding_map['leader'].implode('',$numstring).$this->_coding_map[$checkdigit].$this->_coding_map['trailer']; return $numstring; } |
最終的な出力はPEAR::Image_Barcodeにおまかせ。参考までに、Image_Barcodeで使える「日本郵便 カスタマバーコード」クラスを置いておきます。
- ダウンロード: Image_Barcode_imbjp001.zip (8.7 KB)