Exemple #1
0
 /**
  * Print an image to the printer.
  *
  * Size modifiers are:
  * - IMG_DEFAULT (leave image at original size)
  * - IMG_DOUBLE_WIDTH
  * - IMG_DOUBLE_HEIGHT
  *
  * See the example/ folder for detailed examples.
  *
  * The function bitImage() takes the same parameters, and can be used if
  * your printer doesn't support the newer graphics commands.
  *
  * @param EscposImage $img The image to print.
  * @param int $size Output size modifier for the image.
  */
 function graphics(EscposImage $img, $size = self::IMG_DEFAULT)
 {
     self::validateInteger($size, 0, 3, __FUNCTION__);
     $imgHeader = self::dataHeader(array($img->getWidth(), $img->getHeight()), true);
     $tone = '0';
     $colors = '1';
     $xm = ($size & self::IMG_DOUBLE_WIDTH) == self::IMG_DOUBLE_WIDTH ? chr(2) : chr(1);
     $ym = ($size & self::IMG_DOUBLE_HEIGHT) == self::IMG_DOUBLE_HEIGHT ? chr(2) : chr(1);
     $header = $tone . $xm . $ym . $colors . $imgHeader;
     $this->wrapperSendGraphicsData('0', 'p', $header . $img->toRasterFormat());
     $this->wrapperSendGraphicsData('0', '2');
 }
 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 */
     //$draw -> setFont('Arial');// (not necessary?)
     $draw->setFontSize(24);
     // Size 21 looks good for FONT B
     $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);
     //$image -> writeImage("test.png");
     /* Save image */
     $escposImage = new EscposImage();
     $escposImage->readImageFromImagick($image);
     $size = Escpos::IMG_DEFAULT;
     $this->printer->bitImage($escposImage, $size);
 }
Exemple #3
0
 /**
  * Load a PDF for use on the printer
  *
  * @param string $pdfFile The file to load
  * @param string $pageWidth The width, in pixels, of the printer's output. The first page of the PDF will be scaled to approximately fit in this area.
  * @param array $range array indicating the first and last page (starting from 0) to load. If not set, the entire document is loaded.
  * @throws Exception Where Imagick is not loaded, or where a missing file or invalid page number is requested.
  * @return multitype:EscposImage Array of images, retrieved from the PDF file.
  */
 public static function loadPdf($pdfFile, $pageWidth = 550, array $range = null)
 {
     if (!extension_loaded('imagick')) {
         throw new Exception(__FUNCTION__ . " requires imagick extension.");
     }
     /*
      * Load first page at very low density (resolution), to figure out what
      * density to use to achieve $pageWidth
      */
     try {
         $image = new Imagick();
         $testRes = 2;
         // Test resolution
         $image->setresolution($testRes, $testRes);
         $image->readimage($pdfFile . "[0]");
         $geo = $image->getimagegeometry();
         $image->destroy();
         $width = $geo['width'];
         $newRes = $pageWidth / $width * $testRes;
         /* Load actual document (can be very slow!) */
         $rangeStr = "";
         // Set to [0] [0-1] page range if $range is set
         if ($range != null) {
             if (count($range) != 2 || !isset($range[0]) || !is_integer($range[0]) || !isset($range[1]) || !is_integer($range[1]) || $range[0] > $range[1]) {
                 throw new Exception("Invalid range. Must be two numbers in the array: The start and finish page indexes, starting from 0.");
             }
             $rangeStr = "[" . ($range[0] == $range[1] ? $range[0] : implode($range, "-")) . "]";
         }
         $image->setresolution($newRes, $newRes);
         $image->readImage($pdfFile . "{$rangeStr}");
         $pages = $image->getNumberImages();
         /* Convert images to Escpos objects */
         $ret = array();
         for ($i = 0; $i < $pages; $i++) {
             $image->setIteratorIndex($i);
             $ep = new EscposImage();
             $ep->readImageFromImagick($image);
             $ret[] = $ep;
         }
         return $ret;
     } catch (ImagickException $e) {
         // Wrap in normal exception, so that classes which call this do not themselves require imagick as a dependency.
         throw new Exception($e);
     }
 }