How can i perform auto cut on Epson TMT82 from PHP File? bellow is my config file.
Config:
$tmpdir = sys_get_temp_dir();
$file = tempnam($tmpdir, 'ctk');
$handle = fopen($file, 'w');
$condensed = Chr(27) . Chr(33) . Chr(4);
$bold1 = Chr(27) . Chr(69);
$bold0 = Chr(27) . Chr(70);
$initialized = chr(27) . chr(64);
$condensed1 = chr(15);
$condensed0 = chr(18);
$Data = $initialized;
$Data .= $condensed1;
Printing:
fwrite($handle, $Data);
fclose($handle);
copy($file, "//localhost/printer"); # printing
unlink($file)
Try to write chr(29) + 'V' (or chr(86)), and then the values for m and n, depending on the exact function you want to use as per the manual below (if you just want to cut without feeding at all, you want to use function A, which means you can just follow with a 0 (or 48, not sure why they mentioned both numbers in the manual...!?), like so:
chr(29) . "V" . 0
https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=87
(you need to be logged on to read this reference, but registering is free)
Edited: the chr() code for 'V' is 86, not 56
Edit2: Just to comment on what Matt Gibson said; chr(27) . chr(105) should also work on your model (just checked, also chr(27) . chr(109)), but it's an obsolete command, you should be using chr(29) . "V". In any case, some printers like to receive these sorts of commands on their own instead of having them along with the rest of the string.
Once you get this right, you should probably define a variable with the type of cut that you want. Ex. $cutPaper = chr(29) . "V" . 0;
Related
I have a folder of images that I am trying to change to be the date and time the picture was taken. I managed to make it work for the most part, however if the image has the same DateTimeOriginal (to the second), the subsequent images are deleted and replaced with the last one. This is a problem when I use burst on my camera.
I am trying to have the code add "_1" after each file name, unless the file name exists, then I want the "_1" to increase by 1. So far, the code I have will catch the first duplicate name and work properly, but every other matching filename after just deletes the file before it that has the same name (which was just renamed by the code).
In case it makes a difference, I am using XAMPP to run the PHP code in a local directory on my windows 10 PC, but I do test it online as well and have the same outcome.
The following is the code I have come up with by piecing together other code that I have found, and then attempting to customize it. I have a general understanding of PHP through trial and error, but have no education. I feel like I should be using a "while" statement while(file_exists('pictures/'.$timestamp.'_'.$count.'.jpg')) instead of or in conjunction with the current if statement I have if (file_exists('pictures/'.$timestamp.'_'.$count.'.jpg'))
The entire code I am using is here:
<?php
$count=1;
$handle = opendir(dirname(realpath(__FILE__)).'/pictures/');
while($file = readdir($handle)){
if($file !== '.' && $file !== '..'){
date_default_timezone_set('America/Edmonton');
$dirloc = "pictures/$file";
$newdirloc = "NewPictures/$file";
$exif_data = exif_read_data ($dirloc, $file, 0, true);
$exifString = $exif_data['DateTimeOriginal'];
$exifPieces = explode(":", $exifString);
$newExifString = $exifPieces[0] . "-" . $exifPieces[1] . "-" . $exifPieces[2] . ":" . $exifPieces[3] . ":" . $exifPieces[4];
$exifTimestamp = strtotime($newExifString);
$timestamp = date("y-m-d_H-i-s", $exifTimestamp);
if (file_exists('pictures/'.$timestamp.'_'.$count.'.jpg')) {
$ExistingFile = 'pictures/'.$timestamp.'_'.$count.'.jpg';
$delimiters = ['-', '_', '.'];
$newStr = str_replace($delimiters, $delimiters[0], $ExistingFile);
$NamePieces = explode("-", $newStr);
$count = $NamePieces[6];
++$count;
echo ($file.' has now been changed to '.$timestamp.'_'.$count.'.jpg (Increased count from existing file)<br>');
rename('pictures/'.$file, 'pictures/'.$timestamp.'_'.$count.'.jpg');
}
else {
echo ($file.' has now been changed to '.$timestamp.'_1.jpg (Unique File)<br>');
rename('pictures/'.$file, 'pictures/'.$timestamp.'_1.jpg');
}
}
}
?>
Thanks for helping this newbie figure this out!
Edit:
I think I've narrowed it down to a simpler question.
If it is possible to see if $ANY_NUMBER is in fact any number, what would I say $ANY_NUMBER is = to? With this change, I should be able to get rid of the count=1 at the start, and if it is true that it is any number in that [6] spot, than I should be able to say that count= that [6] spot. Does that make sense?
if (file_exists('pictures/'.$timestamp.'_'.$ANY_NUMBER.'.jpg')) {
$ExistingFile = 'pictures/'.$timestamp.'_'.$ANY_NUMBER.'.jpg';
$delimiters = ['-', '_', '.'];
$newStr = str_replace($delimiters, $delimiters[0], $ExistingFile);
$NamePieces = explode("-", $newStr);
$count = $NamePieces[6];
++$count;
echo ($count);
}
I'm trying to split a line of PHP up, the reason being I don't actually always need some of the code and so that I can reuse parts of it.
The main reason I'm doing this is because the currency I get shows more digits for some currencies e.g. 1.2562 instead of 1.25, so I want to use the substr function only on certain GET's and be able to modify it for other GET's.
http://prntscr.com/6ttw8o
symbol is always required, substr isn't, $converter always required, end part of substr isn't however it can change, new currency is required.
$symbol[2] . substr(($converter->convert($defaultCurrency, $newCurrency) * 1), 0, 4) . " <b>" .$newCurrency. "</b>";
I've tried doing this with explode, however I'm not entirely sure how to do it as I have never really had to split anything up before so I'm a little puzzled on how to go about it.
Once the code has gone through the GET checking which is the current one set, I want it to grab the specified split up code pieces and then output it.
Put your code in a function like this:
function formatCurrency($symbol, $converter, $defaultCurrency, $newCurrency, $factor=1.0, $suffix="", $substrChars=0) {
if($substrChars>0) {
return $symbol . substr(($converter->convert($defaultCurrency, $newCurrency) * $factor), 0, $substrChars) . $suffix . " <b>" . $newCurrency. "</b>";
} else {
return $symbol . ($converter->convert($defaultCurrency, $newCurrency) * $factor) . $suffix . " <b>" . $newCurrency. "</b>";
}
}
If you call it without the $substrChars parameter, it will omit the substr() call, otherwise it will strip all but the first $substrChars characters:
if( $_GET['currency'] === "GBP" ){
$newCurrency = $_GET['currency'];
$string = formatCurrency($symbol[1], $converter, $defaultCurrency, $newCurrency, 2.0, ".00", 0);
} elseif( $_GET['currency'] === "USD" ){
$newCurrency = $_GET['currency'];
$string = formatCurrency($symbol[2], $converter, $defaultCurrency, $newCurrency, 1.0, "", 4);
}
This solution is very readable because you immediately see the difference between the two branches in the conditional statement.
I am writing a script in PHP in which I had to write the system uptime, the current time, and the amount of users logged in the system into a log file, and be updated continually via a crontab.
What I need help with is that I would like the updates to accumulate within the file and be added continually. So far, whenever my script gets executed, the newest update overwrites the previous update.
What I've done is that I tried to declare an array of entries and as I iterate through the array push the contents of the update into the array (It might be a bit of half-baked logic on my part).
My Code:
$fileName = '../so-and-so directory/output.log';
$dt = date('m/d/y');
$time = date('h:i A');
$data = shell_exec('uptime');
$uptime= explode(' up ', $data);
$uptime = explode(', ', $uptime[1]);
$uptime = $uptime[0].','.$uptime[1];
$users = system('w', $who);
$array = new SplFixedArray(3);
$fileLog = fopen($fileName, 'w');
$fileString = "Date: ".$dt. "\n". " Time: ".$time . "\n".
"System uptime ". $uptime ."\n" ."Users " . $users;
foreach ($array as $entry) {
array_push(file_put_contents($fileName, $fileString));
}
fclose($fileLog);
I feel that the solution is very simple but I'm missing it. Would somebody please clue me in?
The "w" filemode truncates the file on open. "a" appends to the end instead. See fopen(3) or the PHP documentation for details.
Also, file_put_contents() is destroying the file. Try fwrite() instead.
drop fopen; simply use
file_put_contents($fileName, $fileString);
file_put_contents will overwrite the existing file by default.
In short:
$fileName = '../so-and-so directory/output.log';
$dt = date('m/d/y');
$time = date('h:i A');
$data = shell_exec('uptime');
$uptime= explode(' up ', $data);
$uptime = explode(', ', $uptime[1]);
$uptime = $uptime[0].','.$uptime[1];
$users = system('w', $who);
$fileString = "Date: ".$dt. "\n". " Time: ".$time . "\n".
"System uptime ". $uptime ."\n" ."Users " . $users;
file_put_contents($fileName, $fileString);
So it turns out that I needed to edit my crontab file as such:
* * * * * such-and-such-script.php >> ../so-and-so directory/output.log 2>&1
To make them append without the previous one being overwritten by the new one. I also lost the fopen() and instead of doing file_put_contents, I did fwrite() into the file. It works great now. Thank you!
I'm familiar with creating a CSV, then adding lines to it by using functions like fputcsv, or manually with fwrite. IE:
fwrite($fh, $facility . "\r\n");
fwrite($fh, $s . " - " . $e . "\r\n");
fwrite($fh, "\r\n");
fwrite($fh, "\r\n");
foreach ($first_column as $f){
fwrite($fh, $f . "," . "\r\n");
}
My $first_column array looks something like:
$first_column = array(
'Chocolate Milk',
'White Milk',
'Orange Juice',
'Apple Juice',
);
It creates a file that looks similar to this:
West Middle School
5/1/2013 - 5/3/2013
Chocolate Milk,
White Milk,
Orange Juice,
Apple Juice,
I'd like to add data in the next field based on dates. The data will look similar to this:
$second column['05/01/2013'] = array(5,3,2,2);
$third column['05/02/2013'] = array(5,1,3,5);
I then can just loop through each date, and match up the values to the $first_column mapping.
Can this be done? Or will I have to write the data on a row by row basis like:
$data_row['chocolate milk'] = array(5,5,3,6);
$data_row['white milk'] = array(3,1,5,0);
My desired CSV would be something like this:
West Middle School
5/1/2013 - 5/3/2013
,5/1/2013,5/2/2013
Chocolate Milk,5,5
White Milk,3,1
Orange Juice,2,3
Apple Juice,2,5
You can have data in your program in column basis. But write them to file in this manner is not optimal. Better will be to create csv structure in memory and then store it in row order.
However, you can store them in col-by-col. Just write first collumn. Close file. Reopen file, load it line-by-line and append to end of each line second column. This create you new "line". Write it to new file.... and repeat this until done. This is fastest solution to programm, if you want to col-by-col aproach. Efficiency of this approach will be poor, of course.
THE PROCESS:
User checks checkboxes to share files with customer accounts
Checkbox values are compared against an array stored in a txt file from the customers folder
The arrays are compared by being merged into one array using array_merge()
The duplicates are eliminated using array_unique()
New array written to txt file
THE PROBLEM:
If my text file already contains the following data: (numbers representing text file lines)
M HTH A277 Frame Off STD Specs 02-01-12.pdf
M HTH A277 Frame On STD Specs 02-01-12.pdf
M HTH Option Can Price List 02-02-2012.xls
I then try to share more files including those that are already shared. My new text file looks like this: (numbers representing text file lines)
M HTH A277 Frame Off STD Specs 02-01-12.pdf
(blank)
M HTH A277 Frame On STD Specs 02-01-12.pdf
(blank)
M HTH Option Can Price List 02-02-2012.xls
(blank)
(blank)
M HTH A277 Frame Off STD Specs 02-01-12.pdf
M HTH A277 Frame On STD Specs 02-01-12.pdf
M HTH Option Can Price List 02-02-2012.xls
Valley Creek Estates - 2010.pdf
The values above are the exact values I'm dealing with. I've tried to be as thorough as possible with this explanation which could make things confusing. If anyone can provide me with any suggestions they would be greatly appreciated. Thanks in advance. This is what I've got for code so far:
THE CODE:
$arr = $_POST['checked'];
$cust = $_POST['custname'];
if ($cust != ""){
$myFile = "CONTA.txt";
//If file exists get previous array from file
if (file_exists("customer/" . $cust . "/" . $myFile)) {
$fh = fopen("customer/" . $cust . "/" . $myFile, 'r') or die("");
while (!feof($fh) ) {
$compare[] = fgets($fh);
}
fclose($fh);
//Combine checkbox array with previous array & eliminate duplicates
$combined = array_unique(array_merge($compare,$arr));
}
else
{
//Since no previous file or array existed. Just use current checkbox array.
$combined = $arr;
}
//Input array into file
$fh = fopen("customer/" . $cust . "/" . $myFile, 'w') or die("can't open file");
foreach ($combined as $value) {
fwrite($fh, $value . "\n");
}
echo "<span class='message'>Items shared successfully!</span>";
fclose($fh);
}
}
To me it looks like the problem is "\n" character. Each line has a new line on the end and when you compare one line that has the newline character and the same line that doesn't they aren't the same. I would confirm this by echoing each line from the fgets. If they are line broken, then you know you are getting the new line character.
EDIT:
I would try putting
trim(fgets($fh))
by default it should strip the newline character
trim specs