WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Use barcode in plugin (3 posts)

  1. little_kostas
    Member
    Posted 8 months ago #

    Hello guys,
    i an trying to generate barcodes using the following script: BarcodePHP

    Also, i am creating my plugin using the WordPress Plugin Boilerplate

    Heres my code(inside the public plugin folder):

    class Coupons {
    ...
    private function __construct() {
    ...
    add_shortcode( 'coupons', array ($this, 'coupons_shortcode') );
    ..
    }
    
    public function coupons_shortcode() {
    // Including all required classes
    require_once('/includes/barcode/class/BCGArgumentException.php');
    require_once('/includes/barcode/class/BCGBarcode.php');
    require_once('/includes/barcode/class/BCGBarcode1D.php');
    require_once('/includes/barcode/class/BCGFontFile.php');
    require_once('/includes/barcode/class/BCGFontFile.php');
    require_once('/includes/barcode/class/BCGColor.php');
    require_once('/includes/barcode/class/BCGDrawing.php');
    require_once('/includes/barcode/class/BCGFontPhp.php');
    
    // Including the barcode technology
    require_once('/includes/barcode/class/BCGcode39.barcode.php');
    
    // Loading Font
    $font = new BCGFontFile('./includes/barcode/font/Arial.ttf', 18);
    
    // Don't forget to sanitize user inputs
    $text = isset($_GET['text']) ? $_GET['text'] : 'HELLO';
    
    // The arguments are R, G, B for color.
    $color_black = new BCGColor(0, 0, 0);
    $color_white = new BCGColor(255, 255, 255);
    
    $drawException = null;
    try {
    	$code = new BCGcode39();
    	$code->setScale(2); // Resolution
    	$code->setThickness(30); // Thickness
    	$code->setForegroundColor($color_black); // Color of bars
    	$code->setBackgroundColor($color_white); // Color of spaces
    	$code->setFont(new BCGFontPhp(5)); // Font (or 0)
    	$code->parse($text); // Text
    } catch(Exception $exception) {
    	$drawException = $exception;
    }
    
     /* Here is the list of the arguments
    		// 1 - Filename (empty : display on screen)
    		// 2 - Background color */
    	$drawing = new BCGDrawing('', $color_white);
    	if($drawException) {
    		$drawing->drawException($drawException);
    	} else {
    		$drawing->setBarcode($code);
    		$drawing->draw();
    	}
    
    // Header that says it is an image (remove it if you save the barcode to a file)
    	header('Content-Type: image/png');
    	header('Content-Disposition: inline; filename="barcode.png"');
    
    	// Draw (or save) the image into PNG format.
    	$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
    
    	}
    
    }

    I am using the [coupons] shortcode inside a page, but when i run the code, i do not get the Barcode, insted i get a message that "The image http://localhost/wordpress/?page_id=2 contains errors".

    The following code inside the coupons_shortcode, is the same as the example provided by the barcodephp example.

    I am running this code localy, and i have enabled the gd2 library.

    Has anyone tried this before?
    Can you please help me?

    [Moderator Note: No bumping. If it's that urgent, consider hiring someone.]

  2. makaveiljojo
    Member
    Posted 7 months ago #

    I haven't ever tried something like this before. But you may refer to the
    PHP barcode generator script and PHP barcode generator script tutorial I got on Google. They might be helpful.

  3. little_kostas
    Member
    Posted 6 months ago #

    .

Reply

You must log in to post.

About this Topic