I am developing a desktop based application using php
and javascript
where I need to print the image of a person in the dithering format. As shown in the question. I have the solution for the image that converts the rgb
image to dithering image. This is the conversion code . I need to convert that dithering image to grf
if I want to print the image through the zebra printers.In the below php
code
$photo_url="";
if(isset($_GET['photo'])){
$photo_url=$_GET['photo'];
}
function image2grf($filename='photo_url', $targetname = 'R:IMAGE.GRF')
{
$info = getimagesize($filename);
$im = imagecreatefrompng($filename);
$width = $info[0]; // imagesx($im);
$height = $info[1]; // imagesy($im);
$depth = $info['bits'] ?: 1;
$threshold = $depth > 1 ? 100 : 0;
$hexString = '';
$byteShift = 7;
$currentByte = 0;
// create a gd indexed color converter
$converter = new GDIndexedColorConverter();
// the color palette
$palette = array(
array(0, 0, 0),
array(255, 255, 255),
array(0, 0, 0),
array(0, 0, 0),
array(0, 0, 0)
);
// convert the image to indexed color mode
$photo_url = $converter->convertToIndexedColor($im, $palette, 0.8);
// iterate over all image pixels
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) {
$color = imagecolorat($im, $x, $y);
// $color = imagetruecolortopalette($photo_url, true, 2);
// compute gray value from RGB color
if ($depth > 1) {
$value = max($color >> 16, $color >> 8 & 255, $color & 255);
} else {
$value = $color;
}
// set (inverse) bit for the current pixel
$currentByte |= (($value > $threshold ? 1 : 0) << $byteShift);
$byteShift--;
// 8 pixels filled one byte => append to output as hex
if ($byteShift < 0) {
$hexString .= sprintf('%02X', $currentByte);
$currentByte = 0;
$byteShift = 7;
}
}
// append last byte at end of row
if ($byteShift < 7) {
$hexString .= sprintf('%02X', $currentByte);
$currentByte = 0;
$byteShift = 7;
}
$hexString .= PHP_EOL;
}
// compose ZPL ~DG command
$totalBytes = ceil(($width * $height) / 8);
$bytesPerRow = ceil($width / 8);
return sprintf('~DG%s,%05d,%03d,' . PHP_EOL, $targetname, $totalBytes, $bytesPerRow) . $hexString;
}
// Usage:
print "^XA N ^XZ" . PHP_EOL;
print image2grf($photo_url, 'R:SAMPLE.GRF');
it converts the captured png
image to grf
and stored in the variable R:SAMPLE.GRF
and the below image is printed using javascript
as specified the previous question(link is specified above).
In the solution it converts the rgb
image to dithering image but I want to save that image in grf
format then only I can print the dithering image through zebra printer.
Help me in resolving this