Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Show barcode image on product page - Fixes #1728 #4227

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions lib/ProductOpener/Images.pm
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,28 @@ HTML


$scripts .= <<JS
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.0/dist/JsBarcode.all.min.js"></script>
<script type="text/javascript">
// This code will execute only when javascript is enabled.

// Get the barcode `span` element from DOM.
const barcode = document.getElementById('barcode');

// Get the barcode_paragraph `p` element from DOM.
const barcodeParagraph = document.getElementById('barcode_paragraph');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The barcode_paragraph element does not exist, if the barcode doesn't start with a 0, so that should likely be checked before doing all of this. Maybe even the JsBarcode.all.min.js script only needs to be included, if we know the barcode will be replaced by an SVG.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it may need to be limited to some page types, because right now, the barcode is also replaced if I still have to log in: image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you mention the pages, on which the barcode is needed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephanegigandet @teolemon Where did you initially want the barcode to be displayed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No one is replying 🙁

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ArishSultan, sorry for my late reply. We should show the barcode only the product info page: https://world.openfoodfacts.org/product/5022019160310/organic-ginger-beer-belvoir

Ideally it would appear when we click on a barcode button.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, i am on it


// Only if the barcodeParagraph exists.
if (barcodeParagraph) {
// Remove barcode span and add svg
barcodeParagraph.removeChild(barcode);

const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
barcodeParagraph.appendChild(svg);

// generate the barcode svg
JsBarcode(svg, barcode.innerHTML);
}
</script>
<script type="text/javascript" src="/js/dist/jquery.iframe-transport.js"></script>
<script type="text/javascript" src="/js/dist/jquery.fileupload.js"></script>
<script type="text/javascript" src="/js/dist/load-image.all.min.js"></script>
Expand Down Expand Up @@ -445,9 +467,9 @@ sub get_code_and_imagefield_from_file_name($$) {
}

# Check for a specified imagefield

$filename =~ s/(table|nutrition(_|-)table)/nutrition/i;

if ($filename =~ /((front|ingredients|nutrition)((_|-)\w\w\b)?)/i) {
$imagefield = $1;
$imagefield =~ s/-/_/;
Expand Down Expand Up @@ -479,7 +501,7 @@ sub process_image_upload($$$$$$$) {
my $debug_string_ref = shift; # to return debug information to clients

$log->debug("process_image_upload", { product_id => $product_id, imagefield => $imagefield }) if $log->is_debug();

# The product_id can be prefixed by a server (e.g. off:[code]) with a different $www_root
my $product_www_root = www_root_for_product_id($product_id);
my $product_data_root = data_root_for_product_id($product_id);
Expand Down Expand Up @@ -527,7 +549,7 @@ sub process_image_upload($$$$$$$) {
}
}
}

local $log->context->{imagefield} = $imagefield;
local $log->context->{uploader} = $userid;
local $log->context->{file} = $file;
Expand All @@ -536,9 +558,9 @@ sub process_image_upload($$$$$$$) {
# Check if we have already received this image before
my $images_ref = retrieve("$product_data_root/products/$path/images.sto");
defined $images_ref or $images_ref = {};

my $file_size = -s $file;

if (($file_size > 0) and (defined $images_ref->{$file_size})) {
$log->debug("we have already received an image with the same size", {file_size => $file_size, imgid => $images_ref->{$file_size}}) if $log->is_debug();
${$imgid_ref} = $images_ref->{$file_size};
Expand Down Expand Up @@ -614,7 +636,7 @@ sub process_image_upload($$$$$$$) {
$bg->Composite(compose => 'Over', image => $source);
$source = $bg;
}

my $img_jpg = "$product_www_root/images/products/$path/$imgid.jpg";

$source->Set('quality',95);
Expand All @@ -623,33 +645,33 @@ sub process_image_upload($$$$$$$) {
# Check that we don't already have the image
my $size_orig = -s $img_orig;
my $size_jpg = -s $img_jpg;

local $log->context->{img_size_orig} = $size_orig;
local $log->context->{img_size_jpg} = $size_jpg;

$debug .= " - size of image file received: $size_orig - saved jpg: $size_jpg";

$log->debug("comparing existing images with size of new image", { img_orig => $img_orig, size_orig => $size_orig, img_jpg => $img_jpg, size_jpg => $size_jpg }) if $log->is_debug();
for (my $i = 0; $i < $imgid; $i++) {

# We did not store original files sizes in images.sto and original files in [imgid].[extension].orig before July 2020,
# but we stored original PNG files before they were converted to JPG in [imgid].png
# We compare both the sizes of the original files and the converted files

my @existing_images = ("$product_www_root/images/products/$path/$i.jpg");
if (-e "$product_www_root/images/products/$path/$i.$extension.orig") {
push @existing_images, "$product_www_root/images/products/$path/$i.$extension.orig";
}
if (($extension ne "jpg") and (-e "$product_www_root/images/products/$path/$i.$extension")) {
push @existing_images, "$product_www_root/images/products/$path/$i.$extension";
}

foreach my $existing_image (@existing_images) {

my $existing_image_size = -s $existing_image;

foreach my $size ($size_orig, $size_jpg) {

$log->debug("comparing image", { existing_image_index => $i, existing_image => $existing_image, existing_image_size => $existing_image_size }) if $log->is_debug();
if ((defined $existing_image_size) and ($existing_image_size == $size)) {
$log->debug("image with same size detected", { existing_image_index => $i, existing_image => $existing_image, existing_image_size => $existing_image_size }) if $log->is_debug();
Expand Down Expand Up @@ -765,7 +787,7 @@ sub process_image_upload($$$$$$$) {
my $code = $product_id;
$code =~ s/.*\///;
symlink("$product_www_root/images/products/$path/$imgid.jpg", "$product_data_root/new_images/" . time() . "." . $code . "." . $imagefield . "." . $imgid . ".jpg");

# Save the image file size so that we can skip the image before processing it if it is uploaded again
$images_ref->{$size_orig} = $imgid;
store("$product_data_root/products/$path/images.sto", $images_ref);
Expand Down Expand Up @@ -842,7 +864,7 @@ sub process_image_move($$$$) {
if (defined $product_ref->{images}{$imgid}) {

my $ok = 1;

my $new_imgid;
my $debug;

Expand Down
3 changes: 2 additions & 1 deletion templates/display_product.tt.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ <h1 property="food:name" itemprop="name">[% title %]</h1>
<!-- Display UPC code if the EAN starts with 0 -->
[% IF upc_code == 'defined' %]
<p id="barcode_paragraph">
[% lang("barcode") %]: <span id="barcode" property="food:code" itemprop="gtin13" style="speak-as:digits;">[% code %]</span>[% upc %]
[% lang("barcode") %]:
<span id="barcode" property="food:code" itemprop="gtin13" style="speak-as:digits;">[% code %]</span>
</p>
<div property="gr:hasEAN_UCC-13" content="[% code %]" datatype="xsd:string"></div>
[% END %]
Expand Down