img2html

Gesponsorde koppelingen

PHP script bestanden

  1. img2html

« Lees de omschrijving en reacties

#!/usr/bin/php -q

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
ini_set('memory_limit', '128M'); //potentially heavy arrays

if (strtolower(substr(PHP_OS, 0, 3) != 'win')) { //windoze detection
  $nonwin32 = true; //whitch is a good thing :)
}

function
askconfirm () { //ask for confirm
  echo "(y/n)";
  $confirm = strtolower(trim(fgets(STDIN)));
  if ($confirm == 'y') {
    return true;
  }
elseif ($confirm == 'n') {
    return false;
  }
else {
    return askconfirm();
  }
}

function
line_echo ($string, $linelen) { //one-line echo in console quick-fix
  $strlen = strlen($string);
  if ($strlen > $linelen) {
    echo "\nline_echo error: String too long\n";
  }
else {
    $i = 0;
    while ($i < intval($linelen)) {
      $spaces .= ' ';
      $backs .= '\b';
      $i++;
    }

    echo shell_exec('echo -e "'.$backs.$spaces.$backs.'\c"; echo -e "'.$string.'\c"');
  }
}

function
progressbar ($total, $progress, $numblocks) { //draw progress bar
  $linelen = intval($numblocks)+6;
  $blocks = round(($progress/$total*$numblocks), 0);
  $procent = round(($progress/$total*100), 0).'%';
  $a = 0;
  $b = 0;
  while ($a < $blocks) {
    $blockprint .= '#';
    $a++;
  }

  while ($b < ($numblocks-$blocks)) {
    $spaceprint .= ' ';
    $b++;
  }

  $string = '['.$blockprint.$spaceprint.']'.$procent;
  line_echo($string, $linelen);
}

function
getmicrotime () { //tnx PHP.NET
  list($usec, $sec) = explode(' ',microtime());
  return ((float)$usec + (float)$sec);
}
  
function
rgb2hex($rgb) { //tnx PHP.NET
  return sprintf("%06X", $rgb);
}
  
function
write($string, $handle) { //short fwrite function
  @fwrite($handle, $string, strlen($string));
}

function
asktype() { //ask for the filetype
  echo "Type [jpg/gif/png/bmp]:";
  $type = strtolower(trim(fgets(STDIN)));
  if (!in_array($type, array('jpg', 'gif', 'png', 'bmp'))) {
    $type = asktype();
  }

  return $type;
}

function
askxpix() { //xpix question
  echo "Xpix [int]:";
  $xpix = intval(strtolower(trim(fgets(STDIN))));
  if ($xpix == 0 || $xpix === false) {
    $xpix = askxpix();
  }

  return $xpix;
}

function
askypix() { //ypix question
  echo "Ypix [int]:";
  $ypix = intval(strtolower(trim(fgets(STDIN))));
  if ($ypix == 0 || $ypix === false) {
    $ypix = askypix();
  }

  return $ypix;
}

function
askimage() { //image question
  echo "Image [filename]:";
  $filein = trim(fgets(STDIN));
  if (!file_exists($filein)) {
    $filein = askimage();
  }
else {
    return $filein;
  }
}

function
askfile() { //out question
  echo "HtmlFile [filename]:";
  $fileout = trim(fgets(STDIN));
  return $fileout;
}

