{"id":1480,"date":"2020-09-28T17:27:49","date_gmt":"2020-09-28T16:27:49","guid":{"rendered":"https:\/\/fhumanes.es\/blog\/?page_id=1480"},"modified":"2022-07-24T20:06:44","modified_gmt":"2022-07-24T18:06:44","slug":"guia-4-phprunner-crear-codigo-de-barras","status":"publish","type":"page","link":"https:\/\/fhumanes.es\/blog\/guias-desarrollo\/guia-4-phprunner-crear-codigo-de-barras\/","title":{"rendered":"Gu\u00eda 4 \u2013 Crear C\u00f3digo de Barras"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft wp-image-1284\" src=\"https:\/\/fhumanes.es\/blog\/wp-content\/uploads\/2020\/07\/guia1_008.png\" alt=\"\" width=\"150\" height=\"150\" srcset=\"https:\/\/fhumanes.es\/blog\/wp-content\/uploads\/2020\/07\/guia1_008.png 200w, https:\/\/fhumanes.es\/blog\/wp-content\/uploads\/2020\/07\/guia1_008-150x150.png 150w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/>Con la publicaci\u00f3n de la generaci\u00f3n de QR ha surgido, a alguno de los usuarios de PHPRunner, la necesidad de disponer de c\u00f3digo de barras de 1 dimensi\u00f3n, Code 128, que es el que disponen de los art\u00edculos y que las tiendas y almacenes utilizan para identificar el art\u00edculo.<\/p>\n<h4>Objetivo<\/h4>\n<p>Crear im\u00e1ges de c\u00f3digos de barras de 1D y almacenarlas en la gesti\u00f3n de ficheros que hace PHPrunner, exactamente igual que si hubieramos utilizado el aplicativo para cargarla en el sistema.<\/p>\n<h4>Soluci\u00f3n<\/h4>\n<p>Para la creaci\u00f3n de este tipo de im\u00e1genes he utilizado las librerias PHP de <a href=\"https:\/\/github.com\/picqer\/php-barcode-generator\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/picqer\/php-barcode-generator<\/a>.<\/p>\n<p>DEMO: <a href=\"https:\/\/fhumanes.com\/barcode\/\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/fhumanes.com\/barcode\/<\/a><\/p>\n<p>Es simple y dispone de un conjunto enorme de c\u00f3digos de 1D.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/fhumanes.es\/blog\/wp-content\/uploads\/2020\/09\/barcode_001.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1482\" src=\"https:\/\/fhumanes.es\/blog\/wp-content\/uploads\/2020\/09\/barcode_001-300x106.png\" alt=\"\" width=\"800\" height=\"284\" srcset=\"https:\/\/fhumanes.es\/blog\/wp-content\/uploads\/2020\/09\/barcode_001-300x106.png 300w, https:\/\/fhumanes.es\/blog\/wp-content\/uploads\/2020\/09\/barcode_001-1024x363.png 1024w, https:\/\/fhumanes.es\/blog\/wp-content\/uploads\/2020\/09\/barcode_001-768x272.png 768w, https:\/\/fhumanes.es\/blog\/wp-content\/uploads\/2020\/09\/barcode_001.png 1250w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n<p>En el ejemplo, tanto en el Alta como en la Edici\u00f3n del registro, lo que hace es crear un c\u00f3digo de barras 1D con el contenido del campo \u201cText\u201d y el tipo de gr\u00e1fico elegido.<\/p>\n<p>El c\u00f3digo que hace la imagen y la guarda es:<\/p>\n<div class=\"su-spoiler su-spoiler-style-fancy su-spoiler-icon-plus su-spoiler-closed\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span>generate_barcode.php<\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;?php\r\n\/\/ https:\/\/github.com\/picqer\/php-barcode-generator , source of Code\r\n\r\n\/* \r\nOccurs after record was updated  or added\r\n\r\n$values     - array of values has been written to the database.\r\n              To access specific field value use $values[\"FieldName\"]\r\n              \"dict\" is an alternate name for this parameter.              \r\n$where      - WHERE clause that points to the edited record. Example: ID=19              \r\n$oldvalues  - array with replaced field values. To access specific column value use\r\n              $oldvalues[\"FieldName\"]              \r\n$keys       - array of key column values that point to the edited record. To access\r\n              specific key column use $keys[\"KeyFieldName\"]              \r\n$inline     - equals to true when the Inline Edit in process, false otherwise              \r\n$pageObject - an object of Page class representing the current page  \r\n*\/\r\nglobal $conn;\r\n\/\/  Barcode \"type\" retrieval\r\n$id_type = $values['prueba_barcode_type_idprueba_barcode_type'];\r\n$sql = \"SELECT Type, Code FROM prueba_barcode_type WHERE idprueba_barcode_type = $id_type\";\r\n$rs = $conn-&gt;query($sql);\r\n$data = $rs-&gt;fetch_assoc();\r\n$type = $data['Type'];\r\n$code = $data['Code'];\r\n\r\n\/\/ Load the barcode library classes\r\nrequire_once __DIR__ . '\/php-barcode-generator_2.2.4\/autoload.php'; \r\n\r\n\/\/ Save it to a file\r\n$fileName = 'Barcode_'.$values['idprueba_barcode'].'.png';\r\n$file = substr(__DIR__, 0, -6);  \/\/ root of file\r\n$file = $file.'files\/'.$fileName;\r\n\r\n$Color = [0, 0, 0]; \/\/ Black\r\n\r\n\r\n$generator = new Picqer\\Barcode\\BarcodeGeneratorPNG();\r\n\r\nfile_put_contents($file, $generator-&gt;getBarcode($values['Text'],$type, 2, 25, $Color));\r\n\r\n\/*\r\nThe getBarcode() method accepts the following parameters:\r\n\r\n    $barcode  - String needed to encode in the barcode\r\n    $type  - Type of barcode, use the constants defined in the class\r\n    $widthFactor  - Width is based on the length of the data, with this factor you can make the barcode bars wider than default\r\n    $height  - The total height of the barcode in pixels\r\n    $foregroundColor -  Hex code as string, or array of RGB, of the colors of the bars (the foreground color)\r\n*\/\r\n\r\n\/\/ Save the new file in Database\r\n\r\n$size = filesize($file);\r\n$fileArray = [];\r\n$fileArray[0]['name']= 'files\\\/'.$fileName;\r\n$fileArray[0]['usrName']= $fileName;\r\n$fileArray[0]['size']= $size;\r\n$fileArray[0]['type']= 'image\\\/png';\r\n$fileArray[0]['searchStr'] = $fileName.',!:sStrEnd'; \r\n$fileFileSystem=my_json_encode($fileArray);\r\n            \r\n\/\/ Update in DB the file\r\n$id_key = $values['idprueba_barcode']; \r\n$sql=\"Update prueba_barcode set File = '$fileFileSystem' where idprueba_barcode = $id_key\";\r\n$res=db_exec($sql,$conn);<\/pre>\n<\/div><\/div>\n<p>Como siempre, cualquier duda me pod\u00e9is contactar en <a href=\"mailto:fernandohumanes@gmail.com\">fernandohumanes@gmail.com<\/a><\/p>\n<p>Os dejo los ficheros que necesitar\u00e9is, si lo quer\u00e9is reproducir en vuestros equipos.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Con la publicaci\u00f3n de la generaci\u00f3n de QR ha surgido, a alguno de los usuarios de PHPRunner, la necesidad de disponer de c\u00f3digo de barras de 1 dimensi\u00f3n, Code 128, que es el que disponen de los art\u00edculos y que las tiendas y almacenes utilizan para identificar el art\u00edculo. Objetivo Crear im\u00e1ges de c\u00f3digos de &hellip; <a href=\"https:\/\/fhumanes.es\/blog\/guias-desarrollo\/guia-4-phprunner-crear-codigo-de-barras\/\" class=\"more-link\">Seguir leyendo <span class=\"screen-reader-text\">Gu\u00eda 4 \u2013 Crear C\u00f3digo de Barras<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1254,"menu_order":14,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1480","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/pages\/1480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/comments?post=1480"}],"version-history":[{"count":6,"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/pages\/1480\/revisions"}],"predecessor-version":[{"id":2637,"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/pages\/1480\/revisions\/2637"}],"up":[{"embeddable":true,"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/pages\/1254"}],"wp:attachment":[{"href":"https:\/\/fhumanes.es\/blog\/wp-json\/wp\/v2\/media?parent=1480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}