Look mom, no tables :)
I made some changes to the code from 'hazard AT krankteil DOTTILLYDO de' so the function would output a div that displays the image.
As for the size of the outputted file I can say the original png file was lots smaller, but maybe its a nice feature for small buttons or such.
The way you can use it is the same as the code from 'hazard AT krankteil DOTTILLYDO de'.
litle note: each div contains a bogus image. When this is not in IE will screw up the output.
<?
function hexcolor($c) {
$r = ($c >> 16) & 0xFF;
$g = ($c >> 8) & 0xFF;
$b = $c & 0xFF;
return '#'.str_pad(dechex($r), 2, '0', STR_PAD_LEFT).str_pad(dechex($g), 2, '0', STR_PAD_LEFT).str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
}
function png2div($filename) {
$img = imagecreatefrompng($filename);
$width = imagesx($img);
$height = imagesy($img);
$div_width = 1;
$previous_color = 0;
$output = '<div style="position:relative;width:' . $width . 'px;height:'. $height .'px;">';
for($y = 0;$y < $height;++$y){
for($x = 0;$x < $width;++$x){
$current_color = ImageColorAt($img, $x, $y);
if($current_color == $previous_color && $x < $width-1){
++$div_width;
}
else{
$output .= '<div style="position:relative;float:left;width:' . $div_width . 'px;height:1px;background-color:' . hexcolor((($div_width > 1)? $previous_color:$current_color)) . '"><img src="bogus.gif" alt="" width="1" height="1" /></div>';
$previous_color = $current_color;
$div_width = 1;
}
}
ob_flush();
}
$output .= '</div>';
return $output;
}
?>