function
img2html($type, $xpix, $ypix, $file, $out, $nonwin32) { //the magic itself
  $timebegin = getmicrotime();
  if (!file_exists($file)) {
    echo "File does not exist!\r\n";
    exit;
  }

  if ($xpix <= 0 || $ypix <= 0) {
    echo "Pix size Error\r\n";
    exit;
  }

  if ($type == 'jpg') {
    $img = @imagecreatefromjpeg($file);
    if (!$img) {
      echo "Not a valid Jpeg file!\r\n";
      exit;
    }
  }
elseif ($type == 'gif') {
    $img = @imagecreatefromgif($file);
    if (!$img) {
      echo "Not a valid Gif file!\r\n";
      exit;
    }
  }
elseif ($type == 'png') {
    $img = @imagecreatefrompng($file);
    if (!$img) {
      echo "Not a valid PNG file!\r\n";
      exit;
    }
  }
elseif ($type == 'bmp') {
    $img = @imagecreatefromwbmp($file);
    if (!$img) {
      echo "Not a valid bitmap file!\r\n";
      exit;
    }
  }
else {
    echo "Wrong Type!\r\n";
    exit;
  }

  if (file_exists($out)) {
    echo "File Exists!\r\nOverwrite? ";
    $confirm = askconfirm();
    if (!$confirm) {
      exit;
    }
else {
      unlink($out);
    }
  }

  $handle = @fopen($out, 'a+');
  if (!$handle) {
    echo "File Error!\r\n";
    exit;
  }

  $imgx = imagesx($img);
  $imgy = imagesy($img);
  $x = 0;
  $y = 0;
  $availablecolors = array();
  echo "Analyzing...\r\n";
  while ($y < $imgy && $y >= 0) {
    while ($x < $imgx) {
      $availablecolor = rgb2hex(imagecolorat($img, $x, $y));
      if ($availablecolors[$availablecolor]) {
        $availablecolors[$availablecolor]++;
      }
else {
        $availablecolors[$availablecolor] = 1;
      }

      $x++;
    }

    $procent = round((($y+1)/$imgy*100), 0);
    if ($nonwin32) {
      progressbar(100, $procent, 20);
    }
else {
      echo "Analized pixline ".($y+1)."/".$imgy." (".$procent."%)\r\n";
    }

    $x = 0;
    $y++;
  }

  echo "\r\nSorting...";
  arsort($availablecolors);
  list($tablecolor) = array_keys($availablecolors);
  echo "done.\r\nWriting...\r\n";
  write('<html><head><title>IMG2HTML: '.$file.' ('.$imgx.'x'.$imgy.')</title></head><body>'."\r\n".'<table width="'.($xpix*$imgx).'" height="'.($ypix*$imgy).'" cellpadding="0" cellspacing="0" border="0" bgcolor="#'.$tablecolor.'">'."\r\n", $handle);
  $x = 0;
  $y = 0;
  while ($y < $imgy && $y >= 0) {
    write('<tr>'."\r\n", $handle);
    $colspan = 1;
    while ($x <= $imgx) {
      $imgcolors = rgb2hex(imagecolorat($img, $x, $y));
      if ($x != 0) {
        if ($prevcolor == $imgcolors) {
          $colspan++;
        }
else {
          if ($colspan != 1) {
            if ($prevcolor == $tablecolor) {
              write('<td height="'.$ypix.'" width="'.$colspan.'" colspan="'.$colspan.'"></td>'."\r\n", $handle);
            }
else {
              write('<td height="'.$ypix.'" width="'.$colspan.'" colspan="'.$colspan.'" bgcolor="#'.$prevcolor.'"></td>'."\r\n", $handle);
            }

            $colspan = 1;
          }
else {
            if ($prevcolor == $tablecolor) {
              write('<td height="'.$ypix.'" width="'.$xpix.'"></td>'."\r\n", $handle);
            }
else {
              write('<td height="'.$ypix.'" width="'.$xpix.'" bgcolor="#'.$prevcolor.'"></td>'."\r\n", $handle);
            }
          }
        }
      }

      $prevcolor = $imgcolors;
      $x++;
    }

    write('</tr>'."\r\n", $handle);
    $procent = round((($y+1)/$imgy*100), 0);
    if ($nonwin32) {
      progressbar(100, $procent, 20);
    }
else {
      echo "Wrote pixline ".($y+1)."/".$imgy." (".$procent."%)\r\n";
    }

    $x = 0;
    $y++;
  }

  write('</table>'."\r\n".'<!--Created by IMG2HTML v0.2.0 (jorrizza.net, phphulp.nl)-->'."\r\n".'</body></html>', $handle);
  @
fclose($handle);
  @
imagedestroy($img);
  echo "\r\nDone in ".round(getmicrotime()-$timebegin, 5)."s.\r\n";
}

echo "IMG2HTML 0.2.0\r\n";
$type = asktype();
$xpix = askxpix();
$ypix = askypix();
$filein = askimage();
$fileout = askfile();
echo "\r\n";
img2html($type, $xpix, $ypix, $filein, $fileout, $nonwin32);
?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.