public readImageFromImagick ( Imagick $im ) | ||
$im | Imagick | Image to load from |
public function readImageFromImagick(\Imagick $im)
{
/* Strip transparency */
$im = self::alphaRemove($im);
/* Threshold */
$im->setImageType(\Imagick::IMGTYPE_TRUECOLOR);
// Remove transparency (good for PDF's)
$max = $im->getQuantumRange();
$max = $max["quantumRangeLong"];
$im->thresholdImage(0.5 * $max);
/* Make a string of 1's and 0's */
$imgHeight = $im->getimageheight();
$imgWidth = $im->getimagewidth();
$imgData = str_repeat("", $imgHeight * $imgWidth);
for ($y = 0; $y < $imgHeight; $y++) {
for ($x = 0; $x < $imgWidth; $x++) {
/* Faster to average channels, blend alpha and negate the image here than via filters (tested!) */
$cols = $im->getImagePixelColor($x, $y);
$cols = $cols->getcolor();
$greyness = (int) (($cols['r'] + $cols['g'] + $cols['b']) / 3) >> 7;
// 1 for white, 0 for black
$imgData[$y * $imgWidth + $x] = 1 - $greyness;
// 1 for black, 0 for white
}
}
$this->setImgWidth($imgWidth);
$this->setImgHeight($imgHeight);
$this->setImgData($imgData);
}
public function writeText($text) { if ($this->printer == null) { throw new LogicException("Not attached to a printer."); } if ($text == null) { return; } $text = trim($text, "\n"); /* Create Imagick objects */ $image = new \Imagick(); $draw = new \ImagickDraw(); $color = new \ImagickPixel('#000000'); $background = new \ImagickPixel('white'); /* Create annotation */ if ($this->font !== null) { // Allow fallback on defaults as necessary $draw->setFont($this->font); } /* In Arial, size 21 looks good as a substitute for FONT_B, 24 for FONT_A */ $draw->setFontSize($this->fontSize); $draw->setFillColor($color); $draw->setStrokeAntialias(true); $draw->setTextAntialias(true); $metrics = $image->queryFontMetrics($draw, $text); $draw->annotation(0, $metrics['ascender'], $text); /* Create image & draw annotation on it */ $image->newImage($metrics['textWidth'], $metrics['textHeight'], $background); $image->setImageFormat('png'); $image->drawImage($draw); // debugging if you want to view the images yourself //$image -> writeImage("test.png"); /* Save image */ $escposImage = new ImagickEscposImage(); $escposImage->readImageFromImagick($image); $size = Printer::IMG_DEFAULT; $this->printer->bitImage($escposImage, $size); }