• Resolved little_kostas

    (@little_kostas)


    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 https://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.]

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Use barcode in plugin’ is closed to new replies.