<?php
error_reporting(E_ALL);
class Image {
var $img;
var $image;
var $type;
function image($imagefile) {
$this->image = $imagefile;
if (!$this->loadImage()) {
die('Bild konnte nicht geladen werden.');
}
}
function loadImage() {
if (preg_match('/http:\/\/([^\/]+)(\/.*)/', $this->image, $match)) {
if (!function_exists('get_headers')) {
$fp = fsockopen($match[1], 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET " . $match[2] . " HTTP/1.1\r\n";
$out .= "Host: " . $match[1] . "\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
if (preg_match('/Content-type: image\/(.*)/i', fgets($fp, 128), $match)) {
$this->type = $match[1];
}
}
fclose($fp);
}
} else {
$headers = get_headers($this->image);
foreach ($headers as $header) {
if (preg_match('/Content-type: image\/(.*)/i', $header, $match)) {
$this->type = $match[1];
}
}
}
} else {
$fileinfo = pathinfo($this->image);
$this->type = $fileinfo['extension'];
}
switch ($this->type) {
case 'jpg':
$this->img = @imagecreatefromjpeg($this->image);
break;
case 'png':
$this->img = @imagecreatefrompng($this->image);
break;
}
return $this->img;
}
function getTextFromImage($x, $y, $width, $fontsize = 5, $bkg = array(255, 255, 255), $font = array(0, 0, 0)) {
$output = '';
$tx = $x;
$ty = $y - 3;
imagepng($this->img, 'temp.png');
while ($tx < $width) {
$chars = array();
for ($i = 32; $i <= 122; $i++) {
$blanks = 0;
$img = imagecreatefrompng('temp.png');
list($r, $g, $b) = $bkg;
$colors['bkg'] = imagecolorallocate($img, $r, $g, $b);
imagestring($img, $fontsize, $tx, $ty, chr($i), $colors['bkg']);
for ($x = 0; $x < imagefontwidth($fontsize); $x++) {
for ($y = 0; $y < imagefontheight($fontsize); $y++) {
$dotcolor = imagecolorat($img, $tx + $x, $ty + $y);
if ($dotcolor == 0) {
$blanks++;
}
}
}
$chars[$i] = $blanks;
}
arsort($chars, SORT_NUMERIC);
$keys = array_keys($chars);
$key = $keys[0];
if ($key == 32 && $chars[$keys[1]] == $chars[$key]) {
$key = $keys[1];
}
$output .= chr($key);
$tx += imagefontwidth($fontsize);
}
return $output;
}
}
$img = new Image('http://www.source-news.de/sig/signatur.php?user=nex&stamm=5739');
echo $img->getTextFromImage(40, 23, 70, 5, array(255, 255, 255));
?>