如何在PHP中创建原始图像,不使用任何库


How to create primitive image in PHP w/o any libraries?

我在没有www连接的intranet服务器上运行php,并且没有安装任何图形库。现在,我需要创建一个16x16像素的色块(彩色#86D0FF),我想知道是否有一种方法可以在不安装库的情况下返回如此简单的事情所需的字节序列?

换句话说:我想在没有安装GD的情况下实现以下目标:

<?php 
    header("Content-Disposition: Attachment;filename=image.png"); // so that it can be saved...
    header("Content-type: image/png");
    $img = imagecreate(16,16);
    $color = imagecolorallocate( $image , 134 , 208 , 255 );
    imagefilledrectangle( $image , 0 , 0 , 16 , 16 , $color );
    imagepng( $image );
    imagedestroy( $image );

好的,我找到了一个解决方案。创建样本并保存为JPG、ICO、PNG和GIF,并用十六进制编辑器查看它们,确定GIF看起来是最简单的。所以我现在正在阅读一个现有的GIF(作为模板),只需操作字节[37..39](在那个特定的文件中)来保存颜色值。

我从定义颜色值的CSS文件中获取颜色值,因此也有一些CSS解析。也许代码可以帮助激励你;-)

<?php
   $css = file_get_contents('my.css');
   $col = $_GET["col"];   // file is referenced using /coloursample.php?col=#
   $patt = '/'.colGroup' . $col . '.*?background-color:.*?'#([A-F0-9]{3,6};/six';
   $bin = file_get_contents('sample.gif');
   if (preg_match($patt,$css,$regs))
   {
      $bin[37] = chr(hexdec(substr($regs[1],0,2)));
      $bin[38] = chr(hexdec(substr($regs[1],2,2)));
      $bin[39] = chr(hexdec(substr($regs[1],4,2)));
   }

  header("Content-Disposition: Attachment; filename=color" . $regs[1] . "gif");
  header("Cpntent-type: image/gif");
  echo $bin;