Skip to content

Instantly share code, notes, and snippets.

@aschmelyun
Created October 1, 2015 19:01
Show Gist options
  • Save aschmelyun/c7728446b42f2d011d12 to your computer and use it in GitHub Desktop.
Save aschmelyun/c7728446b42f2d011d12 to your computer and use it in GitHub Desktop.
Convert .png images to HTML divs
<?php
$img = imagecreatefrompng('test.png');
$img_width = imagesx($img);
$img_height = imagesy($img);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Convert useless images to beautiful HTML!</title>
<style>
div {
width: 1px;
height: 1px;
position: absolute;
}
</style>
</head>
<body>
<?php
for($x=0;$x<=$img_width;$x++) {
for($y=0;$y<=$img_height;$y++) {
$pixel = getPixel($img, $x, $y);
echo '<div style="left:' . $x . 'px;top:' . $y . 'px;background:' . $pixel . '"></div>';
}
}
function getPixel($image, $x, $y) {
$colors = imagecolorsforindex($image, imagecolorat($image, $x, $y));
$rgb = 'rgb(' . $colors['red'] . ',' . $colors['green'] . ',' . $colors['blue'] . ')';
return $rgb;
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment