Skip to content

Commit 8fc9016

Browse files
update
1 parent ff04bde commit 8fc9016

File tree

4 files changed

+42
-59
lines changed

4 files changed

+42
-59
lines changed

src/Capsule/Artisan.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public static function call(string $input): int
7171
$args = array_slice($tokens, 1);
7272
$argv = array_merge(['tame'], [$command], $args);
7373

74+
require_once base_path('tame');
75+
7476
$artisan = new self();
7577

7678
return $artisan->run($argv);

src/Commands/ProcessorCommand.php

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace Tamedevelopers\Support\Commands;
66

7+
use Tamedevelopers\Support\Tame;
78
use Tamedevelopers\Support\Server;
89
use Tamedevelopers\Support\ImageToText;
910
use Tamedevelopers\Support\NameToImage;
11+
use Tamedevelopers\Support\Capsule\File;
1012
use Tamedevelopers\Support\Capsule\Logger;
1113
use Tamedevelopers\Support\Capsule\CommandHelper;
1214

@@ -36,8 +38,8 @@ class ProcessorCommand extends CommandHelper
3638
public function handle()
3739
{
3840
Logger::helpHeader('<yellow>Usage:</yellow>');
39-
Logger::writeln(' php tame make:processor [name]');
40-
Logger::writeln(' php tame make:processor [name]');
41+
Logger::writeln(" php tame processor:toImage --name --path --bgColor --textColor --fontWeight=[bold|normal] --type=[circle|radius] --generate=[bool] \n");
42+
Logger::writeln(' php tame processor:toText --path= --grayscale=[bool] --contrast=[int]');
4143
Logger::writeln('');
4244
}
4345

@@ -46,15 +48,15 @@ public function handle()
4648
*/
4749
public function toImage(): string
4850
{
49-
$args = $this->arguments();
50-
[$name, $bgColor, $textColor, $path, $generate, $output, $fontWeight] = [
51+
[$name, $bgColor, $textColor, $path, $generate, $output, $fontWeight, $type] = [
5152
$this->flag('name'),
5253
$this->flag('bgColor'),
5354
$this->flag('textColor'),
5455
$this->flag('path'),
55-
$this->flag('output'),
5656
(bool) $this->flag('generate') ?: false,
57+
$this->flag('output'),
5758
$this->flag('fontWeight'),
59+
$this->flag('type'),
5860
];
5961

6062
if(!in_array($output, ['save', 'data'])){
@@ -69,46 +71,38 @@ public function toImage(): string
6971
'destination' => $path,
7072
'output' => $output,
7173
'generate' => $generate,
74+
'type' => $type,
7275
]));
7376

77+
$path = Tame::getBasePath($path);
78+
7479
Logger::info("$path\n");
7580

7681
return $path;
7782
}
7883

7984
/**
80-
* Convert and Extract Image to Text
85+
* Extract an Image to Text
8186
*/
8287
public function toText(): string
8388
{
84-
$args = $this->arguments();
85-
[$name, $bgColor, $textColor, $path, $generate, $output, $fontWeight] = [
86-
$this->flag('name'),
87-
$this->flag('bgColor'),
88-
$this->flag('textColor'),
89-
$this->flag('path'),
90-
$this->flag('output'),
91-
(bool) $this->flag('generate') ?: false,
92-
$this->flag('fontWeight'),
89+
[$path, $grayscale, $contrast] = [
90+
$this->flag('path'),
91+
(bool) $this->flag('grayscale') ?: true,
92+
$this->flag('contrast') ?: 20,
9393
];
94-
95-
if(!in_array($output, ['save', 'data'])){
96-
$output = 'save';
97-
}
98-
99-
$path = Server::pathReplacer(ImageToText::run([
100-
'name' => $name,
101-
'bg_color' => $bgColor,
102-
'font_weight' => $fontWeight,
103-
'text_color' => $textColor,
104-
'destination' => $path,
105-
'output' => $output,
106-
'generate' => $generate,
107-
]));
108-
109-
Logger::info("$path\n");
110-
111-
return $path;
94+
95+
$text = ImageToText::run([
96+
'source' => Tame::getBasePath($path),
97+
'preprocess' => [
98+
'grayscale' => $grayscale,
99+
'contrast' => $contrast,
100+
],
101+
]);
102+
103+
Logger::info("$text\n");
104+
105+
return $text;
112106
}
113107

114108
}

src/Traits/TameTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ private static function getFiletime($path = null)
263263
* @param string|null $path
264264
* @return mixed
265265
*/
266-
private static function getBasePath($path = null)
266+
public static function getBasePath($path = null)
267267
{
268268
// removing default base directory path if added by default
269269
$path = Str::replace(Server::formatWithBaseDirectory(), '', $path);

tests/imageToText.php

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Tamedevelopers\Support\ImageToText;
4+
use Tamedevelopers\Support\Capsule\Artisan;
45

56
require_once __DIR__ . '/../vendor/autoload.php';
67

@@ -14,33 +15,19 @@
1415

1516
$isCli = (php_sapi_name() === 'cli');
1617

17-
if ($isCli) {
18-
$path = $argv[1] ?? '';
19-
$lang = $argv[2] ?? 'eng';
20-
$psm = isset($argv[3]) ? (int)$argv[3] : null;
18+
dd(
19+
Artisan::call('processor:toText --path=thousand_units.png'),
20+
);
2121

22-
if ($path === '' || !is_readable($path)) {
23-
fwrite(STDERR, "Usage: php tests/imagetotext.php <image_path> [lang=eng] [psm]\n");
24-
exit(1);
25-
}
22+
// :toText --path=thousand_units.png
23+
if ($isCli) {
24+
$text = Artisan::call('processor');
2625

27-
try {
28-
$text = ImageToText::run([
29-
'source' => $path,
30-
'language' => $lang,
31-
'psm' => $psm,
32-
'preprocess' => [
33-
'grayscale' => true,
34-
'contrast' => 20,
35-
// 'threshold' => 170, // optionally enable thresholding
36-
],
37-
]);
38-
echo $text, "\n";
39-
} catch (\Throwable $e) {
40-
fwrite(STDERR, 'OCR error: ' . $e->getMessage() . "\n");
41-
exit(2);
42-
}
43-
exit(0);
26+
dd(
27+
// $args,
28+
Artisan::call("list"),
29+
$text
30+
);
4431
}
4532

4633
// Web mode

0 commit comments

Comments
 (0)