Skip to content

Fix EAN/UPC HTML barcode rendering with showCode - #217

Open
siarheipashkevich wants to merge 1 commit into
milon:masterfrom
siarheipashkevich:fix/ean-html-retail-hri
Open

siarheipashkevich wants to merge 1 commit into
milon:masterfrom
siarheipashkevich:fix/ean-html-retail-hri

Conversation

@siarheipashkevich

@siarheipashkevich siarheipashkevich commented Oct 1, 2026 •

Copy link
Copy Markdown

Bug

getBarcodeHTML() never learned about the retail UPC-A/EAN layout (commit ca3e928, "Add QR logo overlay and retail UPC-A/EAN layout"). Digit bars already use 7/11 of $h, but the HTML renderer still subtracts showCode + 12 from every bar, so bars get shortened twice and the digits are drawn as a single centered string.

Example: getBarcodeHTML('5901234123457', 'EAN13', 2, 55, 'black', 14)

before after
digit bars 7/11 * 55 - (14 + 12) = 9px 35px
guard bars 55 - 26 = 29px 55px
digits one centered string at the bottom of the box split retail-style: 5 901234 123457
root box height 55px 55px (unchanged)

The PNG and SVG renderers already handle this through usesEanUpcLayout() (if ($showCode && !$eanLayout)), the HTML renderer was left behind. A rendered comparison is attached in the issue/PR discussion if useful.

Fix

  • getBarcodeHTML() skips the showCode + 12 subtraction for the eanupc layout and reserves the same quiet-zone label margins SVG/PNG use;
  • new buildEanUpcHriHtml() mirrors buildEanUpcHriSvg(): first/last digit beside the bars, middle digits centered under the left/right data zones for EAN-8, EAN-13, UPC-A and UPC-E;
  • non-EAN symbologies keep the exact previous behaviour (bars shortened by fontSize + 12, full code centered at the bottom);
  • showCode = true now falls back to the 12px font SVG uses instead of rendering 1px digits. The parameter is documented as an int font size, but passing the SVG default (true) silently produced unreadable output.

Tests

Added HTML coverage next to the existing SVG/PNG retail-layout tests in tests/UpcALayoutAndQrLogoTest.php:

  • testEan13HtmlRetailLayoutKeepsFullHeightAndSplitHri
  • testEan13HtmlWithoutShowCodeHasNoDigits
  • testEan13HtmlShowCodeTrueUsesSvgDefaultFontSize
  • testNonEanHtmlStillReservesSpaceForCode

vendor/bin/phpunit on PHP 8.5: 75 tests, 225 assertions, all green.


milon-barcode-html-before-after

The HTML renderer never learned about the retail UPC-A/EAN layout:
it still subtracted showCode + 12 from every bar while digit bars
already use 7/11 of the requested height. As a result the bars were
chopped to a few pixels (7/11 * 55 - 26 = 9px for showCode 14 at
h=55) and the human-readable digits were drawn as one centered
string overlapping the taller guard bars.

Skip the subtraction for the eanupc layout, mirroring SVG/PNG, and
render the split HRI digits positioned the same way as
buildEanUpcHriSvg does. Non-EAN symbologies keep the previous
behaviour; a boolean showCode now falls back to the 12px font size
SVG uses instead of blowing up to 1px.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant