Created
October 1, 2015 19:01
-
-
Save aschmelyun/c7728446b42f2d011d12 to your computer and use it in GitHub Desktop.
Convert .png images to HTML divs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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