Skip to content

Instantly share code, notes, and snippets.

@sarfraznawaz2005
Last active October 12, 2015 18:32
Show Gist options
  • Save sarfraznawaz2005/88c9a7c49b9e075a8337 to your computer and use it in GitHub Desktop.
Save sarfraznawaz2005/88c9a7c49b9e075a8337 to your computer and use it in GitHub Desktop.
Image2HTML
<?php
function img2html($image, $letters='') {
/*
img2html:
takes an image as an array of data that is created by PHP
for uploaded files.
*/
$suffix = preg_replace('%^[^/]+/%', '', $image['type']);
$getimagefunc = "imagecreatefrom$suffix";
if (!function_exists($getimagefunc)) {
echo "This server does not support the $getimagefunc() function; exiting";
return false;
}
if (!$letters)
$letters = 'ARSE';
$img = $getimagefunc($image['tmp_name']);
$count = 0;
$numletters = strlen($letters);
list($width, $height) = getimagesize($image['tmp_name']);
echo '<table cellpadding=0 cellspacing=0 style="cell-spacing: 0px;">';
for ($y = 0; $y < $height; $y++) {
echo "<tr>\n";
for ($x = 0; $x < $width; $x++) {
$count++;
$colours = imagecolorsforindex($img, imagecolorat($img, $x, $y));
#$hexcolour = '#' . dechex($colours['red']) . dechex($colours['green']) . dechex($colours['blue']);
$hexcolour = sprintf("#%02x%02x%02x", $colours['red'], $colours['green'], $colours['blue']);
echo "<td style='height: 3px; width: 3px; padding: 0px; font-size: 3px; background-color: $hexcolour; color: white;'>",
$letters[($count % $numletters)],
'</td>';
}
#if (!$count % 3)
# continue;
echo "</tr>\n";
}
echo "</table>\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment