Skip to content

Instantly share code, notes, and snippets.

@thelebster
Created February 22, 2013 08:01
Show Gist options
  • Select an option

  • Save thelebster/5011643 to your computer and use it in GitHub Desktop.

Select an option

Save thelebster/5011643 to your computer and use it in GitHub Desktop.
<?php
function MYMODULE_generate_barcode128($string, $rebuild = FALSE) {
$filepath = 'barcodes';
$filename = "{$filepath}/" . md5($string) . ".png";
// First check if the images already exists.
if (!file_exists(file_default_scheme()."://{$filename}") || $rebuild) {
$zend_path = libraries_get_path('Zend');
set_include_path(get_include_path() . PATH_SEPARATOR . $zend_path);
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
$barcodeOptions = array(
'text' => $string,
'barHeight' => 50,
'factor' => 2
);
// No required options
$rendererOptions = array(
'imageType' => 'png'
);
// Draw the barcode in a new image
$img = Zend_Barcode::draw('code128', 'image', $barcodeOptions, $rendererOptions);
ob_start();
@imagepng($img);
$file = file_save_data(ob_get_contents(), file_default_scheme()."://{$filename}", TRUE);
ob_end_clean();
@imagedestroy($img);
$file_uri = $file->uri;
}
else {
$file_uri = file_build_uri($filename);
}
$fileurl = file_create_url($file_uri);
return $fileurl;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment