Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print a small image in front of text #480

Closed
Julio1961 opened this issue Dec 4, 2017 · 8 comments
Closed

Print a small image in front of text #480

Julio1961 opened this issue Dec 4, 2017 · 8 comments

Comments

@Julio1961
Copy link

Hello!
I'm trying to put a small image PNG of a special character I've created on paint and I want it to put in front of a text...
For example:
$img = EscposImage::load("1.png");
$printer -> text("Welcome")
$printer->graphics($img);

This code it doesn't print the image, only the text, no error

If I put on text "\n" on the text
The image goes under the text obviously

Any idea how to put the image in front of the text? Its a small image, size of the character I'm writing on text

Thank you very much!

@Julio1961
Copy link
Author

Any idea? Help!
Thank you!

@mdestafadilah
Copy link

May you can try this this example.

@mike42
Copy link
Owner

mike42 commented Jan 11, 2018

The ESC/POS function used for bitImageColumnFormat() prints on the same line then adds "\n", so check that you can print Tux with that function first-

$tux = EscposImage::load("resources/tux.png", false);
$printer -> bitImageColumnFormat($tux);
$printer -> text("Regular Tux (bit image, column format).\n");

Then, try this example, which contains a stripped-back version of bitImageColumnFormat() that skips the line breaks.

I used a 24x16 character: char

<?php
require __DIR__ . '/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\EscposImage;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;

class MyCoolPrinter extends Mike42\Escpos\Printer {

    // Print image inline. If it is a multi-line image, then only the first line is printed!
    public function inlineImage(EscposImage $img, $size = Printer::IMG_DEFAULT)
    {
        $highDensityVertical = ! (($size & self::IMG_DOUBLE_HEIGHT) == Printer::IMG_DOUBLE_HEIGHT);
        $highDensityHorizontal = ! (($size & self::IMG_DOUBLE_WIDTH) == Printer::IMG_DOUBLE_WIDTH);
        // Header and density code (0, 1, 32, 33) re-used for every line
        $densityCode = ($highDensityHorizontal ? 1 : 0) + ($highDensityVertical ? 32 : 0);
        $colFormatData = $img -> toColumnFormat($highDensityVertical);
        $header = Printer::dataHeader(array($img -> getWidth()), true);
        foreach ($colFormatData as $line) {
            // Print each line, double density etc for printing are set here also
            $this -> connector -> write(self::ESC . "*" . chr($densityCode) . $header . $line);
            break;
        }
    }
}

/* Fill in your own connector here */
$connector = new FilePrintConnector("php://stdout");
$printer = new MyCoolPrinter($connector);

$char = EscposImage::load("char.png");

$printer -> text("Test test t-");
$printer -> inlineImage($char);
$printer -> text("-test test\n");

$printer -> cut();
$printer -> close();

I'm using an Epson TM-T20, and the result is:

2018-01-11-special-chars

@mdestafadilah
Copy link

only work for size 24x16 .. i try tested in 32x32 .. it half cut image...

@Julio1961
Copy link
Author

Julio1961 commented Jan 29, 2018

Hi It worked at least for one line! Thanks again mike

Now im having the same problem as mdestafadilah, the image gets cutted...
I actually need to print a character image that has the size of font using double HEIGHT and double weight

I've been investigating about it and I saw a post of yours in stackoverflow that says that I could use two images chopped to add them together by simply repeating the command...I make it but it doesnt work, since its in Double height, the first cutted image always go ahead of the double height words...
So the other solution you talked about on the post would be GS v 0...I've tried too use your snippet rasterFormat.php, load the img, but it doesn't work either..It doesnt print, no error, doesnt do anytthing and just echo me this "�v0�3"
I guess its not sending the command $GS = "\x1d"; plus all the other parameters...
I dont know what to do...I try to solve it but its so hard, I know I don't have the skill to figure this one out

Can you make me the magic function to be able to make character image in double height situation?

Thanks again!

P. S - Do you accept donations mike42? I really aprechiate your project, it made all my project easier!

@mike42
Copy link
Owner

mike42 commented Feb 1, 2018

I think we are going to need a custom font for your use case. The driver does not really do this yet, so there is a bit more custom code.

You will really be limited to the size of a character (24x12 in "font A") with this code. I hope this is supported on your printer. On my Epson TM-T20, it looks like this:

2018-02-custom-chars-img

<?php
require __DIR__ . '/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\EscposImage;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;

class MyCoolPrinter extends Mike42\Escpos\Printer {

    public function setUserDefinedCharacter(EscposImage $img, $char)
    {
        $verticalBytes = 3;
        $colFormatData = $img -> toColumnFormat(true);
        foreach ($colFormatData as $line) {
            // Print each line, double density etc for printing are set here also
            $this -> connector -> write(self::ESC . "&" . chr($verticalBytes) . $char . $char . chr($img -> getWidth()) . $line);
            break;
        }
    }

    public function selectUserDefinedCharacterSet($on = true)
    {
        self::validateBoolean($on, __FUNCTION__);
        $this -> connector -> write(self::ESC . "%". ($on ? chr(1) : chr(0)));
    }
}

/* Fill in your own connector here */
$connector = new FilePrintConnector("php://stdout");
$printer = new MyCoolPrinter($connector);

// Replace '*' with a 24x12 image.
$char = EscposImage::load("char2.png");
$printer -> setUserDefinedCharacter($char, "*");

// Print some stuff normally, switching character sets just to print the custom character.
$printer -> text("Test test t-");
$printer -> selectUserDefinedCharacterSet(true);
$printer -> text("*");
$printer -> selectUserDefinedCharacterSet(false);
$printer -> text("-est test\n");

// Leave it in our updated character set for a while..
$printer -> selectUserDefinedCharacterSet(true);

// Go taller!
$printer -> selectPrintMode(Printer::MODE_DOUBLE_HEIGHT);
$printer -> text("Test test t-*-test test\n");

// Emphasis too?
$printer -> selectPrintMode(Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_EMPHASIZED);
$printer -> text("Test test t-*-test test\n");

// Adjust height and width
for ($i = 1; $i <= 8; $i++) {
    $printer -> setTextSize($i, $i);
    $printer -> text("A*");
}
$printer -> text("\n");

$printer -> cut();
$printer -> close();

The 12x24 character is: char2

A note on donations: This project has no ongoing costs, and I have no expectation that any users contribute financially, even they use this code commercially. Having said that, I am would be grateful for donations toward purchasing additional printers for strictly non-commercial use, and can accept donations via PayPal (my email address is listed on my GitHub profile) or BTC: 1FTsaXQqw5CPPv9v1cX4BRKVs46hLeuRQK.

@Julio1961
Copy link
Author

Thank you very much mike42!
That piece of code have really helped me!

@vijayabalansujan
Copy link

vijayabalansujan commented Feb 4, 2020

hi mike42 .i'm using epson tm-t82 printer.i'm trying to print the khmer font.it's not wrking.after that i convert the font to image .then it's work.but hv problem.unable to print the inline image and text. after that i used your inlineimage funtion.but image half cut and print.pls help me
20200204_120348 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants