diff --git a/sites/all/modules/barcode/README.txt b/sites/all/modules/barcode/README.txt new file mode 100755 index 0000000..2581cb1 --- /dev/null +++ b/sites/all/modules/barcode/README.txt @@ -0,0 +1,36 @@ +******************************************************************************* + +barcode +barcodefield + +Description: +------------------------------------------------------------------------------- +This module provides a barcode FAPI element and barcode field type. + + +Installation & Use: +------------------------------------------------------------------------------- +1. Enable module in module list located at administer > build > modules. +2. Configuration barcode settings at admin/config/barcode + + +Features: +------------------------------------------------------------------------------- +* Generate a barcode image on the fly. Supports EAN-13, EAN-8, UPC-A, UPC-E, +* ISBN, 2 of 5 Symbologies (std, ind, interleaved), postnet, codabar, code128, +* code39, code93 symbologies. + + +Authors: +------------------------------------------------------------------------------- +Harish Chauhan - create image from barcode with php +http://phpclasses.ca/browse/author/182909.html + +Jingsheng Wang - Drupal module +http://drupal.org/user/228712 + + +References: +------------------------------------------------------------------------------- +Symbology http://www.barcodeisland.com/symbolgy.phtml + diff --git a/sites/all/modules/barcode/barcode-support_other_entities-1434178-3.patch b/sites/all/modules/barcode/barcode-support_other_entities-1434178-3.patch new file mode 100644 index 0000000..2639467 --- /dev/null +++ b/sites/all/modules/barcode/barcode-support_other_entities-1434178-3.patch @@ -0,0 +1,46 @@ +diff --git a/barcode.info b/barcode.info +index d484e6e..a16de75 100755 +--- a/barcode.info ++++ b/barcode.info +@@ -3,6 +3,9 @@ description = Implements a barcode form element. Generate a barcode image on the + core = 7.x + package = Fields + ++dependencies[] = entity ++dependencies[] = entity_token ++ + files[] = barcode.module + files[] = barcode.install + files[] = includes/barcode.plugins.inc +diff --git a/barcode.module b/barcode.module +index 04f2558..20a6b07 100755 +--- a/barcode.module ++++ b/barcode.module +@@ -352,7 +352,7 @@ function barcode_field_formatter_view($object_type, $object, $field, $instance, + function barcode_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) { + foreach ($items as $entity_id => $entity_items) { + foreach ($entity_items as $delta => $value) { +- _barcode_sanitize($items[$entity_id][$delta], $delta, $field, $instances[$entity_id], $entities[$entity_id]); ++ _barcode_sanitize($items[$entity_id][$delta], $delta, $field, $instances[$entity_id], $entities[$entity_id], $entity_type); + } + } + } +@@ -414,13 +414,14 @@ function barcode_field_widget_form(&$form, &$form_state, $field, $instance, $lan + * change token to appropriate values. + * + */ +-function _barcode_sanitize(&$item, $delta, &$field, $instance, &$node) { ++function _barcode_sanitize(&$item, $delta, &$field, $instance, $entity, $entity_type) { + if (empty($item['value'])) { + return; + } +- +- $token_node = isset($node->nid) ? node_load($node->nid) : $node; +- $item['value'] = token_replace($item['value'], array('node' => $token_node), array('clear' => 1)); ++ if (is_numeric($entity)) { ++ $entity = entity_load_single($entity_type, $entity); ++ } ++ $item['value'] = token_replace($item['value'], array($entity_type => $entity), array('clear' => 1)); + } + + /************************************************************************** diff --git a/sites/all/modules/barcode/barcode.info b/sites/all/modules/barcode/barcode.info new file mode 100755 index 0000000..a16de75 --- /dev/null +++ b/sites/all/modules/barcode/barcode.info @@ -0,0 +1,27 @@ +name = Barcode +description = Implements a barcode form element. Generate a barcode image on the fly. Supports many barcode symbologies. +core = 7.x +package = Fields + +dependencies[] = entity +dependencies[] = entity_token + +files[] = barcode.module +files[] = barcode.install +files[] = includes/barcode.plugins.inc +files[] = includes/barcode.admin.inc +files[] = includes/barcode.theme.inc + +; Supported barcodes +files[] = plugins/i25.inc +files[] = plugins/s2o5.inc +files[] = plugins/ean8.inc +files[] = plugins/ean.inc +files[] = plugins/codabar.inc +files[] = plugins/code93.inc +files[] = plugins/code39.inc +files[] = plugins/code128.inc +files[] = plugins/postnet.inc +files[] = plugins/upce.inc + +configure = admin/config/media/barcode diff --git a/sites/all/modules/barcode/barcode.install b/sites/all/modules/barcode/barcode.install new file mode 100755 index 0000000..1f8d50e --- /dev/null +++ b/sites/all/modules/barcode/barcode.install @@ -0,0 +1,109 @@ + array('type' => 'varchar', 'length' => $field['settings']['dbsize'], 'not null' => FALSE), + ); + } + else { + $columns = array( + 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE), + ); + } + + return array('columns' => $columns); +} + +/** + * Implements hook_install(). + */ +function barcode_install() { + // Create the default barcode directory and ensure it's writable. + $path = file_build_uri('barcodes'); + if (file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { + variable_set('barcode_default_path', $path); + } +} + +/** + * Implements hook_requirements(). + */ +function barcode_requirements($phase) { + $requirements = array(); + if ($phase != 'runtime') { + return $requirements; + } + + // Create the styles directory and ensure it's writable. + $settings = barcode_get_settings(); + $created = file_prepare_directory($settings['default_path'], FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); + + if (!$created) { + $requirements['barcode_dirs'] = array( + 'value' => t('Missing directory'), + 'severity' => REQUIREMENT_ERROR, + 'description' => t("The barcode module's default directory %barcode-dir can not be created.", array('%barcode-dir' => $settings['default_path'])), + ); + } + else { + $requirements['barcode_dirs'] = array( + 'value' => t('Barcode directory exists (%path).', array('%path' => $settings['default_path'])), + 'severity' => REQUIREMENT_OK, + ); + } + $requirements['barcode_dirs']['title'] = t('Barcode module directories'); + + // Image library check. + if (!function_exists("imagecreate")) { + $requirements['barcode_image_library'] = array( + 'value' => t('Missing image library'), + 'severity' => REQUIREMENT_ERROR, + 'description' => t('The barcode module needs GD library support.'), + ); + } + else { + $requirements['barcode_image_library'] = array( + 'value' => t('Image library exists.'), + 'severity' => REQUIREMENT_OK, + ); + } + $requirements['barcode_image_library']['title'] = t('Barcode image library'); + + // FreeType Support check. + if (!function_exists("imagettftext")) { + $requirements['barcode_freetype_gd'] = array( + 'value' => t('Missing FreeType extension for GD'), + 'severity' => REQUIREMENT_ERROR, + 'description' => t('The barcode module needs FreeType extension of GD library to be enabled.'), + ); + } + else { + $requirements['barcode_freetype_gd'] = array( + 'value' => t('FreeType extnesion of GD library exists.'), + 'severity' => REQUIREMENT_OK, + ); + } + $requirements['barcode_freetype_gd']['title'] = t('FreeType extension'); + + + return $requirements; +} diff --git a/sites/all/modules/barcode/barcode.module b/sites/all/modules/barcode/barcode.module new file mode 100755 index 0000000..ec1ade5 --- /dev/null +++ b/sites/all/modules/barcode/barcode.module @@ -0,0 +1,446 @@ +'. t('A module that provides a new FAPI element to handle barcodes.') .'
'; + return $output; + } +} + +/** + * Implements hook_menu(). + */ +function barcode_menu() { + $items['admin/config/media/barcode'] = array( + 'title' => 'Barcode', + 'description' => 'Configure barcode settings.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('barcode_settings'), + 'access arguments' => array('administer barcodes'), + 'file' => 'includes/barcode.admin.inc', + ); + return $items; +} + +/** + * Implements hook_permission(). + */ +function barcode_permission() { + return array( + 'administer barcodes' => array( + 'title' => t('Administer barcode module settings'), + 'description' => t('Perform administration tasks for the barcode module.'), + ), + ); +} + +/** + * Implements hook_theme(). + */ +function barcode_theme() { + return array( + 'barcode_formatter_plain' => array( + 'variables' => array('barcode_value' => NULL), + ), + 'barcode_image' => array( + 'variables' => array( + 'barcode_value' => NULL, + 'encoding' => NULL, + 'height' => NULL, + 'scale' => NULL, + 'bgcolor' => NULL, + 'barcolor' => NULL, + 'image_format' => NULL, + ), + ), + ); +} + +/** + * Theme function for 'barcode_plain' barcode field formatter. + */ +function theme_barcode_formatter_plain($variables) { + return $variables['barcode_value']; +} + +/** + * Theme function for the barcode image. + * + * @ingroup themeable + */ +function theme_barcode_image($variables) { + if (empty($variables['barcode_value'])) { + return ''; + } + + $barcode = barcode_get_settings(); + + if (isset($variables['encoding'])) { + $barcode['encoding'] = $variables['encoding']; + } + + module_load_include('inc', 'barcode', 'includes/barcode.plugins'); + $filename = barcode_generate_image($barcode, $variables); + if (!$filename) { + drupal_set_message(t('An error occured while generating the barcode.'), 'error'); + return ''; + } + + return theme('image', array('path' => $filename, 'alt' => $variables['barcode_value'], 'title' => $variables['barcode_value'], 'attributes' => array('class' => array('barcode')))); +} + +/** + * Validate an individual barcode element. + */ +function barcode_element_validate($element, &$form_state) { + $value = trim($element['#value']," \0\t\x0B"); + form_set_value($element, $value, $form_state); + + $length = strlen($value); + + // Check length. @TODO we need to set up plugins in a more modular way + // so they can handle their own validation. + if ($value != '') { + switch ($element['#encoding']) { + case 'EAN-8': + if ($length != $element['#maxlength'] || !is_numeric($value)) { + form_error($element, t('EAN-8 barcode must have 8 digits')); + } + case 'EAN-13': + if ($length != $element['#maxlength'] || !is_numeric($value)) { + form_error($element, t('EAN-13 barcode must have 13 digits')); + } + case 'ISBN': + if ($length != $element['#maxlength']) { + form_error($element, t('The ISBN barcode must have %count digits.', array('%count' => $element['#maxlength']))); + } + break; + case 'POSTNET': + if ($length != 5 and $length != 9 and $length != 11) { + form_error($element, t('Postnet number must have 5, 9 or 11 digits.')); + } + break; + case 'UPC-A': + if ($length != 12) { + form_error($element, t('UPC-A code must have 12 digits.')); + } + break; + case 'ISBN': + if (substr($value, 0, 3) != '978') { + form_error($element, t('ISBN barcode number must start with 978.')); + } + break; + } + } + return $element; +} + +/*************************************************************** + * Field Type API hooks + ***************************************************************/ + +/** + * Implements hook_field_info(). + */ +function barcode_field_info() { + return array( + 'barcode_field' => array( + 'label' => t('Barcode'), + 'description' => t('This field stores a barcode in the database.'), + 'default_widget' => 'barcode_textfield', + 'enable_tokens' => 1, + 'default_formatter' => 'barcode_default', + 'property_type' => 'text', // added to enable Rules + ), + ); +} + +/** + * Implements hook_field_settings_form(). + */ +function barcode_field_settings_form($field, $instance, $has_data) { + $form = array(); + $option = array( + 'UPC-A' => t('UPC-A'), + 'EAN-13' => t('EAN-13'), + 'ISBN' => t('ISBN'), + 'UPC-E' => t('UPC-E'), + 'EAN-8' => t('EAN-8'), + 'S2O5' => t('Standard 2 of 5'), + 'I2O5' => t('Industrial 2 of 5'), + 'I25' => t('Interleaved 2 of 5'), + 'POSTNET' => t('Postnet'), + 'CODABAR' => t('Codabar'), + 'CODE128' => t('Code 128'), + 'CODE39' => t('Code 39'), + 'CODE93' => t('Code 93'), + 'QRCODE' => t('QR Code') + ); + $form['encoding'] = array( + '#type' => 'select', + '#title' => t('Code Type'), + '#options' => $option, + '#default_value' => isset($field['settings']['encoding']) ? $field['settings']['encoding'] : $option['EAN-13'], + '#description' => t('choose the coding method.'), + '#disabled' => $has_data, + ); + $form['dbsize'] = array( + '#type' => 'textfield', + '#title' => t('Maximum length'), + '#description' => t('The maximum length of the field in characters. Please change it according to the coding standard you have chosen to optimize your database space'), + '#default_value' => isset($field['settings']['dbsize']) ? $field['settings']['dbsize'] : 255, + '#size' => 4, + '#required' => TRUE, + '#disabled' => $has_data, + ); + return $form; +} + +/** + * Implements hook_field_instance_settings_form(). + */ +function barcode_field_instance_settings_form($field, $instance) { + $barcode_default_settings = barcode_get_settings(); + if ($field['settings']['encoding'] == 'QRCODE') { + $form['barcode_height'] = array( + '#title' => t('Height'), + '#description' => t('Integer! in order to scan the printed barcode, the suggested height is 200'), + '#type' => 'textfield', + '#default_value' => isset($instance['settings']['barcode_height']) ? $instance['settings']['barcode_height'] : 200, + '#size' => 2, + '#required' => TRUE, + ); + } + else { + $form['barcode_height'] = array( + '#title' => t('Height'), + '#description' => t('Integer! in order to scan the printed barcode, the suggested height is 40'), + '#type' => 'textfield', + '#default_value' => isset($instance['settings']['barcode_height']) ? $instance['settings']['barcode_height'] : $barcode_default_settings['height'], + '#size' => 2, + '#required' => TRUE, + ); + $form['barcode_scale'] = array( + '#title' => t('Scale'), + '#description' => t('Float! in order to scan the printed barcode, the suggested height is 2.0'), + '#type' => 'textfield', + '#default_value' => isset($instance['settings']['barcode_scale']) ? $instance['settings']['barcode_scale'] : $barcode_default_settings['scale'], + '#size' => 2, + '#required' => TRUE, + ); + $form['barcode_bgcolor'] = array( + '#title' => t('Background color'), + '#description' => t('Hex value'), + '#type' => 'textfield', + '#default_value' => isset($instance['settings']['barcode_bgcolor']) ? $instance['settings']['barcode_bgcolor'] : $barcode_default_settings['bgcolor'], + '#size' => 8, + '#required' => TRUE, + ); + $form['barcode_barcolor'] = array( + '#title' => t('Bar color'), + '#description' => t('Hex value'), + '#type' => 'textfield', + '#default_value' => isset($instance['settings']['barcode_barcolor']) ? $instance['settings']['barcode_barcolor'] : $barcode_default_settings['barcolor'], + '#size' => 8, + '#required' => TRUE, + ); + } + $form['barcode_image_format'] = array( + '#title' => t('Image format'), + '#description' => t('The image format used for generated barcodes. Supported formats: png, gif, jpg.'), + '#type' => 'select', + '#default_value' => isset($instance['settings']['barcode_image_format']) ? $instance['settings']['barcode_image_format'] : $barcode_default_settings['image_format'], + '#options' => array('png' => 'png', 'jpg' => 'jpg', 'gif' => 'gif'), + '#maxlength' => 4, + '#required' => TRUE, + ); + + // Add token module replacements fields + if (module_exists('token')) { + $form['tokens'] = array( + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#title' => t('Placeholder tokens'), + '#description' => t("The following placeholder tokens can be used in DEFAULT VALUE of the field. When used in DEFAULT VALUE, they will be replaced with the appropriate values."), + ); + $token_type = array( + 'theme' => 'token_tree', + 'token_types' => array($instance['entity_type']), + 'global_types' => TRUE, + 'click_insert' => TRUE, + 'recursion_limit' => 2, + ); + $form['tokens']['help'] = array( + '#type' => 'markup', + '#markup' => theme('token_tree', $token_type), + ); + } + + return $form; +} + +/** + * Implementation of hook_content_migrate_field_alter(). + */ +function barcode_content_migrate_field_alter(&$field_value) { + // When fields are migrated from D6 to D7, we leave the title column behind + // This column is a relic from the link module, and doesn't really belong in + // barcode + unset($field_value['columns']['title']); +} + + +/** + * Implements hook_field_is_empty(). + */ +function barcode_field_is_empty($item, $field) { + return empty($item['value']); +} + +/*********************************************************************** + * Field Type API: Formatter + **********************************************************************/ + +/** + * Implements hook_field_formatter_info(). + */ +function barcode_field_formatter_info() { + return array( + 'barcode_default' => array( + 'label' => t('Barcode Image'), + 'field types' => array('barcode_field'), + ), + 'barcode_plain' => array( + 'label' => t('Text'), + 'field types' => array('barcode_field'), + ), + ); +} + +/** + * Implements hook_field_formatter_view(). + */ +function barcode_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) { + $element = array(); + + if ($display['type'] == 'barcode_plain') { + foreach ($items as $delta => $item) { + $element[$delta] = array('#markup' => theme('barcode_formatter_plain', array('barcode_value' => check_plain($items[$delta]['value'])))); + } + } + else { + foreach ($items as $delta => $item) { + $temp = array('barcode_value' => check_plain($items[$delta]['value']),'encoding' => $field['settings']['encoding'], 'height' => $instance['settings']['barcode_height'], 'image_format' => $instance['settings']['barcode_image_format']); + if ($field['settings']['encoding'] != 'QRCODE') { + $temp += array('bgcolor' => $instance['settings']['barcode_bgcolor'], 'barcolor' => $instance['settings']['barcode_barcolor'], 'scale' => $instance['settings']['barcode_scale']); + } + $element[$delta] = array('#markup' => theme('barcode_image', $temp)); + } + } + + return $element; +} + +/** + * Implements hook_field_prepare_view(). + */ +function barcode_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) { + foreach ($items as $entity_id => $entity_items) { + foreach ($entity_items as $delta => $value) { + _barcode_sanitize($items[$entity_id][$delta], $delta, $field, $instances[$entity_id], $entities[$entity_id], $entity_type); + } + } +} + +/************************************************************************** + * Field Type API: Widget + **************************************************************************/ + +/** +* Implements hook_field_widget_info(). +*/ +function barcode_field_widget_info() { + return array( + 'barcode_textfield' => array( + 'label' => t('Text field'), + 'field types' => array('barcode_field'), + 'multiple values' => FIELD_BEHAVIOR_DEFAULT, + ), + 'barcode_textarea' => array( + 'label' => t('Text area'), + 'field types' => array('barcode_field'), + ), + ); +} + +/** + * Implements hook_field_widget_form(). + */ +function barcode_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { + $encoding = isset($field['settings']['encoding']) ? $field['settings']['encoding'] : ''; + module_load_include('inc', 'barcode', 'includes/barcode.plugins'); + $maxlength = barcode_plugin_max_length($encoding); + $element['value'] = $element + array( + '#encoding' => $encoding, + '#maxlength' => $maxlength, + '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '', + '#element_validate' => array('barcode_element_validate'), + ); + switch ($instance['widget']['type']) { + case 'barcode_textfield': + $element['value'] += array( + '#type' => 'textfield', + '#size' => 20, + ); + break; + + case 'barcode_textarea': + $element['value'] += array( + '#type' => 'textarea', + '#rows' => 6, + ); + break; + } + + return $element; +} + +/** + * change token to appropriate values. + * + */ +function _barcode_sanitize(&$item, $delta, &$field, $instance, $entity, $entity_type) { + if (empty($item['value'])) { + return; + } + if (is_numeric($entity)) { + $entity = entity_load_single($entity_type, $entity); + } + $item['value'] = token_replace($item['value'], array($entity_type => $entity), array('clear' => 1)); +} + +/************************************************************************** + * Barcode Helper Functions + **************************************************************************/ + +/** + * Returns the barcode settings. + */ +function barcode_get_settings() { + return array( + 'default_path' => variable_get('barcode_default_path', 'public://barcodes'), + 'font' => variable_get('barcode_font', drupal_get_path('module', 'barcode') ."/fonts/DroidSans.ttf"), + 'encoding' => variable_get('barcode_encoding', 'EAN-13'), + 'height' => 40, + 'scale' => 2.0, + 'bgcolor' => '#FFFFFF', + 'barcolor' => '#000000', + 'image_format' => 'png', + ); +} diff --git a/sites/all/modules/barcode/fonts/DroidSans.ttf b/sites/all/modules/barcode/fonts/DroidSans.ttf new file mode 100644 index 0000000..efd1f8b Binary files /dev/null and b/sites/all/modules/barcode/fonts/DroidSans.ttf differ diff --git a/sites/all/modules/barcode/includes/barcode.admin.inc b/sites/all/modules/barcode/includes/barcode.admin.inc new file mode 100644 index 0000000..aed44e1 --- /dev/null +++ b/sites/all/modules/barcode/includes/barcode.admin.inc @@ -0,0 +1,40 @@ + t('Default path'), + '#description' => t('A file system URI where the barcode images will be stored. Changing this location will cause that barcodes will be generated again upon view.'), + '#type' => 'textfield', + '#default_value' => $barcode['default_path'], + '#size' => 60, + '#required' => TRUE, + ); + $form['barcode_font'] = array( + '#title' => t('Font file'), + '#description' => t("The font used in barcode, must be relative path to Drupal's base."), + '#type' => 'textfield', + '#default_value' => $barcode['font'], + '#size' => 100, + '#required' => TRUE, + ); + return system_settings_form($form); +} + +function barcode_settings_validate($form, &$form_state) { + + $created = file_prepare_directory($form_state['values']['barcode_default_path'], FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); + + if (!$created) { + form_set_error('barcode_default_path', t('Path could not be created or made writeable.')); + } +} diff --git a/sites/all/modules/barcode/includes/barcode.plugins.inc b/sites/all/modules/barcode/includes/barcode.plugins.inc new file mode 100644 index 0000000..4e6ca35 --- /dev/null +++ b/sites/all/modules/barcode/includes/barcode.plugins.inc @@ -0,0 +1,133 @@ + t('UPC-A'), + 'EAN-13' => t('EAN-13'), + 'ISBN' => t('ISBN') + ); + $plugins['ean8'] = array('EAN-8' => t('EAN-8')); + $plugins['upce'] = array('UPC-E' => t('UPC-E')); + $plugins['s2o5'] = array( + 'S2O5' => t('Standard 2 of 5'), + 'I2O5' => t('Industrial 2 of 5') + ); + $plugins['i25'] = array('I25' => t('Interleaved 2 of 5')); + $plugins['postnet'] = array('POSTNET' => t('Postnet')); + $plugins['codabar'] = array('CODABAR' => t('Codabar')); + $plugins['code128'] = array('CODE128' => t('Code 128')); + $plugins['code39'] = array('CODE39' => t('Code 39')); + $plugins['code93'] = array('CODE93' => t('Code 93')); + $plugins['qrcode'] = array('QRCODE' => t('QR Code')); + return $plugins; +} + +/** + * Return the plugin filename containing the given encoding. + */ +function barcode_load_plugin($encoding_name) { + $plugins = barcode_discover_plugins(); + foreach ($plugins as $plugin => $encodings) { + foreach ($encodings as $encoding => $display_string) { + if ($encoding == $encoding_name) { + module_load_include('inc', 'barcode', 'plugins/' . $plugin); + return $plugin; + } + } + } + return ''; +} + +function barcode_plugin_generate_image($plugin, $barcode, $settings) { + $gen_function = 'barcode_'. $plugin .'_barcode'; + $gen_function($barcode, $settings); +} + +function barcode_plugin_max_length($encoding_name) { + $plugin = barcode_load_plugin($encoding_name); + $max_length_function = 'barcode_'. $plugin .'_max_length'; + + if (function_exists($max_length_function)) { + return $max_length_function($encoding_name); + } +} + +/** + * Creates or return the plugin filename containing the given encoding. + */ +function barcode_generate_image($barcode, $variables) { + $name = md5($variables['barcode_value']); + $filename_noformat = $barcode['default_path'] .'/'. $name . $variables['encoding']; + $filename = $filename_noformat .'.'. $variables['image_format']; + + // First check if the images already exists. + if (!file_exists($filename)) { + $plugin = barcode_load_plugin($variables['encoding']); + + $settings = new stdClass(); + $settings->default_path = drupal_realpath($barcode['default_path']); + $settings->encode = $variables['encoding']; + $settings->height = $variables['height']; + $settings->font = $barcode['font']; + $settings->format = $variables['image_format']; + // GD library fails on stream wrappers - get the realpath. + $settings->filename_no_format = drupal_realpath($filename_noformat); + $settings->n2w = 2; + + if ($variables['encoding'] != 'QRCODE') { + $settings->color = array(hexdec(substr($variables['barcolor'], 1, 2)), hexdec(substr($variables['barcolor'], 3, 2)), hexdec(substr($variables['barcolor'], 5, 2))); + $settings->bgcolor = array(hexdec(substr($variables['bgcolor'], 1, 2)), hexdec(substr($variables['bgcolor'], 3, 2)), hexdec(substr($variables['bgcolor'], 5, 2))); + $settings->scale = $variables['scale']; + } + barcode_plugin_generate_image($plugin, $variables['barcode_value'], $settings); + } + + if (!file_exists($filename)) { + watchdog('barcode', 'Failed to generate image using settings @settings', array('@settings' => print_r($settings, TRUE))); + return FALSE; + } + + return $filename; +} + +function barcode_check_digit($barnumber, $number) { + $csum_total = 0; // The checksum working variable starts at zero + + // If the source message string is less than 12 characters long, we make it + // 12 characters + if (strlen($barnumber) < $number) { + $barnumber = str_pad($barnumber, $number, "0", STR_PAD_LEFT); + } + + // Calculate the checksum value for the message + for ($i = 0; $i < strlen($barnumber); $i++) { + if ($i % 2 == 0) { + $csum_total = $csum_total + (3 * intval($barnumber{$i})); + } + else { + $csum_total = $csum_total + intval($barnumber{$i}); + } + } + + // Calculate the checksum digit + if ($csum_total % 10 == 0) { + $checksum_digit = ''; + } + else { + $checksum_digit = 10 - ($csum_total % 10); + } + + return $barnumber . $checksum_digit; +} diff --git a/sites/all/modules/barcode/modules/barcode_example/barcode_example.info b/sites/all/modules/barcode/modules/barcode_example/barcode_example.info new file mode 100644 index 0000000..ea4749e --- /dev/null +++ b/sites/all/modules/barcode/modules/barcode_example/barcode_example.info @@ -0,0 +1,5 @@ +name = Barcode example +description = Barcode FAPI element example. +core = 7.x +dependencies[] = barcode +files[] = barcode_example.module diff --git a/sites/all/modules/barcode/modules/barcode_example/barcode_example.module b/sites/all/modules/barcode/modules/barcode_example/barcode_example.module new file mode 100644 index 0000000..27e488f --- /dev/null +++ b/sites/all/modules/barcode/modules/barcode_example/barcode_example.module @@ -0,0 +1,45 @@ + 'Barcode example', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('barcode_example_example'), + 'access arguments' => array('access content'), + ); + return $items; +} + +function barcode_example_example($form, &$form_state) { + $encoding = variable_get('barcode_encoding', 'EAN-13'); + + if (isset($form_state['barcode'])) { + $form['barcode_image'] = array( + '#markup' => theme('barcode_image', array('barcode_value' => $form_state['barcode'], 'encoding' => $encoding)), + ); + } + + $settings = url('admin/config/media/barcode', array( + 'query' => array('destination' => 'barcode_example'), + )); + + $form['barcode'] = array( + '#type' => 'barcode', + '#title' => t('Barcode'), + '#description' => t('Enter a number to generate a barcode image using the %encoding encoding. You can change which encoding is used in the Barcode settings page.', array('%encoding' => $encoding, '@settings' => $settings)), + '#encoding' => $encoding, + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Show the barcode'), + ); + + return $form; +} + +function barcode_example_example_submit($form, &$form_state) { + $form_state['barcode'] = $form_state['values']['barcode']; + $form_state['rebuild'] = TRUE; +} + diff --git a/sites/all/modules/barcode/plugins/codabar.inc b/sites/all/modules/barcode/plugins/codabar.inc new file mode 100644 index 0000000..b199e6d --- /dev/null +++ b/sites/all/modules/barcode/plugins/codabar.inc @@ -0,0 +1,144 @@ +filename_no_format)) { + header("Content-type: image/". $settings->format); + } + + $scale = $settings->scale; + if ($scale < 1) { + $scale = 2; + } + + $total_y = (double)$scale * $settings->height; + $space = array( + 'top' => 2 * $scale, + 'bottom' => 2 * $scale, + 'left' => 2 * $scale, + 'right' => 2 * $scale + ); + + /* count total width */ + $xpos = 0; + + $xpos = $scale * strlen($bars); + + /* allocate the image */ + $total_x = $xpos + $space['left'] + $space['right']; + $xpos = $space['left']; + + $height = floor($total_y - ($scale * 10)); + $height2 = floor($total_y - $space['bottom']); + + $im = @imagecreatetruecolor($total_x, $total_y); + $bg_color = @imagecolorallocate($im, $settings->bgcolor[0], $settings->bgcolor[1], $settings->bgcolor[2]); + @imagefilledrectangle($im, 0, 0, $total_x, $total_y, $bg_color); + $bar_color = @imagecolorallocate($im, $settings->color[0], $settings->color[1], $settings->color[2]); + + for ($i = 0; $i < strlen($bars); $i++) { + $h = $height; + $val = strtoupper($bars[$i]); + + if ($val == 1) { + @imagefilledrectangle($im, $xpos, $space['top'], $xpos+$scale-1, $h, $bar_color); + } + + $xpos += $scale; + } + + + $x = ($total_x - strlen($bars)) / 2; + @imagettftext($im, $scale * 6, 0, $x, $height2, $bar_color, $settings->font, $barnumber); + + if ($settings->format == "png") { + if (!empty($settings->filename_no_format)) { + @imagepng($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagepng($im); + } + } + + if ($settings->format == "gif") { + if (!empty($settings->filename_no_format)) { + @imagegif($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagegif($im); + } + } + + if ($settings->format == "jpg" || $settings->format == "jpeg" ) { + if (!empty($settings->filename_no_format)) { + @imagejpeg($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagejpeg($im); + } + } + + @imagedestroy($im); +} + +/* A Code 11 Barcode has the following structure: + * + * One of four possible start characters (A, B, C, or D), encoded from the + * table below. + * + * A narrow, inter-character space. + * + * The data of the message, encoded from the table below, with a narrow + * inter-character space between each character. + * + * One of four possible stop characters (A, B, C, or D), encoded from the + * table below + */ +function barcode_codabar_encode($barnumber, $settings) { + $enc_table=array("0000011", "0000110", "0001001", "1100000", "0010010", "1000010", "0100001", "0100100", "0110000", "1001000"); + $chr_table=array("-" => "0001100", "$" => "0011000", ":" => "1000101", "/" => "1010001", "." => "1010100", "+" => "0011111", "A" => "0011010", "B" => "0001011", "C" => "0101001", "D" => "0001110"); + + $mfc_str = ""; + + $widebar = str_pad("", $settings->n2w, "1", STR_PAD_LEFT); + $widespc = str_pad("", $settings->n2w, "0", STR_PAD_LEFT); + + for ($i=0; $i < strlen($barnumber); $i++) { + if (preg_match("/[0-9]+/", $barnumber[$i])) { + $tmp = $enc_table[(int)$barnumber[$i]]; + } + else { + $tmp = $chr_table[strtoupper(trim($barnumber[$i]))]; + } + + $bar = TRUE; + + for ($j = 0; $j < strlen($tmp); $j++) { + if ($tmp[$j] == '0' && $bar) { + $mfc_str .= '1'; + } + elseif ($tmp[$j] == '0' && !$bar) { + $mfc_str .= '0'; + } + elseif ($tmp[$j] == '1' && $bar) { + $mfc_str .= $widebar; + } + elseif ($tmp[$j] == '1' && !$bar) { + $mfc_str .= $widespc; + } + + $bar = !$bar; + } + $mfc_str .= '0'; + } + + return $mfc_str; +} diff --git a/sites/all/modules/barcode/plugins/code128.inc b/sites/all/modules/barcode/plugins/code128.inc new file mode 100644 index 0000000..44ffc6c --- /dev/null +++ b/sites/all/modules/barcode/plugins/code128.inc @@ -0,0 +1,173 @@ +filename_no_format)) { + header("Content-type: image/". $settings->format); + } + + $scale = $settings->scale; + if ($scale < 1) { + $scale = 2; + } + + $total_y = (double)$scale * $settings->height + 10 * $scale; + + $space = array( + 'top' => 2 * $scale, + 'bottom' => 2 * $scale, + 'left' => 2 * $scale, + 'right' => 2 * $scale, + ); + + /* count total width */ + $xpos = 0; + + $xpos = $scale * strlen($bars) + 2 * $scale * 10; + + /* allocate the image */ + $total_x = $xpos + $space['left'] + $space['right']; + $xpos = $space['left'] + $scale * 10; + + $height = floor($total_y - ($scale * 20)); + $height2 = floor($total_y - $space['bottom']); + + $im = @imagecreatetruecolor($total_x, $total_y); + $bg_color = @imagecolorallocate($im, $settings->bgcolor[0], $settings->bgcolor[1], $settings->bgcolor[2]); + @imagefilledrectangle($im, 0, 0, $total_x, $total_y, $bg_color); + $bar_color = @imagecolorallocate($im, $settings->color[0], $settings->color[1], $settings->color[2]); + + for ($i=0; $i < strlen($bars); $i++) { + $h = $height; + $val = strtoupper($bars[$i]); + + if ($val == 1) { + @imagefilledrectangle($im, $xpos, $space['top'], $xpos + $scale - 1, $h, $bar_color); + } + + $xpos += $scale; + } + + $font_arr = @imagettfbbox($scale * 10, 0, $settings->font, $barnumber); + $x = floor($total_x - (int)$font_arr[0] - (int)$font_arr[2] + $scale * 10) / 2; + @imagettftext($im, $scale * 10, 0, $x, $height2, $bar_color, $settings->font, $barnumber); + + if ($settings->format == "png") { + if (!empty($settings->filename_no_format)) { + @imagepng($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagepng($im); + } + } + + if ($settings->format == "gif") { + if (!empty($settings->filename_no_format)) { + @imagegif($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagegif($im); + } + } + + if ($settings->format == "jpg" || $settings->format == "jpeg") { + if (!empty($settings->filename_no_format)) { + @imagejpeg($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagejpeg($im); + } + } + + @imagedestroy($im); +} + +function barcode_code128_encode($barnumber, $settings, $use_keys) { + $enc_table=array("11011001100", "11001101100", "11001100110", "10010011000", "10010001100", "10001001100", "10011001000", "10011000100", "10001100100", "11001001000", "11001000100", "11000100100", "10110011100", "10011011100", "10011001110", "10111001100", "10011101100", "10011100110", "11001110010", "11001011100", "11001001110", "11011100100", "11001110100", "11101101110", "11101001100", "11100101100", "11100100110", "11101100100", "11100110100", "11100110010", "11011011000", "11011000110", "11000110110", "10100011000", "10001011000", "10001000110", "10110001000", "10001101000", "10001100010", "11010001000", "11000101000", "11000100010", "10110111000", "10110001110", "10001101110", "10111011000", "10111000110", "10001110110", "11101110110", "11010001110", "11000101110", "11011101000", "11011100010", "11011101110", "11101011000", "11101000110", "11100010110", "11101101000", "11101100010", "11100011010", "11101111010", "11001000010", "11110001010", "10100110000", "10100001100", "10010110000", "10010000110", "10000101100", "10000100110", "10110010000", "10110000100", "10011010000", "10011000010", "10000110100", "10000110010", "11000010010", "11001010000", "11110111010", "11000010100", "10001111010", "10100111100", "10010111100", "10010011110", "10111100100", "10011110100", "10011110010", "11110100100", "11110010100", "11110010010", "11011011110", "11011110110", "11110110110", "10101111000", "10100011110", "10001011110", "10111101000", "10111100010", "11110101000", "11110100010", "10111011110", "10111101110", "11101011110", "11110101110", "11010000100", "11010010000", "11010011100", "11000111010"); + + $start = array( + "A" => "11010000100", + "B" => "11010010000", + "C" => "11010011100", + ); + $stop = "11000111010"; + + $sum = 0; + $mfc_str = ""; + if ($use_keys == 'C') { + for ($i = 0; $i < strlen($barnumber); $i += 2) { + $val = substr($barnumber, $i, 2); + if (is_int($val)) { + $sum += ($i + 1) * (int)($val); + } + elseif ($barnumber == chr(129)) { + $sum += ($i+1) * 100; + } + elseif ($barnumber == chr(130)) { + $sum += ($i + 1) * 101; + } + + $mfc_str .= isset($enc_table[$val]) ? $enc_table[$val] : ''; + } + } + else { + for ($i = 0; $i < strlen($barnumber); $i++) { + $num = ord($barnumber[$i]); + if ($num >= 32 && $num <= 126) { + $num = ord($barnumber[$i]) - 32; + } + elseif ($num == 128) { + $num = 99; + } + elseif ($num == 129) { + $num = 100; + } + elseif ($num == 130) { + $num = 101; + } + elseif ($num < 32 && $use_keys == 'A') { + $num = $num + 64; + } + + $sum += ($i + 1) * $num; + $mfc_str .= isset($enc_table[$num]) ? $enc_table[$num] : ''; + } + } + + if ($use_keys == 'A') { + $check = ($sum + 103) % 103; + } + + if ($use_keys == 'B') { + $check = ($sum + 104) % 103; + } + + if ($use_keys == 'C') { + $check = ($sum + 105) % 103; + } + + return $start[$use_keys] . $mfc_str . $enc_table[$check] . $stop ."11"; +} diff --git a/sites/all/modules/barcode/plugins/code39.inc b/sites/all/modules/barcode/plugins/code39.inc new file mode 100644 index 0000000..e82ee42 --- /dev/null +++ b/sites/all/modules/barcode/plugins/code39.inc @@ -0,0 +1,219 @@ +filename_no_format)) { + header("Content-type: image/". $settings->format); + } + + $scale = $settings->scale; + if ($scale < 1) { + $scale = 2; + } + + $total_y = (double)$scale * $settings->height + 10 * $scale; + + $space = array( + 'top' => 2 * $scale, + 'bottom' => 2 * $scale, + 'left' => 2 * $scale, + 'right' => 2 * $scale, + ); + + /* count total width */ + $xpos = 0; + + $xpos = $scale * strlen($bars) + 2 * $scale * 10; + + /* allocate the image */ + $total_x = $xpos + $space['left'] + $space['right']; + $xpos = $space['left'] + $scale * 10; + + $height = floor($total_y - ($scale * 20)); + $height2 = floor($total_y - $space['bottom']); + + $im = @imagecreatetruecolor($total_x, $total_y); + $bg_color = @imagecolorallocate($im, $settings->bgcolor[0], $settings->bgcolor[1], $settings->bgcolor[2]); + @imagefilledrectangle($im, 0, 0, $total_x, $total_y, $bg_color); + $bar_color = @imagecolorallocate($im, $settings->color[0], $settings->color[1], $settings->color[2]); + + for ($i = 0; $i < strlen($bars); $i++) { + $h = $height; + $val = $bars[$i]; + + if ($val == 1) { + @imagefilledrectangle($im, $xpos, $space['top'], $xpos + $scale - 1, $h, $bar_color); + } + + $xpos += $scale; + } + + $font_arr = @imagettfbbox ($scale*10, 0, $settings->font, $barnumber); + $x = floor($total_x - (int)$font_arr[0] - (int)$font_arr[2] + $scale * 10) / 2; + @imagettftext($im, $scale * 10, 0, $x, $height2, $bar_color, $settings->font, $barnumber); + + if ($settings->format == "png") { + if (!empty($settings->filename_no_format)) { + @imagepng($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagepng($im); + } + } + + if ($settings->format == "gif") { + if (!empty($settings->filename_no_format)) { + @imagegif($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagegif($im); + } + } + + if ($settings->format == "jpg" || $settings->format == "jpeg") { + if (!empty($settings->filename_no_format)) { + @imagejpeg($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagejpeg($im); + } + } + + @imagedestroy($im); +} + +/* A Code 39 barcode has the following structure: + * A start character - the asterisk (*) character. Any number of characters + * encoded from the table below. An optional checksum digit calculated as + * described above and encoded from the table below. A stop character, which + * is a second asterisk character. + */ +function barcode_code39_encode($barnumber, $settings, $checkdigit = FALSE) { + $enc_table = array( + "0" => "NNNWWNWNN", + "1" => "WNNWNNNNW", + "2" => "NNWWNNNNW", + "3" => "WNWWNNNNN", + "4" => "NNNWWNNNW", + "5" => "WNNWWNNNN", + "6" => "NNWWWNNNN", + "7" => "NNNWNNWNW", + "8" => "WNNWNNWNN", + "9" => "NNWWNNWNN", + "A" => "WNNNNWNNW", + "B" => "NNWNNWNNW", + "C" => "WNWNNWNNN", + "D" => "NNNNWWNNW", + "E" => "WNNNWWNNN", + "F" => "NNWNWWNNN", + "G" => "NNNNNWWNW", + "H" => "WNNNNWWNN", + "I" => "NNWNNWWNN", + "J" => "NNNNWWWNN", + "K" => "WNNNNNNWW", + "L" => "NNWNNNNWW", + "M" => "WNWNNNNWN", + "N" => "NNNNWNNWW", + "O" => "WNNNWNNWN", + "P" => "NNWNWNNWN", + "Q" => "NNNNNNWWW", + "R" => "WNNNNNWWN", + "S" => "NNWNNNWWN", + "T" => "NNNNWNWWN", + "U" => "WWNNNNNNW", + "V" => "NWWNNNNNW", + "W" => "WWWNNNNNN", + "X" => "NWNNWNNNW", + "Y" => "WWNNWNNNN", + "Z" => "NWWNWNNNN", + "-" => "NWNNNNWNW", + "." => "WWNNNNWNN", + " " => "NWWNNNWNN", + "$" => "NWNWNWNNN", + "/" => "NWNWNNNWN", + "+" => "NWNNNWNWN", + "%" => "NNNWNWNWN", + "*" => "NWNNWNWNN", + ); + + $mfc_str = ""; + $widebar = str_pad("", $settings->n2w, "1", STR_PAD_LEFT); + $widespc = str_pad("", $settings->n2w, "0", STR_PAD_LEFT); + + if ($checkdigit == TRUE) { + $arr_key = array_keys($enc_table); + $sum = 0; + for ($i = 0; $i < strlen($barnumber); $i++) { + $num = $barnumber[$i]; + if (preg_match("/[A-Z]+/", $num)) { + $num = ord($num)-55; + } + elseif ($num == '-') { + $num = 36; + } + elseif ($num == '.') { + $num = 37; + } + elseif ($num == ' ') { + $num=38; + } + elseif ($num == '$') { + $num = 39; + } + elseif ($num == '/') { + $num = 40; + } + elseif ($num == '+') { + $num = 41; + } + elseif ($num == '%') { + $num = 42; + } + elseif ($num == '*') { + $num = 43; + } + + $sum += $num; + } + $barnumber .= trim($arr_key[(int)($sum % 43)]); + } + + $barnumber = "*". $barnumber ."*"; + + for ($i = 0; $i < strlen($barnumber); $i++) { + $tmp = $enc_table[$barnumber[$i]]; + + $bar = TRUE; + + for ($j = 0; $j < strlen($tmp); $j++) { + if ($tmp[$j] == 'N' && $bar) { + $mfc_str .= '1'; + } + elseif ($tmp[$j] == 'N' && !$bar) { + $mfc_str .= '0'; + } + elseif ($tmp[$j] == 'W' && $bar) { + $mfc_str .= $widebar; + } + elseif ($tmp[$j] == 'W' && !$bar) { + $mfc_str .= $widespc; + } + + $bar = !$bar; + } + + $mfc_str .= '0'; + } + + return $mfc_str; +} + + diff --git a/sites/all/modules/barcode/plugins/code93.inc b/sites/all/modules/barcode/plugins/code93.inc new file mode 100644 index 0000000..b0f64ae --- /dev/null +++ b/sites/all/modules/barcode/plugins/code93.inc @@ -0,0 +1,200 @@ +filename_no_format)) { + header("Content-type: image/". $settings->format); + } + + $scale = $settings->scale; + if ($scale < 1) { + $scale = 2; + } + + $total_y = (double)$scale * $settings->height + 10 * $scale; + + $space = array( + 'top' => 2 * $scale, + 'bottom' => 2 * $scale, + 'left' => 2 * $scale, + 'right' => 2 * $scale, + ); + + /* count total width */ + $xpos = 0; + + $xpos = $scale * strlen($bars) + 2 * $scale * 10; + + /* allocate the image */ + $total_x = $xpos + $space['left'] + $space['right']; + $xpos = $space['left'] + $scale * 10; + + $height = floor($total_y - ($scale * 20)); + $height2 = floor($total_y - $space['bottom']); + + $im = @imagecreatetruecolor($total_x, $total_y); + $bg_color = @imagecolorallocate($im, $settings->bgcolor[0], $settings->bgcolor[1], $settings->bgcolor[2]); + @imagefilledrectangle($im, 0, 0, $total_x, $total_y, $bg_color); + $bar_color = @imagecolorallocate($im, $settings->color[0], $settings->color[1], $settings->color[2]); + + for ($i=0; $i < strlen($bars); $i++) { + $h = $height; + $val = $bars[$i]; + + if ($val == 1) { + @imagefilledrectangle($im, $xpos, $space['top'], $xpos + $scale - 1, $h, $bar_color); + } + + $xpos += $scale; + } + + $font_arr = @imagettfbbox ($scale*10, 0, $settings->font, $barnumber); + $x = floor($total_x - (int)$font_arr[0] - (int)$font_arr[2] + $scale * 10) / 2; + @imagettftext($im, $scale * 10, 0, $x, $height2, $bar_color, $settings->font, $barnumber); + + if ($settings->format == "png") { + if (!empty($settings->filename_no_format)) { + @imagepng($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagepng($im); + } + } + + if ($settings->format == "gif") { + if (!empty($settings->filename_no_format)) { + @imagegif($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagegif($im); + } + } + + if ($settings->format == "jpg" || $settings->format == "jpeg") { + if (!empty($settings->filename_no_format)) { + @imagejpeg($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagejpeg($im); + } + } + + @imagedestroy($im); +} + +/* A Code 93 barcode has the following structure: + * A start character , represented below by the asterisk (*) character. Any + * number of characters encoded from the table below. The "C" and "K" checksum + * digits calculated as described above and encoded using the table below. A + * stop character, which is a second asterisk character. + */ +function barcode_code93_encode($barnumber, $settings) { + $enc_table = array( + "0" => "100010100", + "1" => "101001000", + "2" => "101000100", + "3" => "101000010", + "4" => "100101000", + "5" => "100100100", + "6" => "100100010", + "7" => "101010000", + "8" => "100010010", + "9" => "100001010", + "A" => "110101000", + "B" => "110100100", + "C" => "110100010", + "D" => "110010100", + "E" => "110010010", + "F" => "110001010", + "G" => "101101000", + "H" => "101100100", + "I" => "101100010", + "J" => "100110100", + "K" => "100011010", + "L" => "101011000", + "M" => "101001100", + "N" => "101000110", + "O" => "100101100", + "P" => "100010110", + "Q" => "110110100", + "R" => "110110010", + "S" => "110101100", + "T" => "110100110", + "U" => "110010110", + "V" => "110011010", + "W" => "101101100", + "X" => "101100110", + "Y" => "100110110", + "Z" => "100111010", + "-" => "100101110", + "." => "111010100", + " " => "111010010", + "$" => "111001010", + "/" => "101101110", + "+" => "101110110", + "%" => "110101110", + "$" => "100100110", + "%" => "111011010", + "/" => "111010110", + "+" => "100110010", + "*" => "101011110", + ); + + $mfc_str = ""; + $widebar = str_pad("", $settings->n2w, "1", STR_PAD_LEFT); + $widespc = str_pad("", $settings->n2w, "0", STR_PAD_LEFT); + + $arr_key = array_keys($enc_table); + /// calculating C And K + + for ($j = 0; $j < 2; $j++) { + $sum = 0; + for ($i = strlen($barnumber); $i > 0; $i--) { + $num = $barnumber[strlen($barnumber) - $i]; + if (preg_match("/[A-Z]+/", $num)) + $num = ord($num) - 55; + elseif ($num == '-') { + $num = 36; + } + elseif ($num == '.') { + $num = 37; + } + elseif ($num == ' ') { + $num = 38; + } + elseif ($num == '$') { + $num = 39; + } + elseif ($num == '/') { + $num = 40; + } + elseif ($num == '+') { + $num = 41; + } + elseif ($num == '%') { + $num = 42; + } + elseif ($num == '*') { + $num = 43; + } + + $sum += $i * $num; + } + $barnumber .= trim($arr_key[(int)($sum % 47)]); + } + + $barnumber = "*". $barnumber ."*"; + + for ($i = 0; $i < strlen($barnumber); $i++) { + $mfc_str .= $enc_table[$barnumber[$i]]; + } + $mfc_str .= '1'; + + return $mfc_str; +} diff --git a/sites/all/modules/barcode/plugins/ean.inc b/sites/all/modules/barcode/plugins/ean.inc new file mode 100644 index 0000000..5927d6d --- /dev/null +++ b/sites/all/modules/barcode/plugins/ean.inc @@ -0,0 +1,232 @@ +filename_no_format)) { + header("Content-type: image/". $settings->format); + } + + $scale = $settings->scale; + if ($scale < 1) { + $scale = 2; + } + + $total_y = (double)$scale * $settings->height; + $space = array( + 'top' => 2 * $scale, + 'bottom' => 2 * $scale, + 'left' => 2 * $scale, + 'right' => 2 * $scale, + ); + + /* count total width */ + $xpos = 0; + + $xpos = $scale * (114); + + /* allocate the image */ + $total_x = $xpos + $space['left'] + $space['right']; + $xpos = $space['left'] + ($scale * 6); + + $height = floor($total_y - ($scale * 10)); + $height2 = floor($total_y - $space['bottom']); + + $im = @imagecreatetruecolor($total_x, $total_y); + $bg_color = @imagecolorallocate($im, $settings->bgcolor[0], $settings->bgcolor[1], $settings->bgcolor[2]); + @imagefilledrectangle($im, 0, 0, $total_x, $total_y, $bg_color); + $bar_color = @imagecolorallocate($im, $settings->color[0], $settings->color[1], $settings->color[2]); + + for ($i = 0; $i < strlen($bars); $i++) { + $h = $height; + $val = strtoupper($bars[$i]); + if (preg_match("/[a-z]/i", $val)) { + $val = ord($val) - 65; + $h = $height2; + } + + if ($settings->encode == "UPC-A" && ($i < 10 || $i>strlen($bars) - 13)) { + $h = $height2; + } + + if ($val == 1) { + @imagefilledrectangle($im, $xpos, $space['top'], $xpos + $scale - 1, $h, $bar_color); + } + + $xpos += $scale; + } + + + if ($settings->encode == "UPC-A") { + $str = substr($barnumber, 1, 1); + } + else { + $str = substr($barnumber, 0, 1); + } + + @imagettftext($im, $scale * 6, 0, $space['left'], $height, $bar_color, $settings->font, $str); + + if ($settings->encode == "UPC-A") { + $str = substr($barnumber, 2, 5); + } + else { + $str = substr($barnumber, 1, 6); + } + + $x = $space['left'] + $scale * strlen($barnumber) + $scale * 6; + @imagettftext($im, $scale * 6, 0, $x, $height2, $bar_color, $settings->font, $str); + + if ($settings->encode == "UPC-A") { + $str = substr($barnumber, 7, 5); + } + else { + $str = substr($barnumber, 7, 6); + } + + $x = $space['left'] + $scale * strlen($bars) / 1.65 + $scale * 6; + @imagettftext($im, $scale * 6, 0, $x, $height2, $bar_color, $settings->font, $str); + + if ($settings->encode == "UPC-A") { + $str = substr($barnumber, 12, 1); + $x = $total_x - $space['left'] - $scale * 6; + @imagettftext($im, $scale * 6, 0, $x, $height, $bar_color, $settings->font, $str); + } + + if ($settings->format == "png") { + if (!empty($settings->filename_no_format)) { + @imagepng($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagepng($im); + } + } + + if ($settings->format == "gif") { + if (!empty($settings->filename_no_format)) { + @imagegif($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagegif($im); + } + } + + if ($settings->format == "jpg" || $settings->format == "jpeg") { + if (!empty($settings->filename_no_format)) { + @imagejpeg($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagejpeg($im); + } + } + + @imagedestroy($im); +} + +/* An EAN-13 barcode has the following physical structure: + * + * Left-hand guard bars, or start sentinel, encoded as 101. + * The second character of the number system code, encoded as described below. + * The five characters of the manufacturer code, encoded as described below. + * Center guard pattern, encoded as 01010. + * The five characters of the product code, encoded as right-hand characters, + * described below. + * Check digit, encoded as a right-hand character, described below. + * Right-hand guard bars, or end sentinel, encoded as 101. + * FIRST NUMBER + * + * SYSTEM DIGIT PARITY TO ENCODE WITH + * SECOND NUMBER + * SYSTEM DIGIT MANUFACTURER CODE CHARACTERS + * 1 2 3 4 5 + * 0 (UPC-A) Odd Odd Odd Odd Odd Odd + * 1 Odd Odd Even Odd Even Even + * 2 Odd Odd Even Even Odd Even + * 3 Odd Odd Even Even Even Odd + * 4 Odd Even Odd Odd Even Even + * 5 Odd Even Even Odd Odd Even + * 6 Odd Even Even Even Odd Odd + * 7 Odd Even Odd Even Odd Even + * 8 Odd Even Odd Even Even Odd + * 9 Odd Even Even Odd Even Odd + */ +function barcode_ean_encode($barnumber) { + $left_odd = array("0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011"); + $left_even = array("0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111"); + $right_all = array("1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100"); + + $enc_table = array("000000", "001011", "001101", "001110", "010011", "011001", "011100", "010101", "010110", "011010"); + + $guards = array("bab", "ababa", "bab"); + + $mfc_str = ""; + $prod_str = ""; + + $encbit = $barnumber[0]; + + for ($i = 1; $i < strlen($barnumber); $i++) { + $num = (int)$barnumber{$i}; + if ($i < 7) { + $even = (substr($enc_table[$encbit], $i - 1, 1) == 1); + if (!$even) { + $mfc_str .= $left_odd[$num]; + } + else { + $mfc_str .= $left_even[$num]; + } + } + elseif ($i >= 7) { + $prod_str .= $right_all[$num]; + } + } + + return $guards[0] . $mfc_str . $guards[1] . $prod_str . $guards[2]; +} diff --git a/sites/all/modules/barcode/plugins/ean8.inc b/sites/all/modules/barcode/plugins/ean8.inc new file mode 100644 index 0000000..235896a --- /dev/null +++ b/sites/all/modules/barcode/plugins/ean8.inc @@ -0,0 +1,138 @@ +filename_no_format)) { + header("Content-type: image/". $settings->format); + } + + $scale = $settings->scale; + if ($scale < 1) { + $scale = 2; + } + + $total_y = (double)$scale * $settings->height; + + $space = array( + 'top' => 2 * $scale, + 'bottom' => 2 * $scale, + 'left' => 2 * $scale, + 'right' => 2 * $scale, + ); + + /* count total width */ + $xpos = 0; + + $xpos = $scale * strlen($bars); + + /* allocate the image */ + $total_x = $xpos + $space['left'] + $space['right']; + $xpos = $space['left']; + + $height = floor($total_y - ($scale * 10)); + $height2 = floor($total_y - $space['bottom']); + + $im =@imagecreatetruecolor($total_x, $total_y); + $bg_color = @imagecolorallocate($im, $settings->bgcolor[0], $settings->bgcolor[1], $settings->bgcolor[2]); + @imagefilledrectangle($im, 0, 0, $total_x, $total_y, $bg_color); + $bar_color = @imagecolorallocate($im, $settings->color[0], $settings->color[1], $settings->color[2]); + + for ($i = 0; $i < strlen($bars); $i++) { + $h = $height; + $val = strtoupper($bars[$i]); + if (preg_match("/[a-z]/i", $val)) { + $val = ord($val) - 65; + $h = $height2; + } + + if ($val == 1) { + @imagefilledrectangle($im, $xpos, $space['top'], $xpos + $scale - 1, $h, $bar_color); + } + + $xpos += $scale; + } + + $str = substr($barnumber, 0, 4); + $x = $space['left'] + $scale * strlen($barnumber); + @imagettftext($im, $scale * 6, 0, $x, $height2, $bar_color, $settings->font, $str); + + $str = substr($barnumber, 4, 4); + $x = $space['left'] + $scale * strlen($bars) / 1.65; + @imagettftext($im, $scale * 6, 0, $x, $height2, $bar_color, $settings->font, $str); + + if ($settings->format == "png") { + if (!empty($settings->filename_no_format)) { + @imagepng($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagepng($im); + } + } + + if ($settings->format == "gif") { + if (!empty($settings->filename_no_format)) { + @imagegif($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagegif($im); + } + } + + if ($settings->format == "jpg" || $settings->format == "jpeg" ) { + if (!empty($settings->filename_no_format)) { + @imagejpeg($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagejpeg($im); + } + } + + @imagedestroy($im); +} + +/* An EAN-8 barcode has the following physical structure: + * + * Left-hand guard bars, or start sentinel, encoded as 101. + * Two number system characters, encoded as left-hand odd-parity characters. + * First two message characters, encoded as left-hand odd-parity characters. + * Center guard bars, encoded as 01010. + * Last three message characters, encoded as right-hand characters. + * Check digit, encoded as right-hand character. + * Right-hand guar bars, or end sentinel, encoded as 101. + */ +function barcode_ean8_encode($barnumber) { + $left_odd = array("0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011"); + $left_even = array("0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111"); + $right_all = array("1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100"); + + $enc_table = array("000000", "001011", "001101", "001110", "010011", "011001", "011100", "010101", "010110", "011010"); + + $guards=array("bab", "ababa", "bab"); + + $mfc_str = ""; + $prod_str = ""; + + for ($i = 0; $i < strlen($barnumber); $i++) { + $num = (int)$barnumber{$i}; + if ($i < 4) { + $mfc_str .= $left_odd[$num]; + } + elseif ($i >= 4) { + $prod_str .= $right_all[$num]; + } + } + + return $guards[0] . $mfc_str . $guards[1] . $prod_str . $guards[2]; +} diff --git a/sites/all/modules/barcode/plugins/i25.inc b/sites/all/modules/barcode/plugins/i25.inc new file mode 100644 index 0000000..4edb1cd --- /dev/null +++ b/sites/all/modules/barcode/plugins/i25.inc @@ -0,0 +1,149 @@ +filename_no_format)) { + header("Content-type: image/". $settings->format); + } + + $scale = $settings->scale; + if ($scale < 1) { + $scale = 2; + } + + $total_y = (double)$scale * $settings->height; + + $space = array( + 'top' => 2 * $scale, + 'bottom' => 2 * $scale, + 'left' => 2 * $scale, + 'right' => 2 * $scale + ); + + /* count total width */ + $xpos = 0; + + $xpos = $scale * strlen($bars); + + /* allocate the image */ + $total_x = $xpos + $space['left'] + $space['right']; + $xpos = $space['left']; + + $height = floor($total_y - ($scale * 10)); + $height2 = floor($total_y - $space['bottom']); + + $im = @imagecreatetruecolor($total_x, $total_y); + $bg_color = @imagecolorallocate($im, $settings->bgcolor[0], $settings->bgcolor[1], $settings->bgcolor[2]); + @imagefilledrectangle($im, 0, 0, $total_x, $total_y, $bg_color); + $bar_color = @imagecolorallocate($im, $settings->color[0], $settings->color[1], $settings->color[2]); + + for ($i = 0; $i < strlen($bars); $i++) { + $h = $height; + $val = strtoupper($bars[$i]); + + if ($val == 1) { + @imagefilledrectangle($im, $xpos, $space['top'], $xpos+$scale - 1, $h, $bar_color); + } + + $xpos+=$scale; + } + + $x = ($total_x - strlen($bars)) / 2; + @imagettftext($im, $scale * 6, 0, $x, $height2, $bar_color, $settings->font, $barnumber); + + if ($settings->format == "png") { + if (!empty($settings->filename_no_format)) { + @imagepng($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagepng($im); + } + } + + if ($settings->format=="gif") { + if (!empty($settings->filename_no_format)) { + @imagegif($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagegif($im); + } + } + + if ($settings->format == "jpg" || $settings->format == "jpeg" ) { + if (!empty($settings->filename_no_format)) { + @imagejpeg($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagejpeg($im); + } + } + + @imagedestroy($im); +} + +/* A Standard 2 of 5 barcode has the following physical structure: + * + * Start character, encoded as 11011010. + * Data characters properly encoded (see encoding table below). + * Stop character, encoded as 11010110. + * + * ASCII BARCODE + * 0 NNWWN + * 1 WNNNW + * 2 NWNNW + * 3 WWNNN + * 4 NNWNW + * 5 WNWNN + * 6 NWWNN + * 7 NNNWW + * 8 WNNWN + * 9 NWNWN + */ +function barcode_i25_encode($barnumber, $settings) { + $enc_table=array("NNWWN", "WNNNW", "NWNNW", "WWNNN", "NNWNW", "WNWNN", "NWWNN", "NNNWW", "WNNWN", "NWNWN"); + $guards=array("1010", "1101"); + + $len=strlen($barnumber); + if ($len % 2 != 0) { + $barnumber = barcode_check_digit($barnumber, $len); + if ($len == strlen($barnumber) && substr($barnumber, -1) != '0') { + $barnumber .= '0'; + } + } + + $mfc_str = ""; + + $widebar = str_pad("", $settings->n2w, "1", STR_PAD_LEFT); + $widespc = str_pad("", $settings->n2w, "0", STR_PAD_LEFT); + + for ($i = 0; $i < strlen($barnumber); $i += 2) { + $tmp = $enc_table[(int)$barnumber[$i]]; + $tmp1 = $enc_table[(int)$barnumber[$i+1]]; + for ($j = 0; $j < strlen($tmp); $j++) { + if ($tmp[$j]=='N') { + $mfc_str .= '1'; + } + else { + $mfc_str .= $widebar; + } + + if ($tmp1[$j]=='N') { + $mfc_str .= '0'; + } + else { + $mfc_str .= $widespc; + } + } + } + + return $guards[0] . $mfc_str . $guards[1]; +} + diff --git a/sites/all/modules/barcode/plugins/postnet.inc b/sites/all/modules/barcode/plugins/postnet.inc new file mode 100644 index 0000000..08b2674 --- /dev/null +++ b/sites/all/modules/barcode/plugins/postnet.inc @@ -0,0 +1,126 @@ +filename_no_format)) { + header("Content-type: image/". $settings->format); + } + + $scale = $settings->scale; + if ($scale < 1) { + $scale = 2; + } + + $total_y = (double)$scale * $settings->height; + $space = array( + 'top' => 2 * $scale, + 'bottom' => 2 * $scale, + 'left' => 2 * $scale, + 'right' => 2 * $scale + ); + + /* count total width */ + $xpos = 0; + + $xpos = $scale * strlen($bars) * 2; + + /* allocate the image */ + $total_x = $xpos + $space['left'] + $space['right']; + $xpos = $space['left']; + + $height = floor($total_y - ($scale * 10)); + $height2 = floor($total_y - $space['bottom']); + + $im = @imagecreatetruecolor($total_x, $total_y); + $bg_color = @imagecolorallocate($im, $settings->bgcolor[0], $settings->bgcolor[1], $settings->bgcolor[2]); + @imagefilledrectangle($im, 0, 0, $total_x, $total_y, $bg_color); + $bar_color = @imagecolorallocate($im, $settings->color[0], $settings->color[1], $settings->color[2]); + + for ($i = 0; $i < strlen($bars); $i++) { + $val = strtoupper($bars[$i]); + $h = $total_y - $space['bottom']; + + if ($val == 1) { + @imagefilledrectangle($im, $xpos, $space['top'], $xpos + $scale - 1, $height2, $bar_color); + } + else { + @imagefilledrectangle($im, $xpos, floor($height2 / 1.5), $xpos + $scale - 1, $height2, $bar_color); + } + + $xpos += 2 * $scale; + } + + if ($settings->format == "png") { + if (!empty($settings->filename_no_format)) { + @imagepng($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagepng($im); + } + } + + if ($settings->format == "gif") { + if (!empty($settings->filename_no_format)) { + @imagegif($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagegif($im); + } + } + + if ($settings->format == "jpg" || $settings->format == "jpeg" ) { + if (!empty($settings->filename_no_format)) { + @imagejpeg($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagejpeg($im); + } + } + + @imagedestroy($im); +} + +/* + * A PostNet barcode has the following structure: + * + * Frame bar, encoded as a single 1. + * 5, 9, or 11 data characters properly encoded (see encoding table below). + * Check digit, encoded using encoding table below. + * Final frame bar, encoded as a single 1. + * + * 0 11000 + * 1 00011 + * 2 00101 + * 3 00110 + * 4 01001 + * 5 01010 + * 6 01100 + * 7 10001 + * 8 10010 + * 9 10100 + */ +function barcode_postnet_encode($barnumber, $settings) { + $enc_table = array("11000", "00011", "00101", "00110", "01001", "01010", "01100", "10001", "10010", "10100"); + + $sum = 0; + $encstr = ""; + for ($i = 0; $i < strlen($barnumber); $i++) { + $sum += (int)$barnumber[$i]; + $encstr .= $enc_table[(int)$barnumber[$i]]; + } + + if ($sum % 10 != 0) { + $check = (int)(10 - ($sum % 10)); + } + + $encstr .= $enc_table[$check]; + $encstr = "1" . $encstr . "1"; + return $encstr; +} diff --git a/sites/all/modules/barcode/plugins/qrcode.inc b/sites/all/modules/barcode/plugins/qrcode.inc new file mode 100755 index 0000000..61de8f0 --- /dev/null +++ b/sites/all/modules/barcode/plugins/qrcode.inc @@ -0,0 +1,40 @@ +filename_no_format)) { + header("Content-type: image/". $settings->format); + } + + $h = $settings->height; + $name = md5($barnumber); + $content = urlencode($barnumber); + $url = 'http://chart.apis.google.com/chart?chs=' . $h . 'x' . $h . '&cht=qr&chl=' . $content; + $img = $settings->default_path .'/'. $name . $settings->encode .'.'. $settings->format; + + // file_put_contents($img, file_get_contents($url)); + + /**Added by H. **/ + $headers = array( + "Expect:", + // more headers here + ); + $curl_handle=curl_init(); + curl_setopt($curl_handle, CURLOPT_URL, $url); + curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); + curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Drupal'); + curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers); + $query = curl_exec($curl_handle); + curl_close($curl_handle); + + file_put_contents($img, $query); +} + +function barcode_qrcode_max_length($encoding) { + return 8192; +} diff --git a/sites/all/modules/barcode/plugins/s2o5.inc b/sites/all/modules/barcode/plugins/s2o5.inc new file mode 100644 index 0000000..7683751 --- /dev/null +++ b/sites/all/modules/barcode/plugins/s2o5.inc @@ -0,0 +1,133 @@ +filename_no_format)) { + header("Content-type: image/". $settings->format); + } + + $scale = $settings->scale; + if ($scale < 1) { + $scale = 2; + } + + $total_y = (double)$scale * $settings->height; + $space = array( + 'top' => 2 * $scale, + 'bottom' => 2 * $scale, + 'left' => 2 * $scale, + 'right' => 2 * $scale + ); + + /* count total width */ + $xpos = $scale * strlen($bars); + + /* allocate the image */ + $total_x = $xpos + $space['left'] + $space['right']; + $xpos = $space['left']; + + $height = floor($total_y - ($scale * 10)); + $height2 = floor($total_y - $space['bottom']); + + $im = @imagecreatetruecolor($total_x, $total_y); + $bg_color = @imagecolorallocate($im, $settings->bgcolor[0], $settings->bgcolor[1], $settings->bgcolor[2]); + @imagefilledrectangle($im, 0, 0, $total_x, $total_y, $bg_color); + $bar_color = @imagecolorallocate($im, $settings->color[0], $settings->color[1], $settings->color[2]); + + for ($i = 0; $i < strlen($bars); $i++) { + $h = $height; + $val = strtoupper($bars[$i]); + + if ($val == 1) { + @imagefilledrectangle($im, $xpos, $space['top'], $xpos + $scale - 1, $h, $bar_color); + } + + $xpos += $scale; + } + + $x = ($total_x - strlen($bars)) / 2; + @imagettftext($im, $scale * 6, 0, $x, $height2, $bar_color, $settings->font, $barnumber); + + if ($settings->format == "png") { + if (!empty($settings->filename_no_format)) { + @imagepng($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagepng($im); + } + } + + if ($settings->format == "gif") { + if (!empty($settings->filename_no_format)) { + @imagegif($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagegif($im); + } + } + + if ($settings->format == "jpg" || $settings->format == "jpeg" ) { + if (!empty($settings->filename_no_format)) { + @imagejpeg($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagejpeg($im); + } + } + + @imagedestroy($im); +} + +/* A Standard 2 of 5 barcode has the following physical structure: + * + * Start character, encoded as 11011010. + * Data characters properly encoded (see encoding table below). + * Stop character, encoded as 11010110. + * + * ASCII BARCODE + * 0 NNWWN + * 1 WNNNW + * 2 NWNNW + * 3 WWNNN + * 4 NNWNW + * 5 WNWNN + * 6 NWWNN + * 7 NNNWW + * 8 WNNWN + * 9 NWNWN + */ +function barcode_s2o5_encode($barnumber, $settings) { + $enc_table = array("NNWWN", "WNNNW", "NWNNW", "WWNNN", "NNWNW", "WNWNN", "NWWNN", "NNNWW", "WNNWN", "NWNWN"); + $guards = array("11011010", "1101011"); + + $len = strlen($barnumber); + $barnumber = barcode_check_digit($barnumber, $len); + if ($len == strlen($barnumber) && substr($barnumber, -1) != '0') { + $barnumber .= '0'; + } + + $mfc_str = ""; + + $widebar = str_pad("", $settings->n2w, "1", STR_PAD_LEFT); + $widebar .= "0"; + + for ($i = 0; $i < strlen($barnumber); $i++) { + $num = (int)$barnumber{$i}; + $str = ""; + $str = str_replace("N", "10", $enc_table[$num]); + $str = str_replace("W", $widebar, $str); + $mfc_str .= $str; + } + + return $guards[0] . $mfc_str . $guards[1]; +} + + diff --git a/sites/all/modules/barcode/plugins/upce.inc b/sites/all/modules/barcode/plugins/upce.inc new file mode 100644 index 0000000..dca7a33 --- /dev/null +++ b/sites/all/modules/barcode/plugins/upce.inc @@ -0,0 +1,190 @@ +6) { + barcode_load_plugin('ean'); + barcode_ean13_check_digit($barnumber); + $barnumber = substr($this->barcode_ean13_check_digit($barnumber), 1); + $encbit = $barnumber[0]; + $checkdigit = $barnumber[11]; + $barnumber = barcode_convert_upca_to_upce($barnumber, $settings); + } + else { + $barnumber = barcode_check_digit($barnumber, 7); + $encbit = $barnumber[0]; + $checkdigit = $barnumber[7]; + $barnumber = substr($barnumber, 1, 6); + } + + $bars = barcode_upce_encode($barnumber, $settings, $encbit, $checkdigit); + + if (empty($settings->filename_no_format)) { + header("Content-type: image/". $settings->format); + } + + $scale = $settings->scale; + if ($scale < 1) { + $scale = 2; + } + + $total_y = (double)$scale * $settings->height; + $space = array( + 'top' => 2 * $scale, + 'bottom' => 2 * $scale, + 'left' => 2 * $scale, + 'right' => 2 * $scale + ); + + /* count total width */ + $xpos=$scale*strlen($bars)+$scale*12; + + /* allocate the image */ + $total_x = $xpos + $space['left'] + $space['right']; + $xpos = $space['left'] + ($scale * 6); + + $height = floor($total_y - ($scale * 10)); + $height2 = floor($total_y - $space['bottom']); + + $im = @imagecreatetruecolor($total_x, $total_y); + $bg_color = @imagecolorallocate($im, $settings->bgcolor[0], $settings->bgcolor[1], $settings->bgcolor[2]); + @imagefilledrectangle($im, 0, 0, $total_x, $total_y, $bg_color); + $bar_color = @imagecolorallocate($im, $settings->color[0], $settings->color[1], $settings->color[2]); + + for ($i=0; $i < strlen($bars); $i++) { + $h = $height; + $val = strtoupper($bars[$i]); + if (preg_match("/[a-z]/i", $val)) { + $val = ord($val) - 65; + $h = $height2; + } + + if ($val == 1) { + @imagefilledrectangle($im, $xpos, $space['top'], $xpos + $scale - 1, $h, $bar_color); + } + + $xpos += $scale; + } + + @imagettftext($im, $scale * 6, 0, $space['left'], $height, $bar_color, $settings->font, $encbit); + + $x = $space['left'] + $scale * strlen($barnumber) + $scale * 6; + @imagettftext($im, $scale * 6, 0, $x, $height2, $bar_color, $settings->font, $barnumber); + + $x = $total_x - $space['left'] - $scale * 6; + @imagettftext($im, $scale * 6, 0, $x, $height, $bar_color, $settings->font, $checkdigit); + + if ($settings->format == "png") { + if (!empty($settings->filename_no_format)) { + @imagepng($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagepng($im); + } + } + + if ($settings->format=="gif") { + if (!empty($settings->filename_no_format)) { + @imagegif($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagegif($im); + } + } + + if ($settings->format == "jpg" || $settings->format == "jpeg") { + if (!empty($settings->filename_no_format)) { + @imagejpeg($im, $settings->filename_no_format .".". $settings->format); + } + else { + @imagejpeg($im); + } + } + + @imagedestroy($im); +} + +function barcode_upce_encode($barnumber, $settings, $encbit, $checkdigit) { + $left_odd = array("0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011"); + $left_even=array("0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111"); + + $enc_table0=array("EEEOOO", "EEOEOO", "EEOOEO", "EEOOOE", "EOEEOO", "EOOEEO", "EOOOEE", "EOEOEO", "EOEOOE", "EOOEOE"); + $enc_table1=array("OOOEEE", "OOEOEE", "OOEEOE", "OOEEEO", "OEOOEE", "OEEOOE", "OEEEOO", "OEOEOE", "OEOEEO", "OEEOEO"); + + $guards=array("bab", "ababa", "b"); + + if ($encbit == 0) { + $enc_table = $enc_table0; + } + elseif ($encbit == 1) { + $enc_table = $enc_table1; + } + else { + $settings->error = "Not an UPC-E barcode number"; + return FALSE; + } + + $mfc_str = ""; + $prod_str = ""; + $checkdigit; + $enc_table[$checkdigit]; + + for ($i = 0; $i < strlen($barnumber); $i++) { + $num = (int)$barnumber{$i}; + $even = (substr($enc_table[$checkdigit], $i, 1) == 'E'); + if (!$even) { + $mfc_str .= $left_odd[$num]; + } + else { + $mfc_str .= $left_even[$num]; + } + } + + return $guards[0] . $mfc_str . $guards[1] . $guards[2]; +} + +// @todo handle error +function barcode_convert_upca_to_upce($upca, &$settings) { + $upce = ""; + + // If the source message string is less than 12 characters long, we make it + // 12 characters + if (strlen($upca) < 12) { + $upca = str_pad($upca, 12, "0", STR_PAD_LEFT); + } + + if (substr($upca, 0, 1) != '0' && substr($upca, 0, 1) != '1') { + $settings->error = 'Invalid Number System (only 0 & 1 are valid)'; + return FALSE; + } + else { + if (substr($upca, 3, 3) == '000' || substr($upca, 3, 3) == '100' || substr($upca, 3, 3) == '200') { + $upce = substr($upca, 1, 2) . substr($upca, 8, 3) . substr($upca, 3, 1); + } + elseif (substr($upca, 4, 2) == '00') { + $upce = substr($upca, 1, 2) . substr($upca, 9, 2) .'3'; + } + elseif (substr($upca, 5, 1) == '0') { + $upce = substr($upca, 1, 4) . substr($upca, 10, 1) .'4'; + } + elseif (substr($upca, 10, 1) >= '5') { + $upce = substr($upca, 1, 5) . substr($upca, 10, 1); + } + else { + $settings->error = 'Invalid product code (00005 to 00009 are valid)'; + return FALSE; + } + } + + return $upce; +} + diff --git a/sites/all/modules/custom/ccgd_user_barcode/README.txt b/sites/all/modules/custom/ccgd_user_barcode/README.txt new file mode 100644 index 0000000..79b809f --- /dev/null +++ b/sites/all/modules/custom/ccgd_user_barcode/README.txt @@ -0,0 +1,4 @@ +This feature creates barcodes to be used on the user profile page. + +For more detaild information, see [this blog post](http://radarearth.com/content/automatically-generate-barcodes-users-drupal-site) + diff --git a/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.features.field_base.inc b/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.features.field_base.inc new file mode 100644 index 0000000..343d04f --- /dev/null +++ b/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.features.field_base.inc @@ -0,0 +1,33 @@ + 1, + 'cardinality' => 1, + 'deleted' => 0, + 'entity_types' => array(), + 'field_name' => 'field_user_barcodes', + 'foreign keys' => array(), + 'indexes' => array(), + 'locked' => 0, + 'module' => 'barcode', + 'settings' => array( + 'dbsize' => 255, + 'encoding' => 'CODABAR', + ), + 'translatable' => 0, + 'type' => 'barcode_field', + ); + + return $field_bases; +} diff --git a/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.features.field_instance.inc b/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.features.field_instance.inc new file mode 100644 index 0000000..e888ad8 --- /dev/null +++ b/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.features.field_instance.inc @@ -0,0 +1,58 @@ + 'user', + 'default_value' => array( + 0 => array( + 'value' => 'A[user:uid]A', + ), + ), + 'deleted' => 0, + 'description' => '', + 'display' => array( + 'default' => array( + 'label' => 'above', + 'module' => 'barcode', + 'settings' => array(), + 'type' => 'barcode_default', + 'weight' => 0, + ), + ), + 'entity_type' => 'user', + 'field_name' => 'field_user_barcodes', + 'label' => 'User Barcode', + 'required' => 0, + 'settings' => array( + 'barcode_barcolor' => '#000000', + 'barcode_bgcolor' => '#FFFFFF', + 'barcode_height' => 40, + 'barcode_image_format' => 'png', + 'barcode_scale' => 2, + 'user_register_form' => 0, + ), + 'widget' => array( + 'active' => 0, + 'module' => 'barcode', + 'settings' => array(), + 'type' => 'barcode_textarea', + 'weight' => 7, + ), + ); + + // Translatables + // Included for use with string extractors like potx. + t('User Barcode'); + + return $field_instances; +} diff --git a/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.features.inc b/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.features.inc new file mode 100644 index 0000000..91f7144 --- /dev/null +++ b/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.features.inc @@ -0,0 +1,14 @@ + "1"); + } +} diff --git a/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.info b/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.info new file mode 100644 index 0000000..6074d5a --- /dev/null +++ b/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.info @@ -0,0 +1,18 @@ +name = CCGD User barcode +description = Generates barcodes to display on the user profile page based on each user's drupal id. +core = 7.x +package = ccgd +version = 7.x-1.0-alpha1 +project = ccgd_user_barcode +dependencies[] = barcode +dependencies[] = ctools +dependencies[] = entity +dependencies[] = entity_token +dependencies[] = features +dependencies[] = strongarm +dependencies[] = token +features[ctools][] = strongarm:strongarm:1 +features[features_api][] = api:2 +features[field_base][] = field_user_barcodes +features[field_instance][] = user-user-field_user_barcodes +features[variable][] = barcode_default_path diff --git a/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.module b/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.module new file mode 100644 index 0000000..df64096 --- /dev/null +++ b/sites/all/modules/custom/ccgd_user_barcode/ccgd_user_barcode.module @@ -0,0 +1,7 @@ +disabled = FALSE; /* Edit this to true to make a default strongarm disabled initially */ + $strongarm->api_version = 1; + $strongarm->name = 'barcode_default_path'; + $strongarm->value = 'public://barcodes'; + $export['barcode_default_path'] = $strongarm; + + return $export; +}