无法让 div 标签块正确排列

发布于 2024-09-24 09:58:26 字数 1513 浏览 0 评论 0原文

我正在使用这段代码(大部分是从 PHP.net 评论复制的 - http://www.php.net/manual/en/function.imagecolorat.php)为了读取图片,逐像素扫描并将图片作为 div 标签块输出餐桌般的时尚。这就是我想到的...

<?php 
$img = imagecreatefrompng("image1.png"); 

$w = imagesx($img); 
$h = imagesy($img); 

for($y=0;$y<$h;$y++) { 
   for($x=0;$x<$w;$x++) { 
      $rgb = imagecolorat($img, $x, $y); 
      $r = ($rgb >> 16) & 0xFF; 
      $g = ($rgb >> 8) & 0xFF; 
      $b = $rgb & 0xFF;
      $hex = "#".str_repeat("0",2-strlen(dechex($r))).dechex($r). 
              str_repeat("0",2-strlen(dechex($g))).dechex($g). 
              str_repeat("0",2-strlen(dechex($b))).dechex($b);
     echo "<div style='background: {$hex}; height: 5px; width: 5px; display: inline;'></div>\r\n";
     /*
        echo "#".str_repeat("0",2-strlen(dechex($r))).dechex($r). 
              str_repeat("0",2-strlen(dechex($g))).dechex($g). 
              str_repeat("0",2-strlen(dechex($b))).dechex($b).","; 
     */ 
  } 
  echo "<br />\r\n"; 
} 
?>

我尝试使用“block”、“inline”、“inline-block”和“inline-table”作为 div 的显示属性,但它们似乎都有自己的问题。我要么什么也得不到,像素列在垂直线上直接向下,或者div在正方形中正确排列,但它们之间有间距(这不应该发生,因为我使用reset.css来消除所有填充、间距等)。

此外,这个特定的函数似乎没有考虑到透明度。我正在使用的图片中有透明像素,它似乎将它们输出为浅蓝色。

链接 - http://schnell.dreamhosters.com/folio/pixelread.php

I'm using this block of code (mostly copied from a PHP.net comment here - http://www.php.net/manual/en/function.imagecolorat.php) in order to read in a picture, scan it pixel by pixel and output the picture as a block of div tags in a table-like fashion. Here's what I came up with...

<?php 
$img = imagecreatefrompng("image1.png"); 

$w = imagesx($img); 
$h = imagesy($img); 

for($y=0;$y<$h;$y++) { 
   for($x=0;$x<$w;$x++) { 
      $rgb = imagecolorat($img, $x, $y); 
      $r = ($rgb >> 16) & 0xFF; 
      $g = ($rgb >> 8) & 0xFF; 
      $b = $rgb & 0xFF;
      $hex = "#".str_repeat("0",2-strlen(dechex($r))).dechex($r). 
              str_repeat("0",2-strlen(dechex($g))).dechex($g). 
              str_repeat("0",2-strlen(dechex($b))).dechex($b);
     echo "<div style='background: {$hex}; height: 5px; width: 5px; display: inline;'></div>\r\n";
     /*
        echo "#".str_repeat("0",2-strlen(dechex($r))).dechex($r). 
              str_repeat("0",2-strlen(dechex($g))).dechex($g). 
              str_repeat("0",2-strlen(dechex($b))).dechex($b).","; 
     */ 
  } 
  echo "<br />\r\n"; 
} 
?>

I've tried using 'block', 'inline', 'inline-block' and 'inline-table' for the display property of the divs, but they each seem to make their own problems. I either get nothing at all, columns of pixels going straight down in a vertical line or the divs line up correctly in a square, but with spacing between them (which shouldn't happen since I'm using a reset.css to eliminate all padding, spacing, etc).

Also, this particular function doesn't seem to account for transparency. The picture I'm using has transparent pixels in it and it seems to be outputing them as a light blue.

Link - http://schnell.dreamhosters.com/folio/pixelread.php

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡渣熟男 2024-10-01 09:58:26

嗯,这是一个有趣的问题。我不能保证该函数具有透明度,但 HTML 定位问题应该很容易解决。

我认为最好的解决方案是创建一个与图像一样宽的容器,然后将所有 div 向左浮动。 像 Which一样的东西

echo "<div style='width: ".($imagesx * 5)."px;' class='outer'>";

会给出你需要的宽度(如果我正确理解代码的话)。然后你只需使用这个CSS:

.outer div {
  width: 5px;
  height: 5px;
  float: left;
}

这样做将有效地减少你必须生成的冗余内联样式的数量。哦,并删除每行后面生成的 br

至于alpha透明度问题,我相信这个评论应该对你有帮助: http ://www.php.net/manual/en/function.imagecolorat.php#79116


编辑

您忘记了 width: 65px 上的 px!这是正在生成的。

<div style='width: 65; background: #eeeeee; margin: 0px auto;'>

还有,可爱的马里奥。 ;)

Well, this is an interesting one. I can't vouch for that function accounting for transparency, but the HTML positioning problem should be easy enough to solve.

I think the best solution would be to create a container as wide as the image, then float all of the divs left. Something like

echo "<div style='width: ".($imagesx * 5)."px;' class='outer'>";

Which will give the width you need (if I understand the code correctly). Then you simply use this CSS:

.outer div {
  width: 5px;
  height: 5px;
  float: left;
}

Doing this will effectively reduce the number of redundant inline styles you have to generate. Oh, and remove the br generated after each row.

As for the alpha transparency problem, I believe this comment should help you: http://www.php.net/manual/en/function.imagecolorat.php#79116


Edit

You forgot the px on width: 65px! This is being generated.

<div style='width: 65; background: #eeeeee; margin: 0px auto;'>

Also, nice Mario. ;)

铁憨憨 2024-10-01 09:58:26
 echo "<div style='background: {$hex}; height: 5px; width: 5px; position: absolute; top: {$y * 5}; left: {$x * 5};'></div>\r\n";

您需要将它们全部放置在 position:relative; 包含元素中。并且可能也删除

 echo "<div style='background: {$hex}; height: 5px; width: 5px; position: absolute; top: {$y * 5}; left: {$x * 5};'></div>\r\n";

You'll want to place them all within a position: relative; containing element. And probably remove the <br>s too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文