Пример #1
0
function barcode($image, $step = 1, $length = false)
{
    if (function_exists('imagefilter') && function_exists('imagetruecolortopalette') && function_exists('imagecolorset') && function_exists('imagecolorclosest')) {
        //Gaussian blur to fill in holes from dithering
        imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
        //force two colors; dithering = FALSE
        imagetruecolortopalette($image, FALSE, 2);
        //find the closest color to black and replace it with actual black
        imagecolorset($image, imagecolorclosest($image, 0, 0, 0), 0, 0, 0);
        //find the closest color to white and replace it with actual white
        imagecolorset($image, imagecolorclosest($image, 255, 255, 255), 255, 255, 255);
    }
    //search
    $height = imagesy($image);
    for ($i = $step; $i < $height - $step; $i += $step) {
        $a = barWidth($image, $i);
        $w = nwWidth($a);
        if ($w['n'] != 0 && $w['w'] != 0) {
            $s = widthsToNW($a, $w['n'], $w['w']);
            if (validate($s)) {
                $code = NWtoCode($s);
                if ($code != "false" && (!$length || strlen($code) == $length)) {
                    return $code;
                }
            } else {
                if (validateCodaBar($s)) {
                    $code = NWtoCodeCodaBar($s . "N");
                    //remember to add the last space
                    if ($code != "false") {
                        $code = substr($code, 1, -1);
                        //remove the start and stop characters
                        if (!$length || strlen($code) == $length) {
                            return $code;
                        }
                    }
                }
            }
        }
    }
    return false;
}
Пример #2
0
function barcode($image, $step = 1, $length = false, $numsonly = false)
{
    if (function_exists('imagefilter') && function_exists('imagetruecolortopalette') && function_exists('imagecolorset') && function_exists('imagecolorclosest')) {
        //Gaussian blur to fill in holes from dithering
        imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
        //force two colors; dithering = FALSE
        imagetruecolortopalette($image, FALSE, 2);
        //find the closest color to black and replace it with actual black
        imagecolorset($image, imagecolorclosest($image, 0, 0, 0), 0, 0, 0);
        //find the closest color to white and replace it with actual white
        imagecolorset($image, imagecolorclosest($image, 255, 255, 255), 255, 255, 255);
    }
    //search
    $height = imagesy($image);
    for ($i = $step; $i < $height - $step; $i += $step) {
        $a = barWidth($image, $i);
        $w = nwWidth($a);
        if ($w['n'] != 0 && $w['w'] != 0) {
            $s = widthsToNW($a, $w['n'], $w['w']);
            if (validate($s)) {
                $code = NWtoCode($s);
                if ($code != "false" && (!$length || strlen($code) == $length)) {
                    return $code;
                }
            } else {
                if (validateCodaBar($s)) {
                    $code = NWtoCodeCodaBar($s . "N");
                    //remember to add the last space
                    if ($code != "false") {
                        $code = substr($code, 1, -1);
                        //remove the start and stop characters
                        if (!$length || strlen($code) == $length) {
                            return $code;
                        }
                    }
                }
            }
        }
    }
    if (OCR_ENABLED) {
        //use tesseract to find the "barcode" or any text as OCR
        $tmp = tempnam(TEMPORARY_DIRECTORY, "BARCODE");
        imagepng($image, $tmp);
        exec(TESSERACT_BIN . " {$tmp} {$tmp} -psm 3");
        //run tessearct in single line mode
        $result = file_get_contents($tmp . ".txt");
        unlink($tmp);
        unlink($tmp . ".txt");
        if (!empty($result)) {
            //check length if set to check
            //
            //
            if ($length && !$numsonly) {
                $res = preg_match("/([0-9]{" . $length . "})/", $result, $matches);
                if ($res) {
                    return $matches[0];
                }
            }
            $nums = preg_replace('/\\D+/', '', $result);
            // numbers only
            if ($length && strlen($nums) == $length) {
                return $nums;
            } else {
                if (!$length && !$numsonly) {
                    return $result;
                } else {
                    if (!$length && $numsonly) {
                        return $nums;
                    }
                }
            }
            //if no length is set just return what is read by OCR
        }
    }
    return false;
}