1 | #!/usr/bin/php5 |
---|
2 | <?php |
---|
3 | /* |
---|
4 | * img2txt image to text converter |
---|
5 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
6 | * 2007 Jean-Yves Lamoureux <jylam@lnxscene.org> |
---|
7 | * All Rights Reserved |
---|
8 | * |
---|
9 | * $Id: img2txt.php 3302 2008-11-06 23:25:26Z bsittler $ |
---|
10 | * |
---|
11 | * This program is free software. It comes without any warranty, to |
---|
12 | * the extent permitted by applicable law. You can redistribute it |
---|
13 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
14 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
15 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
16 | */ |
---|
17 | |
---|
18 | if (php_sapi_name() != "cli") { |
---|
19 | die("You have to run this program with php-cli!\n"); |
---|
20 | } |
---|
21 | |
---|
22 | function usage($argc, $argv) |
---|
23 | { |
---|
24 | fprintf(STDERR, "Usage: %s [OPTIONS]... <IMAGE>\n", $argv[0]); |
---|
25 | fprintf(STDERR, "Convert IMAGE to any text based available format.\n"); |
---|
26 | fprintf(STDERR, "Example : %s -w 80 -f ansi ./caca.png\n\n", $argv[0]); |
---|
27 | fprintf(STDERR, "Options:\n"); |
---|
28 | fprintf(STDERR, " -h, --help\t\t\tThis help\n"); |
---|
29 | fprintf(STDERR, " -v, --version\t\t\tVersion of the program\n"); |
---|
30 | fprintf(STDERR, " -W, --width=WIDTH\t\tWidth of resulting image\n"); |
---|
31 | fprintf(STDERR, " -H, --height=HEIGHT\t\tHeight of resulting image\n"); |
---|
32 | fprintf(STDERR, " -x, --font-width=WIDTH\t\tWidth of output font\n"); |
---|
33 | fprintf(STDERR, " -y, --font-height=HEIGHT\t\tHeight of output font\n"); |
---|
34 | fprintf(STDERR, " -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n"); |
---|
35 | fprintf(STDERR, " -c, --contrast=CONTRAST\tContrast of resulting image\n"); |
---|
36 | fprintf(STDERR, " -g, --gamma=GAMMA\t\tGamma of resulting image\n"); |
---|
37 | fprintf(STDERR, " -d, --dither=DITHER\t\tDithering algorithm to use :\n"); |
---|
38 | $list = caca_get_dither_algorithm_list(caca_create_dither(imagecreate(1, 1))); |
---|
39 | foreach($list as $type => $name) |
---|
40 | { |
---|
41 | fprintf(STDERR, "\t\t\t%s: %s\n", $type, $name); |
---|
42 | } |
---|
43 | |
---|
44 | fprintf(STDERR, " -f, --format=FORMAT\t\tFormat of the resulting image :\n"); |
---|
45 | $list = caca_get_export_list(); |
---|
46 | foreach($list as $type => $name) |
---|
47 | { |
---|
48 | fprintf(STDERR, "\t\t\t%s: %s\n", $type, $name); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | function version() |
---|
53 | { |
---|
54 | printf( |
---|
55 | "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n" . |
---|
56 | "Internet: <sam@zoy.org> <jylam@lnxscene.org> Version: %s\n" . |
---|
57 | "\n" . |
---|
58 | "img2txt, along with its documentation, may be freely copied and distributed.\n" . |
---|
59 | "\n" . |
---|
60 | "The latest version of img2txt is available from the web site,\n" . |
---|
61 | " http://caca.zoy.org/wiki/libcaca in the libcaca package.\n" . |
---|
62 | "\n", |
---|
63 | caca_get_version()); |
---|
64 | } |
---|
65 | function main() |
---|
66 | { |
---|
67 | global $argc, $argv; |
---|
68 | $cols = 0; |
---|
69 | $lines = 0; |
---|
70 | $font_width = 6; |
---|
71 | $font_height = 10; |
---|
72 | $format = NULL; |
---|
73 | $dither = NULL; |
---|
74 | $gamma = $brightness = $contrast = -1.0; |
---|
75 | |
---|
76 | if($argc < 2) |
---|
77 | { |
---|
78 | fprintf(STDERR, "%s: wrong argument count\n", $argv[0]); |
---|
79 | usage($argc, $argv); |
---|
80 | return 1; |
---|
81 | } |
---|
82 | |
---|
83 | $long_options = array( |
---|
84 | "width:" => 'W', |
---|
85 | "height:" => 'H', |
---|
86 | "font-width:" => 'x', |
---|
87 | "font-height:" => 'y', |
---|
88 | "format:" => 'f', |
---|
89 | "dither:" => 'd', |
---|
90 | "gamma:" => 'g', |
---|
91 | "brightness:" => 'b', |
---|
92 | "contrast:" => 'c', |
---|
93 | "help" => 'h', |
---|
94 | "version" => 'v' |
---|
95 | ); |
---|
96 | $options = getopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options)); |
---|
97 | if (! $options) |
---|
98 | { |
---|
99 | /* PHP before 5.3 or so does not have long option support in |
---|
100 | * most cases */ |
---|
101 | $options = getopt("W:H:f:d:g:b:c:hvx:y:"); |
---|
102 | } |
---|
103 | |
---|
104 | foreach($options as $opt => $arg) |
---|
105 | { |
---|
106 | if (array_key_exists($opt + (isset($arg) ? ':' : ''), $long_options)) |
---|
107 | { |
---|
108 | $opt = $long_options[$opt + (isset($arg) ? ':' : '')]; |
---|
109 | } |
---|
110 | switch($opt) |
---|
111 | { |
---|
112 | case 'W': /* --width */ |
---|
113 | $cols = intval($arg); |
---|
114 | break; |
---|
115 | case 'H': /* --height */ |
---|
116 | $lines = intval($arg); |
---|
117 | break; |
---|
118 | case 'x': /* --width */ |
---|
119 | $font_width = intval($arg); |
---|
120 | break; |
---|
121 | case 'y': /* --height */ |
---|
122 | $font_height = intval($arg); |
---|
123 | break; |
---|
124 | case 'f': /* --format */ |
---|
125 | $format = $arg; |
---|
126 | break; |
---|
127 | case 'd': /* --dither */ |
---|
128 | $dither = $arg; |
---|
129 | break; |
---|
130 | case 'g': /* --gamma */ |
---|
131 | $gamma = floatval($arg); |
---|
132 | break; |
---|
133 | case 'b': /* --brightness */ |
---|
134 | $brightness = floatval($arg); |
---|
135 | break; |
---|
136 | case 'c': /* --contrast */ |
---|
137 | $contrast = floatval($arg); |
---|
138 | break; |
---|
139 | case 'h': /* --help */ |
---|
140 | usage($argc, $argv); |
---|
141 | return 0; |
---|
142 | case 'v': /* --version */ |
---|
143 | version(); |
---|
144 | return 0; |
---|
145 | default: |
---|
146 | return 1; |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | $cv = caca_create_canvas(0, 0); |
---|
152 | if(!$cv) |
---|
153 | { |
---|
154 | fprintf(STDERR, "%s: unable to initialise libcaca\n", $argv[0]); |
---|
155 | return 1; |
---|
156 | } |
---|
157 | |
---|
158 | $i_str = file_get_contents($argv[$argc-1]); |
---|
159 | $i = $i_str ? imagecreatefromstring($i_str) : NULL; |
---|
160 | if(!$i) |
---|
161 | { |
---|
162 | fprintf(STDERR, "%s: unable to load %s\n", $argv[0], $argv[$argc-1]); |
---|
163 | return 1; |
---|
164 | } |
---|
165 | |
---|
166 | /* Assume a 6×10 font */ |
---|
167 | if(!$cols && !$lines) |
---|
168 | { |
---|
169 | $cols = 60; |
---|
170 | $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height; |
---|
171 | } |
---|
172 | else if($cols && !$lines) |
---|
173 | { |
---|
174 | $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height; |
---|
175 | } |
---|
176 | else if(!$cols && $lines) |
---|
177 | { |
---|
178 | $cols = $lines * imagesx($i) * $font_height / imagesy($i) / $font_width; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | caca_set_canvas_size($cv, $cols, $lines); |
---|
183 | caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT); |
---|
184 | caca_clear_canvas($cv); |
---|
185 | $i_dither = caca_create_dither($i); |
---|
186 | if(! caca_set_dither_algorithm($i_dither, $dither?$dither:"fstein")) |
---|
187 | { |
---|
188 | fprintf(STDERR, "%s: Can't dither image with algorithm '%s'\n", $argv[0], $dither?$dither:"fstein"); |
---|
189 | return -1; |
---|
190 | } |
---|
191 | |
---|
192 | if($brightness!=-1) caca_set_dither_brightness ($i_dither, $brightness); |
---|
193 | if($contrast!=-1) caca_set_dither_contrast ($i_dither, $contrast); |
---|
194 | if($gamma!=-1) caca_set_dither_gamma ($i_dither, $gamma); |
---|
195 | |
---|
196 | caca_dither_bitmap($cv, 0, 0, $cols, $lines, $i_dither, $i); |
---|
197 | |
---|
198 | $export = caca_export_string($cv, $format?$format:"ansi"); |
---|
199 | if(!$export) |
---|
200 | { |
---|
201 | fprintf(STDERR, "%s: Can't export to format '%s'\n", $argv[0], $format); |
---|
202 | } |
---|
203 | else |
---|
204 | { |
---|
205 | echo $export; |
---|
206 | } |
---|
207 | |
---|
208 | return 0; |
---|
209 | } |
---|
210 | exit(main()); |
---|
211 | ?> |
---|