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
Comments
Any idea? Help! |
May you can try this this example. |
The ESC/POS function used for
Then, try this example, which contains a stripped-back version of <?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: |
only work for size 24x16 .. i try tested in 32x32 .. it half cut image... |
Hi It worked at least for one line! Thanks again mike Now im having the same problem as mdestafadilah, the image gets cutted... 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... 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! |
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: <?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(); 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. |
Thank you very much mike42! |
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!
The text was updated successfully, but these errors were encountered: